FBEncrypter库与安卓的兼容性

3
我已经在iOS中使用了以下的加密和解密库。

https://github.com/dev5tec/FBEncryptor

现在我想要在Android上使用同样的功能。是否也有适用于Android的支持?如果没有,那么我该如何在Android中使用此库来满足我的需求呢?或者请推荐另一个与FBEncryptor相同的加密库。
我已经实现了以下代码。
public class AESHelper {

    private final Cipher cipher;
    private final SecretKeySpec key;
    private AlgorithmParameterSpec spec;
    private static final String KEY = "VHJFTFRGJHGHJDhkhjhd/dhfdh=";


    public AESHelper() throws Exception { 
        byte[] keyBytes = KEY.getBytes("UTF-8");
        Arrays.fill(keyBytes, (byte) 0x00);

        cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        key = new SecretKeySpec(keyBytes, "AES");
        spec = getIV();
    }

    public AlgorithmParameterSpec getIV() { 
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        return new IvParameterSpec(iv);
    }

    public String encrypt(String plainText) throws Exception {
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
        String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
        return encryptedText; 
    }

    public String decrypt(String cryptedText) throws Exception {
        cipher.init(Cipher.DECRYPT_MODE, key, spec);
        byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
        byte[] decrypted = cipher.doFinal(bytes);
        String decryptedText = new String(decrypted, "UTF-8");
        return decryptedText; 
    }

}

但它会抛出javax.crypto.BadPaddingException: Pad Block Corrupted异常。

1个回答

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接