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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x

; i, z; @! @# |& V+ F! n+ K.NET Core 关于 DES加密 解密 Windows的加密数据和Linux不一致。* l4 O+ u5 o- e+ u6 \

. F! G3 }- `6 l7 p代码部分  H& O: c' ~# G2 A
/ M/ y' t7 T. P0 ]4 \/ F. M
  • //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;
  •         }
  • }
    6 L% h% e0 w; y% q; {- K1 Y' Y

$ x; d2 W) i6 @) V, N# p' N* V; G/ Z+ Y- X0 R
被加密文字:admin1 d7 R& |* w* P
Windows得到的密文为:mpeF1AIykkU=; ~/ t5 c& d' H9 |3 |
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); 的问题" r2 E$ K$ s) B# x6 C
    Windows 用 \r\n 换行
    , {8 {, b. G- P0 \+ z" C/ _0 eLinux 用 \n 换行

    该用户从未签到

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

    本版积分规则

    关闭

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

    EDA365公众号

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

    GMT+8, 2025-6-22 06:53 , Processed in 0.078125 second(s), 23 queries , Gzip On.

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

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

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