views:

116

answers:

2

I am attempting to find some code to use in VB.NET to decrypt a string that has been encrypted using the Java crypto libary.

All of the code I have found so far uses an Initialisation Vector and Key to decrypt but the string has been encrypted using the Key only. (Using an Initialisation Vector is apperently an optional step in Java).

Does anyone know of code that I can use (preferably in VB.NET but i could convert C#) to decrypt AES 128 Bit encoded string without an Initialisation Vector?

Many thanks

Steve

A: 

This is c# syntax, but the classes should all be the same for VB.net. You would need to know the padding scheme (if any) and the cipher mode used on the encryption routines. Its a fair bet that if an IV isn't being used, then its using ECB mode.

Its also important to get the encodings correct when building the byte arrays containing keys and encrypted data. It may be ASCII, Unicode, UTF...

using System.Security.Cryptography;
using System.IO;

byte[] encryptedBytes = new byte[16]; // multiple of 16 (blocksize is 128 bits)
byte[] keyBytes = new byte[16]; // if keysize is 128 bits

Rijndael rijndael = Rijndael.Create();
rijndael.Mode = CipherMode.ECB; // But try other modes
rijndael.Padding = PaddingMode.None; // But try other padding schemes
rijndael.BlockSize = 128;
rijndael.KeySize = 128;
rijndael.Key = keyBytes;
ICryptoTransform cryptoTransform = rijndael.CreateDecryptor();

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write);

// Write the data to the stream to perform the decryption
cs.Write(encryptedBytes, 0, encryptedBytes.Length);

// Close the crypto stream to apply any padding.
cs.Close();

// Now get the decrypted data from the MemoryStream.
byte[] decryptedBytes = ms.ToArray();
PaulG
A: 

VERY HELPFUL!!

Chris Parsons