.net中提供了不少加解密的函式
使用起來非常方便 以下是大概的用法
//記得要先引用加解密類別
using System.Security.Cryptography;
-----
//加密函式
private byte[] encrypt(string string_secretContent, string string_pwd)
{
//密碼轉譯一定都是用byte[] 所以把string都換成byte[]
byte[] byte_secretContent = Encoding.UTF8.GetBytes(string_secretContent);
byte[] byte_pwd = Encoding.UTF8.GetBytes(string_pwd);
//加解密函數的key通常都會有固定的長度 而使用者輸入的key長度不定 因此用hash過後的值當做key
MD5CryptoServiceProvider provider_MD5 = new MD5CryptoServiceProvider();
byte[] byte_pwdMD5 = provider_MD5.ComputeHash(byte_pwd);
//產生加密實體 如果要用其他不同的加解密演算法就改這裡(ex:3DES)
RijndaelManaged provider_AES = new RijndaelManaged();
ICryptoTransform encrypt_AES = provider_AES.CreateEncryptor(byte_pwdMD5, byte_pwdMD5);
//output就是加密過後的結果
byte[] output = encrypt_AES.TransformFinalBlock(byte_secretContent, 0, byte_secretContent.Length);
return output;
}
//解密函式
private string decrypt(byte[] byte_ciphertext, string string_pwd)
{
//密碼轉譯一定都是用byte[] 所以把string都換成byte[]
byte[] byte_pwd = Encoding.UTF8.GetBytes(string_pwd);
//加解密函數的key通常都會有固定的長度 而使用者輸入的key長度不定 因此用hash過後的值當做key
MD5CryptoServiceProvider provider_MD5 = new MD5CryptoServiceProvider();
byte[] byte_pwdMD5 = provider_MD5.ComputeHash(byte_pwd);
//產生解密實體
RijndaelManaged provider_AES = new RijndaelManaged();
ICryptoTransform decrypt_AES = provider_AES.CreateDecryptor(byte_pwdMD5, byte_pwdMD5);
//string_secretContent就是解密後的明文
byte[] byte_secretContent = decrypt_AES.TransformFinalBlock(byte_ciphertext, 0, byte_ciphertext.Length);
string string_secretContent = Encoding.UTF8.GetString(byte_secretContent);
return string_secretContent;
}
要測試的話就像這樣:
string string_secretContent = "科科 我是要加密的文件";
string string_pwd = "我是密碼";
//加密
byte[] byte_ciphertext = encrypt(string_secretContent, string_pwd);
//解密
string result = decrypt(byte_ciphertext, string_pwd);
[附註]
.net中似乎沒有提供AES的加解密 上網搜尋了一下wiki
其中在http://en.wikipedia.org/wiki/Advanced_Encryption_Standard這頁中提到:
"In cryptography, the Advanced Encryption Standard (AES), also known as Rijndael, is a block cipher adopted as an encryption standard by the U.S. government. It has been analyzed extensively and is now used worldwide, as was the case with its predecessor, the Data Encryption Standard (DES). "
"Strictly speaking, AES is not precisely Rijndael (although in practice they are used interchangeably) as Rijndael supports a larger range of block and key sizes; AES has a fixed block size of 128 bits and a key size of 128, 192, or 256 bits, whereas Rijndael can be specified with key and block sizes in any multiple of 32 bits, with a minimum of 128 bits and a maximum of 256 bits."
所以Rijndael算是AES比較有彈性的版本 因此在.net中要使用AES加密就改用RijndaelManaged類別吧
使用起來非常方便 以下是大概的用法
//記得要先引用加解密類別
using System.Security.Cryptography;
-----
//加密函式
private byte[] encrypt(string string_secretContent, string string_pwd)
{
//密碼轉譯一定都是用byte[] 所以把string都換成byte[]
byte[] byte_secretContent = Encoding.UTF8.GetBytes(string_secretContent);
byte[] byte_pwd = Encoding.UTF8.GetBytes(string_pwd);
//加解密函數的key通常都會有固定的長度 而使用者輸入的key長度不定 因此用hash過後的值當做key
MD5CryptoServiceProvider provider_MD5 = new MD5CryptoServiceProvider();
byte[] byte_pwdMD5 = provider_MD5.ComputeHash(byte_pwd);
//產生加密實體 如果要用其他不同的加解密演算法就改這裡(ex:3DES)
RijndaelManaged provider_AES = new RijndaelManaged();
ICryptoTransform encrypt_AES = provider_AES.CreateEncryptor(byte_pwdMD5, byte_pwdMD5);
//output就是加密過後的結果
byte[] output = encrypt_AES.TransformFinalBlock(byte_secretContent, 0, byte_secretContent.Length);
return output;
}
//解密函式
private string decrypt(byte[] byte_ciphertext, string string_pwd)
{
//密碼轉譯一定都是用byte[] 所以把string都換成byte[]
byte[] byte_pwd = Encoding.UTF8.GetBytes(string_pwd);
//加解密函數的key通常都會有固定的長度 而使用者輸入的key長度不定 因此用hash過後的值當做key
MD5CryptoServiceProvider provider_MD5 = new MD5CryptoServiceProvider();
byte[] byte_pwdMD5 = provider_MD5.ComputeHash(byte_pwd);
//產生解密實體
RijndaelManaged provider_AES = new RijndaelManaged();
ICryptoTransform decrypt_AES = provider_AES.CreateDecryptor(byte_pwdMD5, byte_pwdMD5);
//string_secretContent就是解密後的明文
byte[] byte_secretContent = decrypt_AES.TransformFinalBlock(byte_ciphertext, 0, byte_ciphertext.Length);
string string_secretContent = Encoding.UTF8.GetString(byte_secretContent);
return string_secretContent;
}
要測試的話就像這樣:
string string_secretContent = "科科 我是要加密的文件";
string string_pwd = "我是密碼";
//加密
byte[] byte_ciphertext = encrypt(string_secretContent, string_pwd);
//解密
string result = decrypt(byte_ciphertext, string_pwd);
[附註]
.net中似乎沒有提供AES的加解密 上網搜尋了一下wiki
其中在http://en.wikipedia.org/wiki/Advanced_Encryption_Standard這頁中提到:
"In cryptography, the Advanced Encryption Standard (AES), also known as Rijndael, is a block cipher adopted as an encryption standard by the U.S. government. It has been analyzed extensively and is now used worldwide, as was the case with its predecessor, the Data Encryption Standard (DES). "
"Strictly speaking, AES is not precisely Rijndael (although in practice they are used interchangeably) as Rijndael supports a larger range of block and key sizes; AES has a fixed block size of 128 bits and a key size of 128, 192, or 256 bits, whereas Rijndael can be specified with key and block sizes in any multiple of 32 bits, with a minimum of 128 bits and a maximum of 256 bits."
所以Rijndael算是AES比較有彈性的版本 因此在.net中要使用AES加密就改用RijndaelManaged類別吧
5 意見:
張貼留言AES DES 3DES加解密都一樣
請問您要怎麼測試阿~??
要在哪裡放你說的測試程式碼?
請問要怎麼用你說的測試?
程式碼要放哪裡?
有哪些步驟??
^^ 謝謝~~
非常感謝!!
張貼留言