1个回答
2013-09-05
展开全部
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());
}
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());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询