Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
chackett
Posts: 29
Registered: ‎12-16-2011
My Device: Bold 9900
My Carrier: Meteor - Ireland
Accepted Solution

C#.NET Compatible AES encryption.

Hi There,

 

I am using the Basic Encryption Tutorial to implement AES encryption in my application.

 

My problem is that I cannot seem to decrypt it in my C#.NET application. I have two concerns, not sure which is the problem or both are.

 

1) In the tutorial there is no use of an Initialization Vector. I am using the AesCryptoServiceProvider from .NET that seems to require an IV to decrypt. I have tried setting the IV in .NET to all zeros but the decrypted message is garbage.

 

2) If I get past the IV problem then .NET complains about bad padding and how it can't be removed. I have tried using the PKCS5 padding in BB and PKCS7 in .NET. I have read they are the same. Maybe they are the same and the IV is upsetting the padding or else they are not the same.

 

Anyway, has anybody successfully implemented AES with cross compatibility to .NET?

 

Please note the supported padding enginges in .NET are { ANSIX923, ISO10126, None, PKCS7, Zeros }

 

Any ideas greatly appreciated. Thanks.

-----------------------------------------------------------------

My Blackberry Blog - Coming Soon
ConorHackett.com - My Personal Site
Please use plain text.
Developer
chackett
Posts: 29
Registered: ‎12-16-2011
My Device: Bold 9900
My Carrier: Meteor - Ireland

Re: C#.NET Compatible AES encryption.

Ok, after spending all of yesterday on this I have just come up with a solution :smileyhappy:

 

The problem was a minor overlooked property in my .NET code. In the AesCryptoServiceProvider class there is a property called Mode which needs to be set to CipherMode.ECB for it to be able to decrypt the BlackBerry generated cipher.

 

Some Notes

Padding seems to default to it but if there is a problem set it to PaddingMode.PKCS7 this is the equivalent to the PKCS5 padding generated by BlackBerry (see PKCS5FormatterEngine)

 

The AesCryptoServiceProvider insists that it has a valid InitializationVector set for decryption purposes but it does not use it. I am able to decrypt my cipher regardless of what the IV is set to.

 

I hope I have been clear about my solution here. And also hope it helps somebody else, In my experience with working with .NET and BlackBerry crypto libraries there are many properties on both sides that can make or break the process while they are not documents. I recommend to thoroughly read all API docs of any classes used to discover these properties which will help with troubleshooting.

 

 

 

-----------------------------------------------------------------

My Blackberry Blog - Coming Soon
ConorHackett.com - My Personal Site
Please use plain text.
Contributor
BeSeWe
Posts: 17
Registered: ‎06-05-2012
My Device: Bold 9900
My Carrier: T-Mobile Germany

Re: C#.NET Compatible AES encryption.

Hi,

 

i have the same Problem.

Is it possible that you give me a small sample of your solution?

 

Thanks in advance.

 

Benjamin

Please use plain text.
Developer
chackett
Posts: 29
Registered: ‎12-16-2011
My Device: Bold 9900
My Carrier: Meteor - Ireland

Re: C#.NET Compatible AES encryption.

Sure why not, but on one condition.. let me know if it works for you :smileyhappy:

 

So first up is generating the AES key on the BlackBerry:

 

https://gist.github.com/4114147

 

Then, encrypt data on the BlackBerry using the provided key:

 

https://gist.github.com/4114053

 

Finally, decrypt the encrypted message in .NET:

 

https://gist.github.com/4114090

 

Let me know if it helps or if you need more help. Please not that this implementation uses CBC mode, which is not very secure. This was for a college project but if you are implementing for a production system you will probably want to use ECB mode.

 

-----------------------------------------------------------------

My Blackberry Blog - Coming Soon
ConorHackett.com - My Personal Site
Please use plain text.
Contributor
BeSeWe
Posts: 17
Registered: ‎06-05-2012
My Device: Bold 9900
My Carrier: T-Mobile Germany

Re: C#.NET Compatible AES encryption.

Hi,

 

many thanks. No It work's. :Clap:

This was exactly what I'm searching for.

 

Thanks

 

Benjamin.

 

Please use plain text.