2010-10-14 68 views
0

我試圖找到一些代碼在VB.NET中使用解密已使用Java加密庫進行加密的字符串。解密Java AES 128Bit加密的字符串,在VB.NET中只使用密鑰加密(不是IV)

到目前爲止,我發現的所有代碼都使用初始化向量和密鑰來解密,但字符串僅使用密鑰進行了加密。 (使用初始化矢量在Java中是一個可選的步驟)。

有沒有人知道的代碼,我可以使用(最好在VB.NET中,但我可以轉換C#)解密AES 128位編碼的字符串沒有初始化矢量?

非常感謝

史蒂夫

回答

0

這是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(); 
0

這是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; 
    } 
    }