简体   繁体   中英

How do I decrypt once I have encrypted?

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 ?

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)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM