java中的triple_无法在使用Java加密的.NET中使用TripleDES进行解密

我们有一个Java客户端向我们发送一些加密数据

1.随机字符串使用RSA和我们提供的离线公钥加密。

2.使用步骤1中生成的密钥,使用alg_tripleDES_CBC = http://www.w3.org/2001/04/xmlenc#tripledes-cbc加密数据

我能够像第一步一样解密密钥......这正在工作。

public static string DecryptKey(string encryptedKey)

{

X509Certificate2 cert = new X509Certificate2("c:\\test.pfx", "test");

RSACryptoServiceProvider privateKeyProvider = (RSACryptoServiceProvider)cert.PrivateKey;

string decryptedKey = System.Text.Encoding.UTF8.GetString(privateKeyProvider.Decrypt(Convert.FromBase64String(encryptedKey), false));

return decryptedKey;

}我有这个代码使用第一步生成的密钥来解密数据。

public static string DecryptString(string Message, string Passphrase)

{

byte[] Results;

System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();

byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

TDESAlgorithm.Key = TDESKey;

TDESAlgorithm.Mode = CipherMode.ECB;

TDESAlgorithm.Padding = PaddingMode.PKCS7;

byte[] DataToDecrypt = Convert.FromBase64String(Message);

try

{

ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();

Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);

}

finally

{

TDESAlgorithm.Clear();

HashProvider.Clear();

}

return UTF8.GetString(Results);

}第二步失败,这个例外。

System.Security.Cryptography.CryptographicException: Bad Data.

at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)

at System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone)

at System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)

at ConsoleApplication3.Program.DecryptString(String Message, String Passphrase) in C:\Documents and Settings\rjaladi\Desktop\ConsoleApplication3\ConsoleApplication3\Program.cs:line 66

at ConsoleApplication3.Program.Main(String[] args) in C:\Documents and Settings\rjaladi\Desktop\ConsoleApplication3\ConsoleApplication3\Program.cs:line 22我需要检查我们的客户是什么?我知道我们传递给TDES的参数有问题。任何帮助?

编辑:加密邮件的相应Java代码。

public String encryptText(String plainText) throws Exception{

byte[] plaintext = plainText.getBytes();

byte[] tdesKeyData = key;

byte[] myIV = initializationVector;

Cipher c3des = Cipher.getInstance(""DESede/CBC/NoPadding"");

SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede");

IvParameterSpec ivspec = new IvParameterSpec(myIV);

c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);

byte[] cipherText = c3des.doFinal(plaintext);

sun.misc.BASE64Encoder obj64=new sun.misc.BASE64Encoder();

return obj64.encode(cipherText);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值