Cryptography is a very wide area in the computer technology. Most of new Information systems tend to have good security mechanisms withing the system. People use many languages to implement security in their information systems. One of the great language for security issues is the JAVA. The JAVA support many cryptographic algorithms and mechanisms withing its cryptographic library. Here I'll give some example to implement a encryption and decryption mechanism in your system.
To implement security you should have reference to the following libraries in your application.
import java.security.*;
import javax.crypto.*;
Then you have to create a cipher object.
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
Then you can generate a key for encryption using key generator in java cryptography.
KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(128);
Key encryptionKey = generator.generateKey();
Here are some streams to read and write files.
FileInputStream fis = new FileInputStream("toencrypt.txt");
DataInputStream fdata=new DataInputStream(fis);
FileOutputStream fos = new FileOutputStream("encrypted.txt");
Start the encryption and read file and write encrypted to file.
cipher.init(Cipher.ENCRYPT_MODE,encryptionKey);
CipherOutputStream cOut = new CipherOutputStream(fos,cipher);
DataOutputStream dout = new DataOutputStream(cOut);
Byte s;
while ((s=fdata.readByte())!=null) {
dout.writeByte(s);
}
You should have save this key and IV used by this algorithm to decrypt the file.
Here I used AES as encryption algorithm and CBC (cipher block chaining mode) as the mode of algorithm.
Monday, January 21, 2008
Subscribe to:
Post Comments (Atom)
1 comment:
Add more post
Post a Comment