求救!AES加密程序 求助!需要AES 128 对称算法加密程序例子,要求CTR模式的。哪位好心人肯帮小弟一把?不胜感激呀! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 微软有个RijndaelManaged类,就是AES加密的 http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rijndaelmanaged.aspx简单实现代码如下: /// <summary> /// 获取密钥 /// </summary> public static string Key { get { return @"aabbccddd"; } } /// <summary> /// 获取向量 /// </summary> public static string IV { get { return @"aabbccddeeffgghh"; } } public static string encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV) { MemoryStream msEncrypt = null; RijndaelManaged aesAlg = null; try { aesAlg = new RijndaelManaged(); //aesAlg.Key = Key; //aesAlg.Key = Key; if (Key.Length < 32) { byte[] tt = new byte[32]; for (int i = 0; i < 32; i++) { tt[i] = 0; if (i < Key.Length) tt[i] = Key[i]; } aesAlg.Key = tt; } aesAlg.IV = IV; ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); msEncrypt = new MemoryStream(); using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } } byte[] enstr = msEncrypt.ToArray(); msEncrypt.Flush(); msEncrypt.Close(); return Convert.ToBase64String(enstr); } catch (Exception ex) { throw ex; } finally { if (aesAlg != null) aesAlg.Clear(); } } public static string decryptStringFromBytes_AES(string encryptStr, byte[] Key, byte[] IV) { RijndaelManaged aesAlg = null; string plaintext = null; try { aesAlg = new RijndaelManaged(); byte[] cipherText = Convert.FromBase64String(encryptStr); //aesAlg.Key = Key; if (Key.Length < 32) { byte[] tt = new byte[32]; for (int i = 0; i < 32; i++) { tt[i] = 0; if (i < Key.Length) tt[i] = Key[i]; } aesAlg.Key = tt; } aesAlg.IV = IV; ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) plaintext = srDecrypt.ReadToEnd(); } } } catch (Exception ex) { throw ex; } finally { if (aesAlg != null) aesAlg.Clear(); } return plaintext; } 我也看过RijndaelManaged类,但怎么在里面找不到CTR模式的啊?只有CBC、CFB、CTS、ECB、OFB五种模式。我该怎么办呢? 之前搞错了,现在确定是要ECB模式的,我的代码如下: RijndaelManaged aes = new RijndaelManaged(); ICryptoTransform transform = aes.CreateEncryptor(); aes.Mode = _cipherMode;//ECB aes.Padding = _paddingMode;//PKCS7 aes.KeySize = 128; aes.Key = GetKeyArray(password);//16字节 byte[] data = transform.TransformFinalBlock(inputData, 0, inputData.Length); aes.Clear(); return data;为何返回的data 是32字节的?我第一次接触加密,请高手指教啊! C#有多个源文件吗? 各个源文件之间是怎么组织的? excel 出力 winform存储数据的问题 菜鸟问题 c#函数怎么返回多个值? 怎么构造GetBulkRequest报文及处理返回的报文呢? 我想用TcpListen去监听局域网中的其他电脑的IP和PORT,如何做呢? 关于站内搜索的问题 dataGriwView的Excel出力的问题 用vs2005在windowce模拟器上进行本地化设计的问题,请教本地化高手 Application Center Test怎么用啊??? 问个引用DELPHI的DLL的问题 C#获取xml的问题
http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rijndaelmanaged.aspx简单实现代码如下: /// <summary>
/// 获取密钥
/// </summary>
public static string Key
{
get { return @"aabbccddd"; }
} /// <summary>
/// 获取向量
/// </summary>
public static string IV
{
get { return @"aabbccddeeffgghh"; }
}
public static string encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
{
MemoryStream msEncrypt = null;
RijndaelManaged aesAlg = null; try
{
aesAlg = new RijndaelManaged();
//aesAlg.Key = Key;
//aesAlg.Key = Key;
if (Key.Length < 32)
{
byte[] tt = new byte[32];
for (int i = 0; i < 32; i++)
{
tt[i] = 0;
if (i < Key.Length)
tt[i] = Key[i]; }
aesAlg.Key = tt;
}
aesAlg.IV = IV; ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); msEncrypt = new MemoryStream();
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
}
byte[] enstr = msEncrypt.ToArray();
msEncrypt.Flush();
msEncrypt.Close();
return Convert.ToBase64String(enstr);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
} public static string decryptStringFromBytes_AES(string encryptStr, byte[] Key, byte[] IV)
{
RijndaelManaged aesAlg = null;
string plaintext = null; try
{
aesAlg = new RijndaelManaged();
byte[] cipherText = Convert.FromBase64String(encryptStr);
//aesAlg.Key = Key;
if (Key.Length < 32)
{
byte[] tt = new byte[32];
for (int i = 0; i < 32; i++)
{
tt[i] = 0;
if (i < Key.Length)
tt[i] = Key[i]; }
aesAlg.Key = tt;
}
aesAlg.IV = IV; ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
} return plaintext;
}
。我该怎么办呢?
RijndaelManaged aes = new RijndaelManaged();
ICryptoTransform transform = aes.CreateEncryptor();
aes.Mode = _cipherMode;//ECB
aes.Padding = _paddingMode;//PKCS7
aes.KeySize = 128;
aes.Key = GetKeyArray(password);//16字节
byte[] data = transform.TransformFinalBlock(inputData, 0, inputData.Length);
aes.Clear();
return data;为何返回的data 是32字节的?我第一次接触加密,请高手指教啊!