How do I decrypt message encrypt in here?

前端 未结 2 645
走了就别回头了
走了就别回头了 2021-02-06 19:58
try{
    String plainData=\"my name is laksahan\",cipherText,decryptedText;
    KeyGenerator keyGen = KeyGenerator.getInstance(\"AES\");
    keyGen.init(128);
    Secret         


        
相关标签:
2条回答
  • 2021-02-06 20:17

    try this

     byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
            Cipher aesCipher = Cipher.getInstance("AES");
            aesCipher.init(Cipher.DECRYPT_MODE, secretKeyUsed while encrypting);
            byte[] plainData = aesCipher.doFinal(data);
            return new String(plainData);
    
    0 讨论(0)
  • 2021-02-06 20:30

    Try this code,it works fine on my pc.Good luck!

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import sun.misc.BASE64Encoder;
    import sun.misc.BASE64Decoder;
    
    public class AESExample
    {
        public static void main(String[] args)
        {
            try
            {
                String plainData = "my name is laksahan", cipherText, decryptedText;
                KeyGenerator keyGen = KeyGenerator.getInstance("AES");
                keyGen.init(128);
                SecretKey secretKey = keyGen.generateKey();
                cipherText = encrypt(plainData, secretKey);
                System.out.println(cipherText);
                decryptedText = decrypt(cipherText, secretKey);
                System.out.println(decryptedText);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
    
        }
    
        public static String encrypt(String plainData, SecretKey secretKey) throws Exception
        {
            Cipher aesCipher = Cipher.getInstance("AES");
            aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] byteDataToEncrypt = plainData.getBytes();
            byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
            return new BASE64Encoder().encode(byteCipherText);
        }
    
        public static String decrypt(String cipherData, SecretKey secretKey) throws Exception
        {
            byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
            Cipher aesCipher = Cipher.getInstance("AES");
            aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] plainData = aesCipher.doFinal(data);
            return new String(plainData);
        }
    
    }
    

    If you want to use a customer key,try the following code,just remember the key length is 128 bit. By the way ,I prefer to store my key in keystore file!

    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    import sun.misc.BASE64Encoder;
    import sun.misc.BASE64Decoder;
    
    public class AESExample
    {
    
        public static void main(String[] args)
        {
            try
            {
                byte[]key={-4, -14, 106, -75, -9, 65, -95, 77, -52, 73, -87, -101, 80, 94, -59, -66};
                String plainData = "my name is laksahan", cipherText, decryptedText;
                System.out.println(key.length);
                cipherText = encrypt(plainData, key);
                System.out.println(cipherText);
                decryptedText = decrypt(cipherText, key);
                System.out.println(decryptedText);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        public static String encrypt(String plainData, byte[] key) throws Exception
        {
            Cipher aesCipher = Cipher.getInstance("AES");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
            aesCipher.init(Cipher.ENCRYPT_MODE, keySpec);
            byte[] byteDataToEncrypt = plainData.getBytes();
            byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
            return new BASE64Encoder().encode(byteCipherText);
        }
    
        public static String decrypt(String cipherData, byte[] key) throws Exception
        {
            byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
            Cipher aesCipher = Cipher.getInstance("AES");
            aesCipher.init(Cipher.DECRYPT_MODE, keySpec);
            byte[] plainData = aesCipher.doFinal(data);
            return new String(plainData);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题