我正在使用「AES/CBC/PKCS5」爲Android和Blackberry寫入加密代碼,如下所示。 機器人:AES加密問題
byte[] encoded = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03,
(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03};
SecretKeySpec secretKeySpec = new SecretKeySpec(encoded, "AES");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");//AES/CBC/PKCS5Padding
System.out.println("swapnil:"+c.getAlgorithm()+" BlockSize:"+c.getBlockSize());
c.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] input = "Hello".getBytes();
byte[] output = c.doFinal(input);
System.out.println("Swapnil: " + new String(output));
對於黑莓:
byte[] key1 = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03,
(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
InitializationVector iv = new InitializationVector(IV_BYTE);
//SymmetricKey key = new SymmetricKeyFactory("AES_256", key1, 0, key1.length);
AESKey aesKey = new AESKey(key1);
EncryptorOutputStream os = EncryptorFactory.getEncryptorOutputStream(aesKey, baos, "AES/CBC/PKCS5", iv);///
System.out.println("Swapnil"+os.getAlgorithm());
os.write("Hello".getBytes());
os.close();
byte[] encryptedData = baos.toByteArray();
String string = new String(encryptedData);
LabelField lblField = new LabelField(string);
add(lblField);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CryptoTokenException e) {
e.printStackTrace();
} catch (CryptoUnsupportedOperationException e) {
e.printStackTrace();
} catch (CryptoException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我的問題是在Android中當我運行該代碼,即使具有相同的密鑰後,它生成不同的加密數據的每個I運行該代碼的時間。在黑莓手機中,每當我運行代碼時,它都會生成相同的加密數據。我認爲Android應該也是這樣,但不知何故它不起作用。
我該如何解決這個問題?
你試試下面的答案嗎? – Nikhil