我正在为 Android 和 Blackberry 编写加密代码,使用“AES/CBC/PKCS5”,如下所示。安卓:
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中,当我运行代码时,即使拥有相同的密钥,每次运行代码时它都会生成不同的加密数据。在黑莓中,每当我运行代码时,它都会生成相同的加密数据。我认为Android应该做同样的事情,但不知何故它不起作用。
我怎样才能解决这个问题?