代码之家  ›  专栏  ›  技术社区  ›  Panadol Chong

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

  •  0
  • Panadol Chong  · 技术社区  · 6 年前

    我的工作区中有一个现有的AES encryption 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 回复  |  直到 6 年前