Versão atual:

Criptografia AES 128 bits com chave e IV de 192 bits

Tenho o código de criptografia abaixo, mas está dando o erro: Exception in thread "main" java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long

Eu tenho uma key e um IV de 24 bytes, alguém sabe como criptografar/descriptografar ?

package testecriptografia;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import static java.lang.System.*;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class TesteCriptografia11 {
    private static final String ALGORITHM = "AES";
    private static final String BLOCK_MODE = "CBC";
    private static final String PADDING = "PKCS5Padding";
    private static final Charset CHARSET = Charset.forName( "UTF-8" );
    private static final IvParameterSpec CBC_SALT = new IvParameterSpec("999999999999999999999999".getBytes());

    private static SecretKeySpec generateKey()
            throws NoSuchAlgorithmException
        {
        final KeyGenerator kg = KeyGenerator.getInstance( ALGORITHM );
        kg.init( 192 );// specify key size in bits

        return new SecretKeySpec( "888888888888888888888888".getBytes(), ALGORITHM );
        }
    @SuppressWarnings({ "JavaDoc" })
    private static String readCiphered( Cipher cipher, SecretKeySpec key, File file )
            throws InvalidKeyException, IOException, InvalidAlgorithmParameterException
        {
        cipher.init( Cipher.DECRYPT_MODE, key, CBC_SALT );
        final CipherInputStream cin = new CipherInputStream( new FileInputStream( file ), cipher );
        final int messageLengthInBytes = ( cin.read() << 8 ) | cin.read();
        out.println( file.length() + " enciphered bytes in file" );
        out.println( messageLengthInBytes + " reconstituted bytes" );
        final byte[] reconstitutedBytes = new byte[ messageLengthInBytes ];
        int bytesReadSoFar = 0;
        int bytesRemaining = messageLengthInBytes;
        while ( bytesRemaining > 0 )
            {
            final int bytesThisChunk = cin.read( reconstitutedBytes, bytesReadSoFar, bytesRemaining );
            if ( bytesThisChunk == 0 )
                {
                throw new IOException( file.toString() + " corrupted." );
                }
            bytesReadSoFar += bytesThisChunk;
            bytesRemaining -= bytesThisChunk;
            }
        cin.close();
        return new String( reconstitutedBytes, CHARSET );
        }
    private static void writeCiphered( Cipher cipher, SecretKeySpec key, File file, String plainText )
            throws InvalidKeyException, IOException, InvalidAlgorithmParameterException
        {
        cipher.init( Cipher.ENCRYPT_MODE, key, CBC_SALT );
        final CipherOutputStream cout = new CipherOutputStream( new FileOutputStream( file ), cipher );
        final byte[] plainTextBytes = plainText.getBytes( CHARSET );
        out.println( plainTextBytes.length + " plaintext bytes written" );
        cout.write( plainTextBytes.length >>> 8 );// msb
        cout.write( plainTextBytes.length & 0xff );// lsb
        cout.write( plainTextBytes );
        cout.close();
        }
    public static void main( String[] args ) throws InvalidAlgorithmParameterException,
            InvalidKeyException,
            IOException,
            NoSuchAlgorithmException,
            NoSuchPaddingException
        {
        final String plainText = "Q.E. to throw cream pies at Cheney and Bush tomorrow at 19:05.";
        SecretKeySpec key = generateKey();
        final Cipher cipher = Cipher.getInstance( ALGORITHM + "/" + BLOCK_MODE + "/" + PADDING );
        writeCiphered( cipher, key, new File( "transport.bin" ), plainText );
        final String reconstitutedText = readCiphered( cipher, key, new File( "transport.bin" ) );
        out.println( "original: " + plainText );
        out.println( "reconstituted: " + reconstitutedText );
        }
    }

Versões(3):

Ver a versão formatada

Criptografia AES 128 bits com chave e IV de 192 bits

Comentário

new question