以下C#代码是解密 API wx.getUserInfo 返回的encryptedData ,但是会出现持续性的解密出来是一串乱码,如解密成功了,持续的全部解密成功, 直到下次有解密失败的,又持续的解密失败。大伙有没有更好的办法吗?
private static byte[] AES_decrypt(string input, string iv, string key)
{
RijndaelManaged aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 128;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.None;
aes.Key = Convert.FromBase64String(key);
aes.IV = Convert.FromBase64String(iv);
var decrypt = aes.CreateDecryptor(aes.Key, aes.IV);
byte[] xBuff = null;
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
{
byte[] xXml = Convert.FromBase64String(input);
byte[] msg = new byte[xXml.Length + 32 - xXml.Length % 32];
Array.Copy(xXml, msg, xXml.Length);
cs.Write(xXml, 0, xXml.Length);
}
xBuff = decode2(ms.ToArray());
}
return xBuff;
}
private static byte[] decode2(byte[] decrypted)
{
int pad = (int)decrypted[decrypted.Length - 1];
if (pad < 1 || pad > 32)
{
pad = 0;
}
byte[] res = new byte[decrypted.Length - pad];
Array.Copy(decrypted, 0, res, 0, decrypted.Length - pad);
return res;
}
对称解密算法初始向量 为Base64_Decode(iv),其中iv由数据接口返回。
他们说这样获取 iv 是怎么获取的 我现在一直用的固定的值 就是 r7BXXKkLb8qrSNn05n0qiA==
主要 我这是 可以解密 但是就是 每次都是前边几个字符乱码
加密数据
i0iKgp4BsaCTybO0/60Lx+zVXIvHwy27r7rwGTOAQlegJqTmWgknogzLIKQuvVpQgXAAXKMNVkdGrmY3hLx6es8xnMBWjzwY2CAtXzh/p/oL/wtiqL8frz4aUqzfaMDsAp8ccoRZS2BeWEHh44xR2DsAtqkrMU5QhqPXZ1c10zE/fYgIHVpsg1nonTP/BKvrgOLRUK7a19oaU3ZB6lpiT175LK3UdACfOwWDZSw+qBuwMAhwnrxvnYrnHJFHh1R3+GoaHgpLo17aoLCO/NXjecVlzeNECuIdkbJWLsxZSx5RbI0yaqyEe6N3nG/AFg8uusipZblBnSvm9k6CW+AgVfrCbya13zMLMSNb1P/yt2jJPVstcAc3Fl1ZBaSoz54jS44v9iTTYLIBjylFNi5UywUqysU8rKehMjMqfbI9vhLdF/Iv9UqJDgv/7dv7hSPpd4dPt2edbzk7F/Nr1UF9rRt3ZzuvcVMj21GPdkA+mNZleK/vcReuuZeQxvDPZrCs1fGUswaKQoEECZthde20Sw==
sessionKey GcD1xwDH7O+5s7ohGwlCHA==
你能用你的程序帮我解下么? 看好着没? 谢谢了哈
你能帮我解下看好这么?》