• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

python和C#DES加密

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
C# code:

using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace LamicSdk.Func { public class HelpEncrypt { private static string GetDefaultKeyAndIv() { return ""; } /// <summary>字符串DES加密函数</summary> /// <param name="str">被加密字符串</param> /// <param name="keyAndIv">加密向量和密钥</param> /// <returns>加密后字符串</returns> public static string Encode(string str, string keyAndIv = null) { try { keyAndIv = string.IsNullOrEmpty(keyAndIv) ? GetDefaultKeyAndIv() : keyAndIv; var provider = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes(keyAndIv), IV = Encoding.ASCII.GetBytes(keyAndIv) }; var bytes = Encoding.UTF8.GetBytes(str); using (var memoryStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(memoryStream, provider.CreateEncryptor(), CryptoStreamMode.Write)) { cryptoStream.Write(bytes, 0, bytes.Length); cryptoStream.FlushFinalBlock(); var builder = new StringBuilder(); foreach (var num in memoryStream.ToArray()) { builder.AppendFormat("{0:X2}", num); } return builder.ToString(); } } } catch (Exception) { return null; } } /// <summary><![CDATA[字符串DES解密函数]]></summary> /// <param name="str"><![CDATA[被解密字符串 ]]></param> /// <param name="keyAndIv">解密向量和密钥</param> /// <returns><![CDATA[解密后字符串]]></returns> public static string Decode(string str, string keyAndIv = null) { try { keyAndIv = string.IsNullOrEmpty(keyAndIv) ? GetDefaultKeyAndIv() : keyAndIv; var provider = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes(keyAndIv), IV = Encoding.ASCII.GetBytes(keyAndIv) }; var buffer = new byte[str.Length / 2]; for (var i = 0; i < str.Length / 2; i++) { var num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10); buffer[i] = (byte)num2; } using (var memoryStream = new MemoryStream()) { using (var cryptoStream =new CryptoStream(memoryStream, provider.CreateDecryptor(), CryptoStreamMode.Write)) { cryptoStream.Write(buffer, 0, buffer.Length); cryptoStream.FlushFinalBlock(); return Encoding.UTF8.GetString(memoryStream.ToArray()); } } } catch (Exception) { return null; } } } }


python code:
import pyDes
import binascii

#des 加密
k = pyDes.des("", pyDes.CBC,"", pad=None, padmode=pyDes.PAD_PKCS5)
d = k.encrypt("22222222222".encode('utf-8'))
result= binascii.b2a_hex(d)
print(result)

 

 

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
(转)C#调用DLL时参数问题的一点心得发布时间:2022-07-13
下一篇:
UnityC#ref与out发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap