0条评论
还没有人评论过~
//des 加密解密
public class CryptoHelper
{
private const string key = "66666des";
private const string iv = "des66666";
/// <summary>
/// 使用DES加密
/// </summary>
/// <param name="plain">明文</param>
/// <returns>返回密文</returns>
public static string DesEncode(string plain)
{
//把密钥转换成字节数组
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
//把向量转换成字节数组
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
//声明1个新的DES对象
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
//开辟一块内存流
MemoryStream msEncrypt = new MemoryStream();
//把内存流对象包装成加密流对象
CryptoStream csEncrypt = new CryptoStream(msEncrypt, des.CreateEncryptor(keyBytes, ivBytes), CryptoStreamMode.Write);
//把加密流对象包装成写入流对象
StreamWriter swEncrypt = new StreamWriter(csEncrypt);
//写入流对象写入明文
swEncrypt.WriteLine(plain);
//写入流关闭
swEncrypt.Close();
//加密流关闭
csEncrypt.Close();
//把内存流转换成字节数组,内存流现在已经是密文了
byte[] bytesCipher = msEncrypt.ToArray();
//内存流关闭
msEncrypt.Close();
//将字节数组转化成Base64字符串
return Convert.ToBase64String(bytesCipher);
}
/// <summary>
/// 使用DES解密
/// </summary>
/// <param name="cipher">密文</param>
/// <returns>返回密文</returns>
public static string DesDeCode(string cipher)
{
//将密文通过Base64位还原成字节数组
byte[] cipherByte = Convert.FromBase64String(cipher);
//把密钥转换成字节数组
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
//把向量转换成字节数组
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
//声明1个新的DES对象
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
//开辟一块内存流,并存放密文字节数组
MemoryStream msDecrypt = new MemoryStream(cipherByte);
//把内存流对象包装成解密流对象
CryptoStream csDecrypt = new CryptoStream(msDecrypt, des.CreateDecryptor(keyBytes, ivBytes), CryptoStreamMode.Read);
//把解密流对象包装成写入流对象
StreamReader srDecrypt = new StreamReader(csDecrypt);
//明文=读出流的读出内容
string strPlainText = srDecrypt.ReadLine();
//读出流关闭
srDecrypt.Close();
//解密流关闭
csDecrypt.Close();
//内存流关闭
msDecrypt.Close();
//返回明文
return strPlainText;
}
}
被加密文字:admin
Windows得到的密文为:mpeF1AIykkU=
Linux得到的密文为:QDI3HuQF+SQ=
swEncrypt.WriteLine(plain);
的问题
Windows 用 \r\n
换行
Linux 用 \n
换行
不同平台上都是这段代码?