Hi

I have a .net client downloading encrypted data from a java server.
Decryption of the data works fine in java clients, .net crypto api but not
with bouncycastle .net.

I have a server which generates encrypted data with this java code using
bouncycastle 1.52:
AlgorithmIdentifier hash = new
AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256, DERNull.INSTANCE);
AlgorithmIdentifier mask = new
AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, hash);
AlgorithmIdentifier pSource = new
AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new
DEROctetString(new byte[0]));
RSAESOAEPparams parameters = new RSAESOAEPparams(hash, mask, pSource);
JceKeyTransRecipientInfoGenerator e = (new
JceKeyTransRecipientInfoGenerator(sertifikat, new
AlgorithmIdentifier(PKCSObjectIdentifiers.id_RSAES_OAEP,
parameters))).setProvider(p);
CMSEnvelopedDataGenerator envelopedDataGenerator = new
CMSEnvelopedDataGenerator();
envelopedDataGenerator.addRecipientInfoGenerator(e);
OutputEncryptor contentEncryptor = (new
JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC)).build();
CMSEnvelopedData cmsData = envelopedDataGenerator.generate(new
CMSProcessableByteArray(bytes), contentEncryptor);
return cmsData.getEncoded();

I return this to a .net client which tries to decrypt it. It works with
this code using .net Crypto api:
var envelopedCms = new EnvelopedCms();
envelopedCms.Decode(encrypteddata);
envelopedCms.Decrypt(envelopedCms.RecipientInfos[0]);
return envelopedCms.ContentInfo.Content;

But when i try to use bouncycastle 1.8.1 it fails with data hash wrong
exception:
CmsEnvelopedDataParser cmsEnvelopedDataParser = new
CmsEnvelopedDataParser(kryptertData);
RecipientInformationStore recipientInformationStore =
cmsEnvelopedDataParser.GetRecipientInfos();
IEnumerator enumerator =
recipientInformationStore.GetRecipients().GetEnumerator();
enumerator.MoveNext();
RecipientInformation recipientInformation = enumerator.Current as
RecipientInformation;
return recipientInformation.GetContent(privateKey);

Result StackTrace:
at Org.BouncyCastle.Crypto.Encodings.OaepEncoding.decodeBlock(Byte[]
inBytes, Int32 inOff, Int32 inLen) in
C:\BouncyCastle\crypto\src\crypto\encodings\OAEPEncoding.cs:line 256
   at Org.BouncyCastle.Crypto.Encodings.OaepEncoding.ProcessBlock(Byte[]
inBytes, Int32 inOff, Int32 inLen) in
C:\BouncyCastle\crypto\src\crypto\encodings\OAEPEncoding.cs:line 131
   at Org.BouncyCastle.Crypto.BufferedAsymmetricBlockCipher.DoFinal() in
C:\BouncyCastle\crypto\src\crypto\BufferedAsymmetricBlockCipher.cs:line 124
   at Org.BouncyCastle.Crypto.BufferedAsymmetricBlockCipher.DoFinal(Byte[]
input, Int32 inOff, Int32 length) in
C:\BouncyCastle\crypto\src\crypto\BufferedAsymmetricBlockCipher.cs:line 139
   at
Org.BouncyCastle.Security.WrapperUtilities.BufferedCipherWrapper.Unwrap(Byte[]
input, Int32 inOff, Int32 length) in
C:\BouncyCastle\crypto\src\security\WrapperUtilities.cs:line 149
   at
Org.BouncyCastle.Cms.KeyTransRecipientInformation.UnwrapKey(ICipherParameters
key) in C:\BouncyCastle\crypto\src\cms\KeyTransRecipientInformation.cs:line
76
 --- End of inner exception stack trace ---
    at
Org.BouncyCastle.Cms.KeyTransRecipientInformation.UnwrapKey(ICipherParameters
key) in C:\BouncyCastle\crypto\src\cms\KeyTransRecipientInformation.cs:line
98
   at
Org.BouncyCastle.Cms.KeyTransRecipientInformation.GetContentStream(ICipherParameters
key) in C:\BouncyCastle\crypto\src\cms\KeyTransRecipientInformation.cs:line
108
   at
Org.BouncyCastle.Cms.RecipientInformation.GetContent(ICipherParameters key)
in C:\BouncyCastle\crypto\src\cms\RecipientInformation.cs:line 96
Result Message:
Test method
ForsendelseClientSample.SvarinnEksempel.TestDekrypteringAvNedlastetFil
threw exception:
Org.BouncyCastle.Cms.CmsException: bad padding in message. --->
Org.BouncyCastle.Crypto.InvalidCipherTextException: data hash wrong

Anyone have any ideas why this don't work with bouncycastle in .net?

--
Idar

-- 
Idar Borlaug

Reply via email to