找回密码
 注册
关于网站域名变更的通知
查看: 678|回复: 3
打印 上一主题 下一主题

.NET Core 关于 DES加密 解密 Windows的加密数据和Linux不一致。

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2021-7-28 09:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
1 G! b" [9 H+ y6 {/ Y; G
.NET Core 关于 DES加密 解密 Windows的加密数据和Linux不一致。
) f" T; q/ L* e0 z9 ^- L
$ L1 ?' s0 P2 q9 J代码部分# \* Y3 I8 T8 ~2 A* U5 I

& Q1 S( q# C: k. ?# T
  • //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;
  •         }
  • }
    " O! X. ]; w6 j3 J0 {. y
$ S1 M* w. y3 t3 M* |% x

( v3 A0 l' U1 [0 P9 ]被加密文字:admin8 c! G* s4 j8 x& i/ j& m, P) n
Windows得到的密文为:mpeF1AIykkU=" M( ]7 b7 e& y$ k' J
Linux得到的密文为:QDI3HuQF+SQ=

该用户从未签到

2#
发表于 2021-7-28 10:57 | 只看该作者
现在搞啥东西都有加密,难为那些工程师了
  • TA的每日心情

    2019-11-19 15:32
  • 签到天数: 1 天

    [LV.1]初来乍到

    3#
    发表于 2021-7-28 11:02 | 只看该作者
    swEncrypt.WriteLine(plain); 的问题
    , U( j  g2 g1 f+ z* N  X4 `Windows 用 \r\n 换行9 ^# y; c* f6 U) R$ a# w( ^! T' Z. U
    Linux 用 \n 换行

    该用户从未签到

    4#
    发表于 2021-7-28 11:04 | 只看该作者
    这个代码是不是可以用在不同平台上啊
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-11-24 02:54 , Processed in 0.140625 second(s), 23 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表