Java中Desede 解密 对应C# Des 解密方法

最近在项目中在接口中使用了Java中的Desede加密,

Java中的解密代码为:


// 解密

public static String decrypt(String src, String key) {
try {
// --通过 base64,将字符串转成 byte 数组
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytesrc = decoder.decodeBuffer(src);
// --解密的 key
DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey securekey = keyFactory.generateSecret(dks);
// --Chipher 对象解密
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, securekey);
byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

网上查了一下DESede是简写,它与DESede/ECB/PKCS5Padding等价

对应c#中的ECB,PKCS7模式,C#对应的解密代码在网上找了一下,这个亲测可用:

            

public static string DESedeDecrypt(string data, string key)
            {
                SymmetricAlgorithm syag = new TripleDESCryptoServiceProvider();
                syag.Mode = CipherMode.ECB;
                syag.Padding = PaddingMode.PKCS7;
                syag.Key = Encoding.UTF8.GetBytes(key);
               // mCSP.IV = Encoding.UTF8.GetBytes(IV); ECB模式下IV无效,不用设置 
                byte[] byt;
                ICryptoTransform ct = syag.CreateDecryptor(syag.Key, syag.IV);
                byt = Convert.FromBase64String(data);
                using (MemoryStream ms = new MemoryStream())
                {
                    CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                    cs.Write(byt, 0, byt.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值