AES шифрование / дешифрование: ошибка с IVParameters spe - PullRequest
0 голосов
/ 13 декабря 2018

Я использую шифр «AES / CBC / PKCS5Padding» для шифрования файла.

Я использую IVParametersSpec для шифрования / дешифрования файла и соответствующего secretKey. Но я получаю ошибку «недопустимой длины».

 //Encryption of a file

public String encryptFile(KeyInfo keyInfo, String tenantId, InputStream       inputStream, File outputFile)
        throws IOException, InvalidKeyException, NoSuchAlgorithmException {


    SecretKeySpec aeskeySpecToEncrypt = makeKey();// generating AES key

    try {
        aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpecToEncrypt,new IvParameterSpec(new byte[16]));
    } catch (InvalidAlgorithmParameterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // InputStream inputStream = inputFile.getInputStream();
    CipherOutputStream outputStream = new CipherOutputStream(new FileOutputStream(outputFile), aesCipher);

    copy(inputStream, outputStream);

    logger.info("file encrypted/stored successfully");

    outputStream.close();
    inputStream.close();

    return encrypt(keyInfo, aeskeySpecToEncrypt.getEncoded().toString());
}


// generating AES key
public SecretKeySpec makeKey() throws NoSuchAlgorithmException, IOException, InvalidKeyException {

    SecretKeySpec aeskeySpecToEncrypt;
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(AES_Key_Size);
    SecretKey secretKey = kgen.generateKey();

    aeskeySpecToEncrypt = new SecretKeySpec(secretKey.getEncoded(), "AES");

    return aeskeySpecToEncrypt;

}


private void copy(InputStream is, OutputStream os) throws IOException {
    int i;
    byte[] b = new byte[1024];
    while ((i = is.read(b)) != -1) {
        os.write(b, 0, i);
    }
}

И расшифровка кода файла:

//@Override
public void decryptFile(File encryptedFile, String encryptedAesKey,   KeyInfo keyInfo, OutputStream outputStream)
        throws IOException, InvalidKeyException {

    SecretKeySpec aeskeySpec = null;
    byte[] aesKey = decrypt(keyInfo, encryptedAesKey);

    aeskeySpec = new SecretKeySpec(aesKey, "AES");

    System.out.println("length"+aeskeySpec.getEncoded().length);

    try {
        aesCipher.init(Cipher.DECRYPT_MODE, aeskeySpec, new IvParameterSpec(new byte[16]));
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
        throw new RuntimeException("invalid key parameters");

    }

    CipherInputStream is = new CipherInputStream(new FileInputStream(encryptedFile), aesCipher);

    copy(is, outputStream);
    logger.info("file decryption successfully");
    is.close();

Я получаю "Недопустимый ключ длиной 11 байтов".Строка кода ошибки:

    aeskeySpec = new SecretKeySpec(aesKey, "AES");

    System.out.println("length"+aeskeySpec.getEncoded().length);  //11bytes

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...