如何识别我的AES加密是128还是256?

时间:2019-07-08 03:43:47

标签: java encryption aes sha256 128-bit

美好的一天,

我的工作区中已有一个现有的AES加密Java类,但是,我想知道它使用的是128还是256,我试图用Google搜索它,但仍然无法获得它,以下是代码:

static {
AesKeyCipher aesKeyCipher = new AesKeyCipher(
                        "747065a6cb23cacf3d4ae71edc929c678e8c15a50379b655b74a30eb77106d68" );
                cipher = aesKeyCipher;
}

public static String encrypt(final String saltText, final String plainText)
            throws UnsupportedEncodingException, GeneralSecurityException {
        final byte[] salt = saltText.getBytes( "UTF-8" );
        final byte[] plain = plainText.getBytes( "UTF-8" );

        for ( int i = 0; i < salt.length; i++ ) {
            if ( i >= plain.length ) {
                break;
            }
            plain[ i ] = (byte) ( salt[ i ] ^ plain[ i ] );
        }

        byte[] encrypted = cipher.encrypt( plain ); // this will call to the following encrypt method

        final String cipherText = new String(
                DatatypeConverter.printBase64Binary( encrypted ) );
        return cipherText;
    }


// this is the encrypt method call by first method
public byte[] encrypt(final byte[] data) throws GeneralSecurityException {
        try {
            final String currentTransform = "/ECB/NoPadding";
            final Cipher cipher = getCipher( currentTransform );
            final SecretKey secretKey = getSecretKey( );
            final AlgorithmParameterSpec params = getAlgorithmParameterSpec( );
            if ( params == null ) {
                cipher.init( Cipher.ENCRYPT_MODE, secretKey );
            } else {
                cipher.init( Cipher.ENCRYPT_MODE, secretKey, params );
            }
            return cipher.doFinal( data );
        } catch ( final GeneralSecurityException e ) {
            e.printStackTrace( );
            throw new EncryptionException( e );
        }
    }

这是我的AesKeyCipher的类:

public class AesKeyCipher extends AbstractSecretKeyCipher implements
        SecretKeyCipher {

    @Override
    protected Cipher getCipher(String transform)
            throws GeneralSecurityException {
        return Cipher.getInstance( getSecretKey( ).getAlgorithm( ) );
    }

    @Override
    protected SecretKey getSecretKey() throws GeneralSecurityException {
        return new SecretKeySpec( hexStringToByteArray( this.key ), "AES" );
    }

    private static byte[] hexStringToByteArray(final String data) {
        int k = 0;
        byte[] results = new byte[data.length( ) / 2];
        for ( int i = 0; i + 1 < data.length( ); i += 2, k++ ) {
            results[ k ] = (byte) ( Character.digit( data.charAt( i ), 16 ) << 4 );
            results[ k ] += (byte) ( Character.digit( data.charAt( i + 1 ), 16 ) );
        }
        return results;
    }

}

请就如何识别它提供建议。

0 个答案:

没有答案