C# DESede/CBC/PKCS5Padding
Java版
public static void main(String[] args) throws Exception { byte[] text = "test".getBytes(); //待加/解密的数据 //密钥数据 byte[] keyData = Base64.decode("Uqt3frmnmVQQgU7S4wUJnKBrQ0CypPii"); String algorithm = "DESede"; //算法名称 String fullAlg = algorithm + "/CBC/PKCS5Padding"; Cipher cipher1 = null; try { cipher1 = Cipher.getInstance(fullAlg); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } int blockSize = cipher1.getBlockSize(); byte[] iv = new byte[blockSize]; for (int i = 0; i < blockSize; ++i) { iv[i] = 0; } System.out.println("iv==>"+new String(iv)+"<=="); Cipher cipher = Cipher.getInstance(fullAlg); SecretKey secretKey = new SecretKeySpec(keyData, algorithm); IvParameterSpec ivSpec = new IvParameterSpec(iv); /**加密*/ cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec); byte[] cipherBytes = cipher.doFinal(text); String cipherString = Base64.encode(cipherBytes); System.out.println("加密后==>" + cipherString); /**解密*/ cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec); byte[] resultBytes = cipher.doFinal(Base64.decode(cipherString)); System.out.println("解密==>" + new String(resultBytes)); }
C#版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | /// <summary> /// 3des加密IV /// </summary> private static readonly byte [] TripleDesIV = { 0, 0, 0, 0, 0, 0, 0, 0 }; /// <summary> /// 3DES的cbc模式加密 /// </summary> /// <param name="text">待加密文本</param> /// <param name="key">密钥</param> /// <returns></returns> public static string TripleDesEncryptorCBC( string text, string key) { //密钥 byte [] keyBytes = GetTripleDesKeyByte(key); //加密字符处理 byte [] plainText = Encoding.UTF8.GetBytes(text); //加密 using ( var tripleDESCipher = new TripleDESCryptoServiceProvider()) { tripleDESCipher.Mode = CipherMode.CBC; tripleDESCipher.Padding = PaddingMode.PKCS7; tripleDESCipher.Key = keyBytes; tripleDESCipher.IV = TripleDesIV; using (ICryptoTransform transform = tripleDESCipher.CreateEncryptor()) { return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); } } } /// <summary> /// 3DES的cbc模式解密 /// </summary> /// <param name="text"></param> /// <param name="key"></param> /// <returns></returns> public static string TripleDesDecryptorCBC( string text, string key) { //密钥 byte [] keyBytes = GetTripleDesKeyByte(key); //加密字符处理 byte [] plainText = Convert.FromBase64String(text); //加密 using ( var tripleDESCipher = new TripleDESCryptoServiceProvider()) { tripleDESCipher.Mode = CipherMode.CBC; tripleDESCipher.Padding = PaddingMode.PKCS7; tripleDESCipher.Key = keyBytes; tripleDESCipher.IV = TripleDesIV; using (ICryptoTransform transform = tripleDESCipher.CreateDecryptor()) { return Encoding.UTF8.GetString(transform.TransformFinalBlock(plainText, 0, plainText.Length)); } } } /// <summary> /// 获取密钥数组 /// </summary> /// <param name="key">密钥</param> /// <returns></returns> public static byte [] GetTripleDesKeyByte( string key) { //密钥处理 byte [] pwdBytes = Convert.FromBase64String(key); byte [] keyBytes = new byte [24]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; Array.Copy(pwdBytes, keyBytes, len); return keyBytes; } |
效果图:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?