0

再会,

我的工作区中有一个现有的 AES 加密 java 类,但是,我想知道它使用 128 或 256,我试图用谷歌搜索它但仍然无法得到它,以下是代码:

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;
    }

}

请告知如何识别它。

4

0 回答 0