c# des 16位密钥和16数据的加密解密,急求................... 20
求c#的des16位密钥及16位加密数据的加密解密方法。密钥和加密数据都是16位的,加密后的结果要求也要16位....
求c#的des 16位密钥及16位加密数据的加密解密方法。密钥和加密数据都是16位的,加密后的结果要求也要16位.
展开
2个回答
展开全部
/// <summary>
/// 加密原函数
/// </summary>
/// <param name="str_in_data">加密原串</param>
/// <param name="str_DES_KEY">密钥</param>
/// <returns>加密串</returns>
public static string Encrypt_DES16(string str_in_data, string str_DES_KEY)
{
try
{
byte[] shuju = new byte[8];
byte[] keys = new byte[8];
for (int i = 0; i < 8; i++)
{
shuju[i] = Convert.ToByte(str_in_data.Substring(i * 2, 2), 16);
keys[i] = Convert.ToByte(str_DES_KEY.Substring(i * 2, 2), 16);
}
DES desEncrypt = new DESCryptoServiceProvider();
desEncrypt.Mode = CipherMode.ECB;
//desEncrypt.Key = ASCIIEncoding.ASCII.GetBytes(str_DES_KEY);
desEncrypt.Key = keys;
byte[] Buffer;
Buffer = shuju;//ASCIIEncoding.ASCII.GetBytes(str_in_data);
ICryptoTransform transForm = desEncrypt.CreateEncryptor();
byte[] R;
R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
string return_str = "";
foreach (byte b in R)
{
return_str += b.ToString("X2");
}
return_str = return_str.Substring(0, 16);
return return_str;
}
catch (Exception e)
{
throw e;
}
}
/// 加密原函数
/// </summary>
/// <param name="str_in_data">加密原串</param>
/// <param name="str_DES_KEY">密钥</param>
/// <returns>加密串</returns>
public static string Encrypt_DES16(string str_in_data, string str_DES_KEY)
{
try
{
byte[] shuju = new byte[8];
byte[] keys = new byte[8];
for (int i = 0; i < 8; i++)
{
shuju[i] = Convert.ToByte(str_in_data.Substring(i * 2, 2), 16);
keys[i] = Convert.ToByte(str_DES_KEY.Substring(i * 2, 2), 16);
}
DES desEncrypt = new DESCryptoServiceProvider();
desEncrypt.Mode = CipherMode.ECB;
//desEncrypt.Key = ASCIIEncoding.ASCII.GetBytes(str_DES_KEY);
desEncrypt.Key = keys;
byte[] Buffer;
Buffer = shuju;//ASCIIEncoding.ASCII.GetBytes(str_in_data);
ICryptoTransform transForm = desEncrypt.CreateEncryptor();
byte[] R;
R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
string return_str = "";
foreach (byte b in R)
{
return_str += b.ToString("X2");
}
return_str = return_str.Substring(0, 16);
return return_str;
}
catch (Exception e)
{
throw e;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public byte[] desEncrypt(byte[] PlainText, byte[] key)
{
byte[] buf=null;
MemoryStream ms = new MemoryStream();
DESCryptoServiceProvider mDES = new DESCryptoServiceProvider();
//mDES.Mode = CipherMode.ECB;
mDES.Key = key;
CryptoStream encStream = new CryptoStream(ms, mDES.CreateEncryptor(), CryptoStreamMode.Write);
BinaryWriter bw = new BinaryWriter(encStream);
bw.Write(PlainText);
bw.Close();
encStream.Close();
buf = ms.ToArray();
ms.Close();
return buf;
}
{
byte[] buf=null;
MemoryStream ms = new MemoryStream();
DESCryptoServiceProvider mDES = new DESCryptoServiceProvider();
//mDES.Mode = CipherMode.ECB;
mDES.Key = key;
CryptoStream encStream = new CryptoStream(ms, mDES.CreateEncryptor(), CryptoStreamMode.Write);
BinaryWriter bw = new BinaryWriter(encStream);
bw.Write(PlainText);
bw.Close();
encStream.Close();
buf = ms.ToArray();
ms.Close();
return buf;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询