0

我通过以下方式加密文本:

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。我如何解密它?

4

1 回答 1

2

首先获取加密字符串:

final String encryptedString = Base64.encodeBase64String(encrypted)

然后使用解密:

 cipher.init(Cipher.DECRYPT_MODE, secretKey);
final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptedString)));
于 2013-08-15T14:14:35.220 回答