java des des3_Java的des3解密方法。转成c#

已确定加密后的值为:paramString="X+u22bc0D/tLPYSVaEIhmeupM/tzKt1cLWEPsGRdzWOqf9Id5vaa3h1DcTmEQSVF1FCJ6eQyw2F9n3qDYq8VLVDRU0amRpwB";java加密方法为:paramArrayOfByte1为keyp...

已确定加密后的值为:paramString="X+u22bc0D/tLPYSVaEIhmeupM/tzKt1cLWEPsGRdzWOqf9Id5vaa3h1DcTmEQSVF1FCJ6eQyw2F9n3qDYq8VLVDRU0amRpwB";

java加密方法为:

paramArrayOfByte1为 key

paramArrayOfByte2为iv

paramArrayOfByte3为 加密的数据

public static byte[] des3EncodeCBC(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, byte[] paramArrayOfByte3)

throws Exception

{

DESedeKeySpec localDESedeKeySpec = new DESedeKeySpec(paramArrayOfByte1);

SecretKey localSecretKey = SecretKeyFactory.getInstance("desede").generateSecret(localDESedeKeySpec);

Cipher localCipher = Cipher.getInstance("desede/CBC/PKCS5Padding");

localCipher.init(1, localSecretKey, new IvParameterSpec(paramArrayOfByte2));

return localCipher.doFinal(paramArrayOfByte3);

}

已知 java des3解密方法:

public static byte[] des3DecodeCBC(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, byte[] paramArrayOfByte3)

throws Exception

{

DESedeKeySpec localDESedeKeySpec = new DESedeKeySpec(paramArrayOfByte1);

SecretKey localSecretKey = SecretKeyFactory.getInstance("desede").generateSecret(localDESedeKeySpec);

Cipher localCipher = Cipher.getInstance("desede/CBC/PKCS5Padding");

localCipher.init(2, localSecretKey, new IvParameterSpec(paramArrayOfByte2));

return localCipher.doFinal(paramArrayOfByte3);

}

解密调用:

return new String(des3DecodeCBC("YWJj1ZSyw2hpamtsbW5vcHFyc3R1dnd4".getBytes(), "25439768".getBytes(), Base64.decode(paramString, 0)), "UTF-8");

现在想用C# 解密 方法:

///

/// DES3 CBC模式解密

///

/// 密钥

/// IV

/// 密文的byte数组

/// 明文的byte数组

public byte[] Des3DecodeCBC(byte[] key, byte[] iv, byte[] data)

{

try

{

MemoryStream msDecrypt = new MemoryStream(data);

TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();

tdsp.Mode = CipherMode.CBC;

tdsp.Padding = PaddingMode.PKCS7;

CryptoStream csDecrypt = new CryptoStream(msDecrypt,

tdsp.CreateDecryptor(key, iv),

CryptoStreamMode.Read);

byte[] fromEncrypt = new byte[data.Length];

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the buffer into a string and return it.

return fromEncrypt;

}

catch (CryptographicException e) { return Encoding.UTF8.GetBytes(e.Message); }

}

调用:

byte[] key = Convert.FromBase64String("YWJj1ZSyw2hpamtsbW5vcHFyc3R1dnd4");

byte[] iv = utf8.GetBytes("25439768");

byte[] val = Des3DecodeCBC(key, iv,Convert.FromBase64String(paramString));

但是这样的话key变为32位。提示“指定键的大小对于此算法无效”

解不出,有能帮忙看看的吗

调用参数这个地方java里使用的是.getBytes(),

C#如果使用System.Text.Encoding.UTF8.GetBytes("YWJj1ZSyw2hpamtsbW5vcHFyc3R1dnd4");

得出32位的byte[] 会提示指定键的大小对于此算法无效

请帮忙使用C# 解出结果。谢谢。可追加

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值