我按以下方式加密文本:
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);
}
在上面的代码中,我加密了字符串tester
。我该如何解密?
答案 0 :(得分:2)
首先获取加密字符串:
final String encryptedString = Base64.encodeBase64String(encrypted)
然后使用:
解密 cipher.init(Cipher.DECRYPT_MODE, secretKey);
final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptedString)));