龙空技术网

DES对称加密,解密

小虎学学 133

前言:

此刻兄弟们对“c语言实现des加密解密”都比较关心,小伙伴们都需要学习一些“c语言实现des加密解密”的相关资讯。那么小编在网摘上网罗了一些对于“c语言实现des加密解密””的相关文章,希望同学们能喜欢,看官们快快来学习一下吧!

public class DES

{

/// <summary>

/// DES加密

/// </summary>

/// <param name="data">加密数据</param>

/// <param name="key">8位字符的密钥字符串</param>

/// <param name="iv">8位字符的初始化向量字符串</param>

/// <returns></returns>

public static string DESEncrypt(string data, string key, string iv)

{

byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);

byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

int i = cryptoProvider.KeySize;

MemoryStream ms = new MemoryStream();

CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

StreamWriter sw = new StreamWriter(cst);

sw.Write(data);

sw.Flush();

cst.FlushFinalBlock();

sw.Flush();

return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

}

/// <summary>

/// DES解密

/// </summary>

/// <param name="data">解密数据</param>

/// <param name="key">8位字符的密钥字符串(需要和加密时相同)</param>

/// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param>

/// <returns></returns>

public static string DESDecrypt(string data, string key, string iv)

{

byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);

byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

byte[] byEnc;

try

{

byEnc = Convert.FromBase64String(data);

}

catch

{

return null;

}

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

MemoryStream ms = new MemoryStream(byEnc);

CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);

StreamReader sr = new StreamReader(cst);

return sr.ReadToEnd();

}

}

有需要的朋友Ctrl+c,Ctrl+v,同时欢迎批评指正

标签: #c语言实现des加密解密