日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

java des加密解密_JAVA和c# 之间数据通讯时通过DES进行加密解密

發布時間:2025/3/21 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java des加密解密_JAVA和c# 之间数据通讯时通过DES进行加密解密 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c#開發前端時調用java開發的接口,需要數據加密后通訊。兩邊都需要同時通過DES(BASE64)進行加解密。

代碼如下

c#端:

加密

public static String Encrypt(String toEncrypt, String key)?? ? ? ?{? ? ? ? ? ?Byte[] _Key = Encoding.ASCII.GetBytes(key);? ? ? ? ? ?Byte[] _Source = Encoding.UTF8.GetBytes(toEncrypt); ? ? ? ? ? ?DES des = DES.Create("DES");? ? ? ? ? ?des.Mode = CipherMode.ECB;? ? ? ? ? ?des.Padding = PaddingMode.PKCS7;? ? ? ? ? ?des.Key = _Key;? ? ? ? ? ?ICryptoTransform cTransform = des.CreateEncryptor();? ? ? ? ? ?Byte[] cryptData = cTransform.TransformFinalBlock(_Source, 0, _Source.Length); ? ? ? ? ? ?String CryptString = Convert.ToBase64String(cryptData);? ? ? ? ? ?return CryptString; }

解密

public static String Decrypt(string encryptedSource, string key)? ? ? ?{? ? ? ? ? ?Byte[] _Key = Encoding.ASCII.GetBytes(key);? ? ? ? ? ?DES des = DES.Create("DES");? ? ? ? ? ?des.Mode = CipherMode.ECB;? ? ? ? ? ?des.Padding = PaddingMode.PKCS7;? ? ? ? ? ?des.Key = _Key;? ? ? ? ? ?ICryptoTransform cTransform = des.CreateDecryptor();? ? ? ? ? ?Byte[] encryptedData = Convert.FromBase64String(encryptedSource);? ? ? ? ? ?Byte[] originalSrouceData = cTransform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);? ? ? ? ? ?String originalString = Encoding.UTF8.GetString(originalSrouceData);? ? ? ? ? ?return originalString;? ? ? ?}

Java端:

加密

public static String encrypt(String data,String key) throws Exception { byte[] bt = encrypt(data.getBytes(), key.getBytes()); String strs = (new BASE64Encoder()).encode(bt); return strs;}private static byte[] encrypt(byte[] data, byte[] key) throws Exception { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); cipher.init(1, securekey, sr); return cipher.doFinal(data);}

解密:

public static String decrypt(String data,String key) throws IOException, Exception { if (data == null) { return null; } else { BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, key.getBytes()); return new String(bt); }}private static byte[] decrypt(byte[] data, byte[] key) throws Exception { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); cipher.init(2, securekey, sr); return cipher.doFinal(data);}

總結

以上是生活随笔為你收集整理的java des加密解密_JAVA和c# 之间数据通讯时通过DES进行加密解密的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。