Saturday, June 23, 2007


SecretWriting

The Secret Writing uses classes that are found only in the Java Cryptography Extension (JCE). The JCE contains cryptographic software whose export is limited by the U.S. government.

The SecretWriting program encrypts and decrypts text. Here is a sample session:

C:\ java SecretWriting -e Hello, world!
Lc4WKHP/uCls8mFcyTw1pQ==

C:\ java SecretWriting -d Lc4WKHP/uCls8mFcyTw1pQ==
Hello, world!

The -e option encrypts data, and the -d option decrypts it. A cipher is used to do this work. The cipher uses a key. Different keys will produce different results. SecretWriting stores its key in a file called SecretKey.ser. The first time you run the program, SecretWriting generates a key and stores it in the file. Subsequently, the key is loaded from the file. If you remove the file, SecretWriting will create a new key. Note that you must use the same key to encrypt and decrypt data. This is a property of a symmetric cipher

"Hello, world!" can be encrypted to many different values, depending on the key that you use. Here are a few sample ciphertexts:

Lc4WKHP/uCls8mFcyTw1pQ==
xyOoLnWOH0eqRwUu3rQHJw==
hevNJLNowIzrocxplKI7dQ==

The source code for this example is longer than the last one, but it's also a more capable program:

import java.io.*;
import java.security.*;
import javax.crypto.*;
import sun.misc.*;
public class SecretWriting {
public static void main(String[] args) throws Exception {
// Check arguments.
if (args.length <>
System.out.println("Usage: SecretWriting -e|-d text");
return;
}
// Get or create key.
Key key;
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("SecretKey.ser"));
key = (Key)in.readObject();
in.close();
}
catch (FileNotFoundException fnfe) {
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
key = generator.generateKey();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("SecretKey.ser"));
out.writeObject(key);
out.close();
}
// Get a cipher object.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Encrypt or decrypt the input string.
if (args[0].indexOf("e") != -1) {
cipher.init(Cipher.ENCRYPT_MODE, key);
String amalgam = args[1];
for (int i = 2; i <>
amalgam += " " + args[i];
byte[] stringBytes = amalgam.getBytes("UTF8");
byte[] raw = cipher.doFinal(stringBytes);
BASE64Encoder encoder = new BASE64Encoder();
Java Cryptography
pa ge 11
String base64 = encoder.encode(raw);
System.out.println(base64);
}
else if (args[0].indexOf("d") != -1) {
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw = decoder.decodeBuffer(args[1]);
byte[] stringBytes = cipher.doFinal(raw);
String result = new String(stringBytes, "UTF8");
System.out.println(result);
}
}
}


SecretWriting has to generate a key the first time you use it. Now this is a demonstration program. What Im trying to implement is a GUI where a User could input text and get it decrypted in an easier way. The design phase has started. Soon the work would start .

Lolzz//...Seriously Encryption has really attracted me....Ye ye

1 comment:

Anonymous said...

nandu u technie guy..read lota german in ur site.....plz decipher it for me...!