Monday, May 12, 2008

Encrypts a string by the given "key"

A more slightly advanced encryption.Encrypts a string by the given "key".
import javax.swing.JOptionPane;


public class betterencryptor {

 public static void main(String[] args) {
  int key1 = Integer.parseInt(JOptionPane.showInputDialog("Input a key, a random large *prime* number:"));
  String message1 = JOptionPane.showInputDialog("Input a string to be encrypted/decrypted:");
  String crypt1 = cryptor(message1,key1);
  String decrypt1 = cryptor(crypt1,key1);
  System.out.println(crypt1);
  System.out.println(decrypt1);
 }


 public static String cryptor(String crypt,int key){
  byte[] string = crypt.getBytes();


  for(int k = 0;k < crypt.length();k++){
   string[k] = new Integer(string[k]^key%256).byteValue();
  }
  return (new String(string));
 }
}
 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory