2

我正在为 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应该做同样的事情,但不知何故它不起作用。

我怎样才能解决这个问题?

4

2 回答 2

3

我正在 Android 中开发 AES 加密,它对我来说工作正常。

public class AESEncrtptor {
    String strKey = "1234567890123456";
    byte[] byteKey;
    byte[] byteVector = new byte[] { 59, 12, (byte) 129, 77, 39, 119, 82, 6,
            23, 1, 55, 24, 12, (byte) 154, (byte) 224, 14 };

    public AESEncrtptor() {
        try {
            byteKey = strKey.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public enum EnumCipherMode {
        CBC, ECB
    }

    public String Encrypt(String strdata, EnumCipherMode CipherMode) {
        try {
            String strAlgorithm = "AES/CBC/PKCS7Padding";
            switch (CipherMode) {
            case CBC:
                strAlgorithm = "AES/CBC/PKCS7Padding";
                break;
            case ECB:
                strAlgorithm = "AES/ECB/PKCS7Padding";
                break;
            }
            Cipher c = Cipher.getInstance(strAlgorithm);
            SecretKeySpec keySpec = new SecretKeySpec(byteKey, strAlgorithm);
            switch (CipherMode) {
            case CBC:
                c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(
                        byteVector));
                break;
            case ECB:
                c.init(Cipher.ENCRYPT_MODE, keySpec);
                break;
            }
            byte[] data = strdata.getBytes();

            byte[] encrypted = c.doFinal(data);
            return Base64.encodeToString(encrypted, Base64.DEFAULT);
        } catch (Exception e) {
            return "";
        }
    }

    public String Decrypt(String strdata, EnumCipherMode CipherMode) {
        try {
            String strAlgorithm = "AES/CBC/PKCS7Padding";
            switch (CipherMode) {
            case CBC:
                strAlgorithm = "AES/CBC/PKCS7Padding";
                break;
            case ECB:
                strAlgorithm = "AES/ECB/PKCS7Padding";
                break;
            }
            Cipher d_c = Cipher.getInstance(strAlgorithm);
            SecretKeySpec d_keySpec = new SecretKeySpec(byteKey, strAlgorithm);
            switch (CipherMode) {
            case CBC:
                d_c.init(Cipher.DECRYPT_MODE, d_keySpec, new IvParameterSpec(
                        byteVector));
                break;
            case ECB:
                d_c.init(Cipher.DECRYPT_MODE, d_keySpec);
                break;
            }

            byte[] decrypted = d_c.doFinal(Base64.decode(strdata,
                    Base64.DEFAULT));
            String decryptedStr = "";
            for (int i = 0; i < decrypted.length; i++)
                decryptedStr += (char) decrypted[i];
            return decryptedStr;
        } catch (Exception e) {
            return "";
        }
    }
}
于 2012-11-21T12:59:57.900 回答
1

实际上我并没有在 doFinal() 之前调用 cipher.processBytes()。所以我想加密长度为 20 字节的数据,它过去只返回 16 字节作为加密数据。

这是最终代码:

private byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data) throws Exception {
        int minSize = cipher.getOutputSize(data.length);
        byte[] outBuf = new byte[minSize];
        int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
        int length2 = cipher.doFinal(outBuf, length1);
        int actualLength = length1 + length2;
        byte[] result = new byte[actualLength];
        System.arraycopy(outBuf, 0, result, 0, result.length);
        return result;
    }
于 2012-12-25T08:54:24.580 回答