现在我只有解密方式
代码如下
/// <summary>
/// AES解密
/// </summary>
/// <param name="text">密文</param>
/// <param name="key">密钥,长度为16的字符串</param>
/// <param name="iv">偏移量,长度为16的字符串</param>
/// <returns>明文</returns>
public static string DecodeAES(string text, string key, string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.Zeros;
rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] encryptedData = HexStrToByte(text);//
//byte[] encryptedData=System.Text.UTF8Encoding.UTF8.GetBytes(text);
byte[] pwdBytes = System.Text.UTF8Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[16];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
byte[] ivBytes = UTF8Encoding.UTF8.GetBytes(iv); //偏移向量
byte[] ivBytesNew = new byte[16];
len = ivBytes.Length;
if (len > 16)
len = ivBytesNew.Length;
System.Array.Copy(ivBytes, ivBytesNew, len); //复制IV16位作为密钥
rijndaelCipher.IV = ivBytesNew;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return UTF8Encoding.UTF8.GetString(plainText);
}
/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
private static byte[] HexStrToByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
加密出来的结果示例如下
0ad3773f966d9334a1d76a637c2d7d498dea2e88830443a60ff76a241ee752b12470b1898fed8e6473a7babef2565bd0902cf260dea098f2c9f4bce0240f567a123206474dcea72171a36c46b2ad6ef68ed769743f3e0c20c6a954235b6d010e17a9d682955f4c2f69eeb9d83be53cc3f651516eec7dae1c2df18d4edd47e88fb1a811ac3730c73b7b2abe93b54ee02ee9f272cc341887c07c30f5cc05a1ed03636b2b808b3a6d25767dc728ae699f57302f33794d665a95276832f300ee7c0989ae8df41c66e84b72ed1db5137b83a858d0880bd976692804dee34a062c87230e4f6d821a751c858931161ad4c8b6ac86b187c05c40c71920ccd798829c60d5722c475c724e463a412f594ea27967273ef4f02f52ec4158997f8b44039cd6651829c284cd85222b72f3f3d91a6a0990fe589b1219c1a764a1b75e09bb95af9fd9d0ac7f396f4f5648f3d657e2fb3e58ccb32404e17eb25184ee1a6a70be72892377d0503f4e4991d1f2f94630133d0d2ba2890cfe9a0608e04089f2aa38335f79086cb6b113dd1b70293ff2929102337eb57804ab2beb77b05667bf60c811932e395479cda51c51a2a4f49d44ad4ba1a77a26c150ea1d074b6430c3e60b93d2767db903e703ce67ca4adb07f729a0ac275725d186ebaa08412d5797a52ce7b4e7a53d9e4878a2557a454bb34240d075dc8f40f7c9f6e47687cafd317a8f24914b0d101446cae72925629d6c21086f569e22b83b2ef258c5330ab9a3eef9691fdfe1ee1c2c9481e48c974bc6d2e54d52
用上面的方式可以解密这段代码
解密方法已经有了 我现在想知道 对应的加密方法是怎么样的
找了几个对应的加密方法 加密出来的结果不是这种数字加字母格式的 想知道这种结果是用什么方式加密出来的
– 飞翔的鱼 5年前