Sun Mar 28, 2010 3:16 pm
import java.io.*;
import java.security.*;
import javax.crypto.*;
public class test {
public static void main(String[] args) throws Exception
{
KeyGenerator keyg = KeyGenerator.getInstance("DES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyg.init(56, random);
SecretKey sk1 = keyg.generateKey();
SecretKey sk2 = keyg.generateKey();
Cipher cp1 = Cipher.getInstance("DES");
cp1.init(Cipher.ENCRYPT_MODE, sk1);
Cipher cp2 = Cipher.getInstance("DES");
cp2.init(Cipher.DECRYPT_MODE, sk2);
Cipher cp3 = Cipher.getInstance("DES");
cp3.init(Cipher.ENCRYPT_MODE, sk1);
FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("encrypted.txt");
CipherOutputStream cos1 = new CipherOutputStream(fos, cp1);
CipherOutputStream cos2 = new CipherOutputStream(cos1, cp2);
CipherOutputStream cos3 = new CipherOutputStream(cos2, cp3);
byte b[] = new byte[8];
int i = fis.read(b);
while(i != -1){
cos3.write(b, 0, i);
i = fis.read(b);
}
fis.close();
cos3.close();
//Decrypt contents from file
FileInputStream fiss = new FileInputStream("encrypted.txt");
FileOutputStream foss = new FileOutputStream("original.txt");
cp1.init(Cipher.DECRYPT_MODE, sk1);
cp2.init(Cipher.ENCRYPT_MODE, sk2);
cp3.init(Cipher.DECRYPT_MODE, sk1);
CipherInputStream cis1 = new CipherInputStream(fiss, cp1);
CipherInputStream cis2 = new CipherInputStream(cis1, cp2);
CipherInputStream cis3 = new CipherInputStream(cis2, cp3);
i = cis3.read(b);
while(i != -1){
foss.write(b, 0, i);
i = cis3.read(b);
}
cis3.close();
foss.close();
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.