H5W3
当前位置:H5W3 > 其他技术问题 > 正文

Java AES 256 解密

题目描述

我用JAVA在實行解密,參考了c#的範例,加密沒有問題 KEY和IV都是相同的

题目来源及自己的思路

相关代码

JAVA的代碼

public static String decrypt(String hashKey, String hashIv, String value) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(hashKey.getBytes("UTF-8"), "AES");
            IvParameterSpec iv = new IvParameterSpec(hashIv.getBytes("UTF-8"));
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(RemovePKCS7Padding(HexToBytes(value)));
            return new String(Base64.encodeBase64(encrypted), "UTF-8");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private static byte[] RemovePKCS7Padding(byte[] data) {
        int ilength = data[data.length - 1];
        byte[] output = new byte[data.length - ilength];
        System.arraycopy(data, 0, output, 0, output.length);
        return output;
    }

    public static byte[] HexToBytes(String value) {
        int hexStringLength = value.length();
        byte[] b = new byte[hexStringLength / 2];
        for (int i = 0; i < hexStringLength; i += 2) {
            int topChar = (value.charAt(i) > 0x40 ? value.charAt(i) - 0x37 : value.charAt(i) - 0x30) << 4;
            int bottomChar = value.charAt(i + 1) > 0x40 ? value.charAt(i + 1) - 0x37 : value.charAt(i + 1) - 0x30;
            b[i / 2] = (byte) (topChar + bottomChar);
        }
        return b;
    }

c#代碼

public string DecryptAES256(string encryptData)//解密 
 {   
 string sSecretKey = "Xgmz5mMUm7JdpPI7mRXIITSNjPEUtV7f";   
 string iv = "nxKLik2dMNPUqIJy";   
 var encryptBytes = HexStringToByteArray(encryptData.ToUpper());  
  var aes = new RijndaelManaged();   
  aes.Key = Encoding.UTF8.GetBytes(sSecretKey);   
  aes.IV = Encoding.UTF8.GetBytes(iv);  
   aes.Mode = CipherMode.CBC;  
    aes.Padding = PaddingMode.None;  
     ICryptoTransform transform = aes.CreateDecryptor();
        return  Encoding.UTF8.GetString(RemovePKCS7Padding(transform.TransformFinalBl ock(e  ncryptBytes, 0, encryptBytes.Length))); 
        }  
 private static byte[] RemovePKCS7Padding(byte[] data)  {  
  int iLength = data[data.Length - 1]; 
    var output = new byte[data.Length - iLength];  
     Buffer.BlockCopy(data, 0, output, 0, output.Length);  
      return output; 
       } 
private static byte[] HexStringToByteArray(string hexString)  {
  int hexStringLength = hexString.Length;   
  byte[] b = new byte[hexStringLength / 2];  
   for (int i = 0; i < hexStringLength; i += 2)   {   
   int topChar = (hexString[i] > 0x40 ? hexString[i] - 0x37 : hexString[i]  - 0x30) << 4;   
   int bottomChar = hexString[i + 1] > 0x40 ? hexString[i + 1] - 0x37 :  hexString[i + 1] - 0x30;   b[i / 2] = Convert.ToByte(topChar + bottomChar);  
    }   
    return b;
     }

你期待的结果是什么?实际看到的错误信息又是什么?

c#的沒問題,官方給的範例
Key : Xgmz5mMUm7JdpPI7mRXIITSNjPEUtV7f
IV : nxKLik2dMNPUqIJy
解密資料 : fb7a19d840c9877d26d961f6a906602439260588e0e9db45cdc0d4d69a3b97fe22e00fda051ee90c7e987e62a717d409a45e4c04893caa90b31f86dc32929debb391145325f07068854efb5977e9aed0b684e7b0a1cb45a764bad9f4d9ab32cb1f634c66e315054b2d3589a1d9fc0ad3dfdb8dad102df281c306c25972047d4e

正常結果為: MerchantID=MS15295340&RespondType=JSON&TimeStamp=1485232229&Version=1.4&MerchantOrderNo=S_1485232288&Amt=40&ItemDesc=UnitTest

出現此錯誤 Input length not multiple of 16 bytes

回答:

需要自写填充,加密字符串长度填充为16的倍数。

本文地址:H5W3 » Java AES 256 解密

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址