已确定加密后的值为: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# 解出结果。谢谢。可追加
展开