Reputation: 21329
I encrypt the text in the following way :
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String input = "tester";
byte encrypted[] = cipher.doFinal(input.getBytes());
// PRINT ENCRYPTED TEXT
System.out.println(new String(Base64.encodeBytes(encrypted)));
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
In the above code I encrypt the string tester
. How do I decrypt it ?
Upvotes: 0
Views: 136
Reputation: 68715
First get the encrypted string :
final String encryptedString = Base64.encodeBase64String(encrypted)
and then decrypt using:
cipher.init(Cipher.DECRYPT_MODE, secretKey);
final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptedString)));
Upvotes: 2