1

我使用 google 显示的生物识别管理器流程成功加密了数据,但是在尝试解密加密数据时,我一直在点击 IllegalBlockSizeException 子句。我需要帮助。

这是我处理所有指纹加密的类(效果很好),但解密不起作用,因为它一直遇到 IllegalBlockSizeException 子句

public class FingerPrintEncrypter {
    private static FingerPrintEncrypter fingerPrintEncrypter;
    private static final String ENCRYPTION_BLOCK_MODE = KeyProperties.BLOCK_MODE_CBC;
    private static final String ENCRYPTION_PADDING = KeyProperties.ENCRYPTION_PADDING_PKCS7;
    private static final String ENCRYPTION_ALGORITHM = KeyProperties.KEY_ALGORITHM_AES;
    private static final String ANDROID_KEY_STORE = "AndroidKeyStore";
    byte[] initVector;

    private FingerPrintEncrypter(){

    }

    public static synchronized FingerPrintEncrypter getInstance(){
        if(fingerPrintEncrypter == null){
            fingerPrintEncrypter = new FingerPrintEncrypter();
        }
        return fingerPrintEncrypter;

    }

    /*
    gets the Cipher obj in encryptionMode for encryption tasks.
     */
    public Cipher getCipherForEncryption(String aliasKeyName){
        try {
            Cipher cipher = getCipher();
            SecretKey secretKey = getOrCreateSecreteKey(aliasKeyName);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            initVector = cipher.getIV();
            return cipher;
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
    gets the Cipher obj in decryptionMode for decryption tasks.
     */
    public Cipher getCipherForDecryption(String aliasKeyName){
        try{
            Cipher cipher = getCipher();
            SecretKey secretKey = getOrCreateSecreteKey(aliasKeyName);
            IvParameterSpec decryptParamSpec = new IvParameterSpec(initVector);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, decryptParamSpec);
            return cipher;
        }catch (InvalidKeyException e){
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    private SecretKey getOrCreateSecreteKey(String aliasKeyName) {
        try {
            KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
            keyStore.load(null);
            SecretKey key = ((SecretKey)keyStore.getKey(aliasKeyName, null));
            if(key == null){
                // key previously create so just grab it
                generateSecretKey(aliasKeyName);
            }
            return key;
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void generateSecretKey(String aliasKeyName){
        try {
            // key not create create a new one
            KeyGenParameterSpec parameterSpec = new KeyGenParameterSpec.Builder(aliasKeyName,
                    KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(ENCRYPTION_BLOCK_MODE)
                    .setEncryptionPaddings(ENCRYPTION_PADDING)
                    .setUserAuthenticationRequired(true)
                    .build();

            KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM, ANDROID_KEY_STORE);
            keyGenerator.init(parameterSpec);
            keyGenerator.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
    }


    private Cipher getCipher() {
        try {
            return Cipher.getInstance(ENCRYPTION_ALGORITHM + "/"
                    + ENCRYPTION_BLOCK_MODE + "/"
                    + ENCRYPTION_PADDING);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String encryptData(String info, Cipher cipher){
        try {
            byte[] encryptedData = cipher.doFinal(info.getBytes("UTF-8"));
            return Base64.encodeToString(encryptedData, Base64.NO_WRAP);
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String decryptData(String stringToDecrypt, Cipher cipher){
        try {
            return new String(cipher.doFinal(Base64.decode(stringToDecrypt, Base64.NO_WRAP)));
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }


}
4

0 回答 0