Java/.NET interop for DES3

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Java/.NET interop for DES3

Scott Walters
Hi,

If it's possible, I need to be able to encrypt with Java & decrypt with .NET
and vice versa using DES3. I've written code but the problem is that the
encrypted data in .NET is longer than in Java.  I've taken pains to try and
set them up to be exactly the same but I get 24 bytes in .NET and only 16
bytes in Java.  Here's the code I'm using.

Thanks,
Scott


Java

Security.addProvider(new BouncyCastleProvider());
BlockCipher engine = new DESedeEngine();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new
CBCBlockCipher(engine));
byte key[] =
{112,-89,-110,37,21,-44,59,-91,-16,49,-115,-47,-122,-76,-83,29,81,46,122,-95,71,-119,-28,16};
KeyParameter keyParam = new KeyParameter(key);
byte iv[] = {-7,-95,-125,-111,95,-33,-59,-73};
CipherParameters cph = new ParametersWithIV(keyParam, iv);
cipher.init(true, cph);
String plainStr = "encrypt this string";
byte plainBytes[] = plainStr.getBytes();
byte encout[] = new byte[256];
int outCnt = cipher.processBytes(plainBytes, 0, plainBytes.length, encout,
0);


c#

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
byte []keydata = new
byte[]{112,167,146,37,21,212,59,165,240,49,141,209,134,180,173,29,81,46,122,161,71,137,228,16};
byte []ivdata = new byte[]{249,161,131,145,95,223,197,183};

tdes.Key = keydata;
tdes.IV = ivdata;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
byte []plainBuf = Encoding.UTF8.GetBytes("encrypt this string");

MemoryStream outStream = new MemoryStream();
CryptoStream encStream = new CryptoStream(outStream,
                tdes.CreateEncryptor(keydata, ivdata), CryptoStreamMode.Write);
encStream.Write(plainBuf, 0, plainBuf.Length);
encStream.FlushFinalBlock();

outStream.Position = 0;
byte []encbuf = new byte[outStream.Length];
outStream.Read(encbuf, 0, (int)outStream.Length);
encStream.Close();

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


Reply | Threaded
Open this post in threaded view
|

Re: Java/.NET interop for DES3

Megan Woods-2
Hi Scott,

Are you collecting the data generated by the BlockCipher doFinal()
method on the java side.?

Cheers

Megan Woods




Scott Walters wrote:

> Hi,
>
> If it's possible, I need to be able to encrypt with Java & decrypt
> with .NET and vice versa using DES3. I've written code but the problem
> is that the encrypted data in .NET is longer than in Java.  I've taken
> pains to try and set them up to be exactly the same but I get 24 bytes
> in .NET and only 16 bytes in Java.  Here's the code I'm using.
>
> Thanks,
> Scott
>
>
> Java
>
> Security.addProvider(new BouncyCastleProvider());
> BlockCipher engine = new DESedeEngine();
> BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new
> CBCBlockCipher(engine));
> byte key[] =
> {112,-89,-110,37,21,-44,59,-91,-16,49,-115,-47,-122,-76,-83,29,81,46,122,-95,71,-119,-28,16};
>
> KeyParameter keyParam = new KeyParameter(key);
> byte iv[] = {-7,-95,-125,-111,95,-33,-59,-73};
> CipherParameters cph = new ParametersWithIV(keyParam, iv);
> cipher.init(true, cph);
> String plainStr = "encrypt this string";
> byte plainBytes[] = plainStr.getBytes();
> byte encout[] = new byte[256];
> int outCnt = cipher.processBytes(plainBytes, 0, plainBytes.length,
> encout, 0);
>
>
> c#
>
> TripleDESCryptoServiceProvider tdes = new
> TripleDESCryptoServiceProvider();
> byte []keydata = new
> byte[]{112,167,146,37,21,212,59,165,240,49,141,209,134,180,173,29,81,46,122,161,71,137,228,16};
>
> byte []ivdata = new byte[]{249,161,131,145,95,223,197,183};
>
> tdes.Key = keydata;
> tdes.IV = ivdata;
> tdes.Mode = CipherMode.CBC;
> tdes.Padding = PaddingMode.PKCS7;
> byte []plainBuf = Encoding.UTF8.GetBytes("encrypt this string");
>
> MemoryStream outStream = new MemoryStream();
> CryptoStream encStream = new CryptoStream(outStream,
>         tdes.CreateEncryptor(keydata, ivdata), CryptoStreamMode.Write);
> encStream.Write(plainBuf, 0, plainBuf.Length);
> encStream.FlushFinalBlock();
>
> outStream.Position = 0;
> byte []encbuf = new byte[outStream.Length];
> outStream.Read(encbuf, 0, (int)outStream.Length);
> encStream.Close();
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today - it's
> FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
>
>
>


--

http://www.widestreet.com.au/blog