解密Java AES 128Bit加密的字符串,在VB.NET中只使用密钥加密(不是IV)
问题描述:
我试图找到一些代码在VB.NET中使用解密已使用Java加密库进行加密的字符串。解密Java AES 128Bit加密的字符串,在VB.NET中只使用密钥加密(不是IV)
到目前为止,我发现的所有代码都使用初始化向量和密钥来解密,但字符串仅使用密钥进行了加密。 (使用初始化矢量在Java中是一个可选的步骤)。
有没有人知道的代码,我可以使用(最好在VB.NET中,但我可以转换C#)解密AES 128位编码的字符串没有初始化矢量?
非常感谢
史蒂夫
答
这是C#语法,但是类都应该为VB.net相同。您需要知道填充方案(如果有)以及加密例程上使用的密码方式。公平地说,如果IV没有被使用,那么它使用ECB模式。
当构建包含密钥和加密数据的字节数组时,获得编码正确性也很重要。它可能是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();
答
这是Java端的源代码。通过传入字符串来解密Decrypt:
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;
}
}