des和Rijndael加密

系统 1669 0

------------IV的作用:
为了保证数据的安全,.NET基类库中提供的私钥算法类使用称作密码块链(CBC,Cipher Block Chaining)的链模式,算法使用一个密钥和一个初始化向量(IV,Initialization Vector)对数据执行加密转换。密钥和初始化向量IV一起决定如何加密数据,以及如何将数据解密为原始数据。通信双方都必须知道这个密钥和初始化向量才能够加密和解密数据。

初始化向量IV的作用:防止随机产生的明文和密文相同
------------加密类:
#region public static DES encrypt for string
///<summary>
///static DES encrypt for string
///</summary>
///<returns>encrypted string</returns>
///<param name="OriginalString">original string</param>
///<param name="EncryptKey">encrypt key, must 8 characters</param>
///<param name="EncryptOffset">encrypt IV, must >= 8 characters</param>
public static string DesEncryptString(string OriginalString, string EncryptKey, string EncryptOffset)
{
byte[] btKey = Encoding.Default.GetBytes(EncryptKey);
byte[] btIV = Encoding.Default.GetBytes(EncryptOffset);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Encoding.Default.GetBytes(OriginalString);
try
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}

return Convert.ToBase64String(ms.ToArray());
}
catch
{
return "";
}
}
}

#endregion

#region public static DES decrypt for string
///<summary>
///DES decrypt for string
///</summary>
///<returns>dncrypted string</returns>
///<param name="EncryptedString">original string</param>
///<param name="EncryptKey">encrypt key, must 8 characters</param>
///<param name="EncryptOffset">encrypt IV, must >= 8 characters</param>
public static string DesDecryptString(string EncryptedString, string EncryptKey, string EncryptOffset)
{
byte[] btKey = Encoding.Default.GetBytes(EncryptKey);
byte[] btIV = Encoding.Default.GetBytes(EncryptOffset);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Convert.FromBase64String(EncryptedString);
try
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}

return Encoding.Default.GetString(ms.ToArray());
}
catch
{
return "";
}
}
}
#endregion

#region public static DES encrypt for file
/// <summary>
/// DES encrypt for file
/// </summary>
/// <param name="m_InFilePath">Input file path</param>
/// <param name="m_OutFilePath">Output file path</param>
/// <param name="strEncrKey">key, must 8 characters</param>
///<param name="strEncrOffset">encrypt IV, must >= 8 characters</param>
/// <returns>success or not</returns>
public static bool DESEncryptFile(string m_InFilePath, string m_OutFilePath, string strEncrKey, string strEncrOffset)
{
if (File.Exists(m_InFilePath) == false)
{
return false;
}

byte[] btKey = Encoding.Default.GetBytes(strEncrKey);
byte[] btIV = Encoding.Default.GetBytes(strEncrOffset);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(m_InFilePath);

using (FileStream fs = new FileStream(m_OutFilePath, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}

fs.Close();
}
catch
{
fs.Close();
return false;
}
}

return true;
}
#endregion

#region public static DES decrypt for file
/// <summary>
/// DES decrypt for file
/// </summary>
/// <param name="m_InFilePath">Input file path</param>
/// <param name="m_OutFilePath">Output file path</param>
/// <param name="sDecrKey">key, must 8 characters</param>
///<param name="strEncrOffset">encrypt IV, must >= 8 characters</param>
/// <returns>success or not</returns>
public static bool DESDecryptFile(string m_InFilePath, string m_OutFilePath, string sDecrKey, string strEncrOffset)
{
if (File.Exists(m_InFilePath) == false)
{
return false;
}

byte[] btKey = Encoding.Default.GetBytes(sDecrKey);
byte[] btIV = Encoding.Default.GetBytes(strEncrOffset);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(m_InFilePath);

using (FileStream fs = new FileStream(m_OutFilePath, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
fs.Close();
}
catch
{
fs.Close();
return false;
}
}

return true;
}
#endregion

#region public static AES/CBC/PKCS7Padding encrypt for string
///<summary>
///static AES/CBC/PKCS7Padding encrypt for string
///</summary>
///<returns>encrypted string</returns>
///<param name="OriginalString">original string</param>
///<param name="EncryptKey">encrypt key, must 8 characters</param>
///<param name="EncryptOffset">encrypt IV, must >= 8 characters</param>
public static string AESEncryptString(string OriginalString, string EncryptKey, string EncryptOffset)
{
try
{
byte[] btKey = Encoding.Default.GetBytes(EncryptKey);
byte[] btIV = Encoding.Default.GetBytes(EncryptOffset);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = btKey;
rDel.Mode = CipherMode.CBC;
rDel.Padding = PaddingMode.PKCS7;
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Encoding.Default.GetBytes(OriginalString);
using (CryptoStream cs = new CryptoStream(ms, rDel.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Convert.ToBase64String(ms.ToArray());
}
}
catch
{
return "";
}
}
#endregion

#region public static AES/CBC/PKCS7Padding decrypt for string
///<summary>
///AES/CBC/PKCS7Padding decrypt for string
///</summary>
///<returns>dncrypted string</returns>
///<param name="EncryptedString">original string</param>
///<param name="EncryptKey">encrypt key, must 8 characters</param>
///<param name="EncryptOffset">encrypt IV, must >= 8 characters</param>
public static string AESDecryptString(string EncryptedString, string EncryptKey, string EncryptOffset)
{
try
{
byte[] btKey = Encoding.Default.GetBytes(EncryptKey);
byte[] btIV = Encoding.Default.GetBytes(EncryptOffset);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = btKey;
rDel.Mode = CipherMode.CBC;
rDel.Padding = PaddingMode.PKCS7;
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Convert.FromBase64String(EncryptedString);
using (CryptoStream cs = new CryptoStream(ms, rDel.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Encoding.Default.GetString(ms.ToArray());
}
}
catch
{
return "";
}
}
#endregion
}

des和Rijndael加密


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论