javax.crypto.BadPaddingException: данный последний блок не заполнен должным образом. Такие проблемы могут возникнуть, если при расшифровке используется плохой ключ - PullRequest
0 голосов
/ 15 апреля 2020

Я получаю эту ошибку при расшифровке данных с использованием AES.

Я также проверил, что преобразование байтов может привести к потере некоторых данных, но ничего не помогло.

Ключ успешно расшифрован.

Но когда вызывается функция decryptfile, она выдает подчиненное исключение.

Пожалуйста, помогите.

КОД ШИФРОВАНИЯ

GenerateSymmetricKey symmetricKey = new GenerateSymmetricKey( 16, "AES" );
String encryptedKey = RSAEncryptPublic( symmetricKey.getKey().getEncoded(), receiverPublicKey );
String encryptedPaymentFile = AESEncryptFile( filePaymentRecord.getBytes("UTF-8") , symmetricKey.getKey() );

ФУНКЦИИ ШИФРОВАНИЯ

public static String RSAEncryptPublic ( byte[] data, PublicKey publicKey ) {

        Cipher cipher = null;
        byte[] encryptedData = null;

        try {
            cipher = Cipher.getInstance( "RSA/ECB/PKCS1Padding" );

            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            encryptedData = cipher.doFinal( data );

        }
        catch ( Exception e ) {
            log.println( LOG + " Exception in Encryption of Key = " + e  );
        }

        return Base64.getEncoder().encodeToString( encryptedData );

    }

public static String AESEncryptFile( byte[] input, SecretKeySpec key ) throws IOException, GeneralSecurityException {

        Cipher cipher = Cipher.getInstance( "AES/ECB/PKCS5Padding" );

        cipher.init( Cipher.ENCRYPT_MODE, key );

        return Base64.getEncoder().encodeToString( cipher.doFinal( input ) );
    }

КОД ДЕКРИПИРОВАНИЯ

byte[] data3 = getFileInBytes( new File ( path + "files\\key_01704346803_IFT_JAM01_UBL_Export_File_1_15042020_193115-0018.csv") ); // sign

String key = RSADecryptKey( data3, receiverPrivateKey );
SecretKeySpec aesKey = new SecretKeySpec( key.getBytes(), "AES" );          
String paymentFile = decryptFile( data4, aesKey );

ФУНКЦИИ ДЕКРИПИРОВАНИЯ

public static String RSADecryptKey( byte[] data, PrivateKey privateKey ) {

        Cipher cipher = null;
        String decryptedData = "";

        try {
            cipher = Cipher.getInstance( "RSA/ECB/PKCS1Padding" );

            cipher.init( Cipher.DECRYPT_MODE, privateKey );

            byte[] decodedData = Base64.getDecoder().decode( data );

            decryptedData = new String( cipher.doFinal( decodedData ), "UTF-8" );
        }
        catch ( Exception e ) {

            System.out.println(  " Exception in Decryption -> " + e );

        }
        return decryptedData;
    }

public String decryptFile(byte[] input, SecretKeySpec key) throws IOException, GeneralSecurityException { 

        Cipher cipher = Cipher.getInstance( "AES/ECB/PKCS5Padding" );

        cipher.init(Cipher.DECRYPT_MODE, key);

        return new String( cipher.doFinal( Base64.getDecoder().decode( input ) ) );
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...