Decrypt Java AES 128Bit encrypted string which has been encrypted using key only (no IV) in VB.NET
I am attempting to find some code to use in VB.NET to decrypt a string 开发者_StackOverflow中文版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
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();
Here's the source code for the Java side. Decrypt is called by passing in a string:
private static final byte[] __RawKey = {
(byte) 0x30, (byte) 0x31, (byte) 0x32,
(byte) 0x33, (byte) 0x34, (byte) 0x35,
(byte) 0x36, (byte) 0x37
};
private String decrypt(String data) throws Exception {
try {
Key key = new SecretKeySpec(__RawKey, 0, __RawKey.length, "DES");
byte[] _encrypted = data.getBytes();
String sKey = new String(__RawKey);
System.out.println(sKey);
System.out.println(sKey.length());
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding", "SunJCE");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] _decrypted = cipher.doFinal(_encrypted);
System.out.println("Decrypted: " + new String(_decrypted));
return new String(_decrypted);
}
catch (Exception e) {
System.out.println(e);
return null;
}
}
精彩评论