Java Reference
In-Depth Information
if (c == encrypt && !encrypted) {
try {
textItem.setText (encryptText(key, text));
statusItem.setText ("encrypted.");
encrypted = true;
}
catch (CryptoException ce) {
throw new RuntimeException (ce.toString());
}
}
else if (c == decrypt && encrypted) {
try {
textItem.setText (decryptText(key,
textItem.getText()));
statusItem.setText ("decrypted.");
encrypted = false;
}
catch (CryptoException ce) {
throw new RuntimeException (ce.toString());
}
}
}
private String encryptText (String key, String text)
throws CryptoException {
byte[] keyBytes= Hex.decode(key.getBytes());
byte[] ptBytes = text.getBytes();
bfCipher = new PaddedBlockCipher
(new CBCBlockCipher
(new BlowfishEngine()));
bfCipher.init(true, new KeyParameter(keyBytes));
byte[] result = new
byte[bfCipher.getOutputSize(ptBytes.length)];
int len = bfCipher.processBytes(ptBytes, 0, ptBytes.length,
result, 0);
bfCipher.doFinal(result, len);
return new String(Hex.encode(result));
}
private String decryptText(String key, String cipherText)
throws CryptoException {
byte[] keyBytes = Hex.decode(key.getBytes());
byte[] textBytes = Hex.decode(cipherText.getBytes());
bfCipher.init(false, new KeyParameter(keyBytes));
byte[] result = new
byte[bfCipher.getOutputSize(textBytes.length)];
int len = bfCipher.processBytes(textBytes, 0,
textBytes.length, result, 0);
bfCipher.doFinal(result, len);
return new String(result).trim();
}
}
Search WWH ::




Custom Search