aes256 java_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值