2016-03-21 55 views
0

我使用下面的代碼來創建一個128位AES加密,如何使用AES加密128位字符串創建包含128 0的

String initializer = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; 

byte[] initializer2 = new BigInteger(initializer, 2).toByteArray(); 

String encrypt = encrypt(binary_string_firsthalf, initializer2); 

public static String encrypt(String plainText, byte[] key) { 
    try { 
     SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); 
     Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.ENCRYPT_MODE, secretKey); 
     byte[] cipherText = cipher.doFinal(plainText.getBytes("UTF8")); 
     String encryptedString = new String(Base64.encode(cipherText)); 
     return encryptedString; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

但代碼返回一個例外,說無效AES密鑰長度。 如何使用128 0創建密碼。

回答

0
byte[] initializer2 = new byte[16]; 

是您需要初始化一個默認值爲0x00的字節數組。


不要使用靜態可預測密鑰,例如所有0x00字節用於實際加密。