求des C#算法

\u8c01\u80fd\u5e2e\u6211\u5c06java\u4e2d\u7684DES\u7b97\u6cd5\u6539\u6210\u7528C#\u6765\u5b9e\u73b0\uff1f

C#\u4e5f\u6709DES\u52a0\u5bc6\u3002

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

///
/// DES\u52a0\u5bc6\u89e3\u5bc6
///
public static class DES
{


//\u9ed8\u8ba4\u5bc6\u94a5\u5411\u91cf
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

///
/// DES\u52a0\u5bc6\u5b57\u7b26\u4e32
///
/// \u5f85\u52a0\u5bc6\u7684\u5b57\u7b26\u4e32
/// \u52a0\u5bc6\u5bc6\u94a5,\u8981\u6c42\u4e3a8\u4f4d
/// \u52a0\u5bc6\u6210\u529f\u8fd4\u56de\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u4e32,\u5931\u8d25\u8fd4\u56de\u6e90\u4e32
public static string Encode(string encryptString, string encryptKey)
{
encryptKey = StringHelper.GetSubString(encryptKey, 8, "");
encryptKey = encryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());

}

///
/// DES\u89e3\u5bc6\u5b57\u7b26\u4e32
///
/// \u5f85\u89e3\u5bc6\u7684\u5b57\u7b26\u4e32
/// \u89e3\u5bc6\u5bc6\u94a5,\u8981\u6c42\u4e3a8\u4f4d,\u548c\u52a0\u5bc6\u5bc6\u94a5\u76f8\u540c
/// \u89e3\u5bc6\u6210\u529f\u8fd4\u56de\u89e3\u5bc6\u540e\u7684\u5b57\u7b26\u4e32,\u5931\u8d25\u8fd4\u6e90\u4e32
public static string Decode(string decryptString, string decryptKey)
{
try
{
decryptKey = StringHelper.GetSubString(decryptKey, 8, "");
decryptKey = decryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return "";
}

}


}

ASCII \u548cUTF8\u7684\u95ee\u9898\u5427

Des算法:
Des算法是一种标准的对称加密算法,其精华就是把明文经过一系列的复杂变化后搞的面目全非,一般在不知密码的情况下要把它的恢复很难。
算法可以由四个部分描述:1.获取16个子密钥 2.初始置换置换函数ip 3.加密函数f 4.末置换函数
一下是分解动作:
1.获取16个子密钥:
密码是64位的布尔值,经过以下步骤得到16个48位的字密钥:
(1)去掉每个第八位,然后通过置换表,得到56位的子密钥
(2) 分成前后28位
(3)根据表对前后28为进行1~16轮的1或2位的循环
(4)移位后,再将两部分合并成56位,然后通过压缩置换得到48位子密钥
2.初始置换函数:
就是把64位的明文根据置换表置换成56位的明文,然后分成左右两部分
3.密码函数f
密码函数F的输入为32位的数据和48位的子密
E是扩展置换,把32位部分明文扩展成48位再与48的子密钥抑或,再经过s盒转化,成32位输出,最后再经过一个置换(p盒)就得到了密码函数的输出了。
然后把密码函数的输出跟初始置换后的左边32位抑或,结果作为新明文的后边,原右边作为新左边。
然后用16个子密钥重复上面的行为。
s盒的过程:把48位分成8个6位,取6位的第一位和最后一位,这两位组成的数作为s盒的行,其他三位作为列,知道行列后就在s盒表中查到对应的的数,这个数是4位的,就用这个4位的数代替该6位的数。所以最后出来的是32位了。
4.f函数后,重组左右部分,得到64位,再一次末置换就得到了用des加密后的密文。
DES对称加密算法归纳总结:
(i)子密钥生成:
C[0]D[0]=PC-1(K)
for 1<=i<=16
{
C[i]=LS(i)(C[i-1])
D[i]=LS(i)(D[i-1])
K[i]= PC-2(C[i]D[i])
}
(ii) 加密过程:
L[0]R[0]=IP(x)
for 1<=i<=16
{
L[i]=R[i-1]
R[i]= L[i-1]XOR f(R[i-1],K[i])
}
c=IP-1(R[16]L[16])
(iii) 解密过程:置换是逆置换
R[16]L[16]=IP(c)
for 1<=i<=16
{
R[i-1]=L[i]
L[i-1]=R[i]XOR f(L[i],K[i])
}
x=IP-1(L[0]R[0])源码
public static string DESEncrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
} ///DES解密
public static string DESDecrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
} des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
try
{
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder(); }
catch {
MessageBox.Show("溢出,解密有误");
return "";
}
return System.Text.Encoding.Default.GetString(ms.ToArray());
} /// <summary>
// TripleDES解密
/// </summary>
public static string TripleDESDecrypt(string encryptedString, string key)
{
if (encryptedString.Equals(string.Empty))
{
return encryptedString;
}
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); //将秘钥编码成为二进制数组
PasswordDeriveBytes derive = new PasswordDeriveBytes(key, null);
byte[] tdesIV = new byte[8];
byte[] btaKey = derive.CryptDeriveKey("TripleDES", "SHA1", 0, tdesIV); //从字符串转换为字节组
byte[] btaCode = System.Convert.FromBase64String(encryptedString); tdes.Mode = CipherMode.ECB;
MemoryStream ms = new MemoryStream(btaCode); CryptoStream encStream = new CryptoStream(ms, tdes.CreateDecryptor(btaKey, tdesIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(encStream, System.Text.Encoding.Unicode);
string strtmp = sr.ReadToEnd();
sr.Close();
encStream.Close(); return strtmp;
} /// <summary>
/// TripleDES加密
/// </summary>
public static string TripleDESEcrypt(string plainString, string key)
{
if (plainString.Equals(string.Empty))
{
return plainString;
} TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); //将秘钥编码成为二进制数组
PasswordDeriveBytes derive = new PasswordDeriveBytes(key, null);
byte[] tdesIV = new byte[8];
byte[] btaKey = derive.CryptDeriveKey("TripleDES", "SHA1", 0, tdesIV); //从字符串转换为字节组
byte[] btaCode = Encoding.Unicode.GetBytes(plainString); tdes.Mode = CipherMode.ECB;
MemoryStream ms = new MemoryStream(); CryptoStream encStream = new CryptoStream(ms, tdes.CreateEncryptor(btaKey, tdesIV), CryptoStreamMode.Write);
encStream.Write(btaCode, 0, btaCode.Length);
encStream.FlushFinalBlock();
encStream.Close(); //再转换为一个字符串
return System.Convert.ToBase64String(ms.ToArray());
}

扩展阅读:c++编程 ... python编程入门自学 ... c# async ... c#是什么 ... ∑求和公式 ... c#菜鸟教程 ... c#stringbuilder方法 ... 为什么dxdy=ρdρdθ ... c#编写计算器 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网