From: "Pavlov, Tony" <Pavlovtp@MSX.UPMC.EDU>
To: "'java-security@java.sun.com'" <java-security@web2.javasoft.com>
Subject: JCE - Encrypting/Decrypting TextArea
Date: Wed, 23 Jul 1997 11:09:20 -0400
Hi,
I tried to build a simple applet to encrypt/decrypt a TextArea.
But it does not seam to work.
Do you have any code samples dealing with ciphers encrypting/decrypting
big strings
text areas etc.
Please, help!
Thanks,
Tony
public boolean encryptTextArea(String passphrase,
TextArea
aTextArea) {
KeyGenerator keygen;
Key key;
String sTextArea;
Cipher cipher;
Cipher clear;
byte[] cipherBuffer = new byte[5120];
byte[] clearBuffer = new byte[5210];
sTextArea = new String(aTextArea.getText());
byte[] textBuffer = new byte[sTextArea.length()];
for(int i = 0; i < sTextArea.length(); i++) {
textBuffer[i] = (byte) sTextArea.charAt(i);
}
try {
SecureRandom random = new SecureRandom();
keygen = KeyGenerator.getInstance("DES");
keygen.initialize(random);
key = keygen.generateKey();
cipher = Cipher.getInstance("DES/ECB/PKCS#5");
clear = Cipher.getInstance("DES/ECB/PKCS#5");
cipher.initEncrypt(key);
cipherBuffer = cipher.crypt(textBuffer);
clear.initDecrypt(key);
clearBuffer = clear.crypt(cipherBuffer);
}
catch (Exception e) {
e.printStackTrace();
return(false);
}
return (true);
}