Ответы пользователя по тегу AES
  • Как зашифровать посредством javax.crypto.Cipher с помощью своего ключа?

    @odissey_nemo
    Программист, ГИС-системы, растры, космоснимки
    Вот такая штука работает, зашифровывает и расшифровывает :

    package su.common.crypto;
    
    import org.apache.commons.lang.StringUtils;
    import su.common.misc.Text;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import java.security.NoSuchAlgorithmException;
    
    /**
     * Created in su.common.crypto.<br>
     * User: ******<br>
     * Date: 30.08.2011<br>
     * Time: 17:12:45<br>
     */
    public class SimpleEncDec {
        private static final String DEFAULT_ALGO_NAME = "AES";
        //	private static final String DEFAULT_CIPHER_NAME = "AES/ECB/PKCS5Padding";
        private static final String DEFAULT_CIPHER_NAME = "AES/ECB/NoPadding";
        private KeyGenerator m_keygen;
        private SecretKey m_key = m_keygen.generateKey();
        private Cipher m_cipherIn;
        private Cipher m_cipherOut;
    
        public SimpleEncDec( String algorithmName ) throws NoSuchAlgorithmException {
    //        KeyGenerator m_keygen;
            if ( Text.isEmpty( algorithmName ) )
                m_keygen = KeyGenerator.getInstance( DEFAULT_ALGO_NAME );
            else
                m_keygen = KeyGenerator.getInstance( algorithmName );
    
            // Create the cipher
            try {
                m_cipherIn = Cipher.getInstance( DEFAULT_CIPHER_NAME );
                m_cipherIn.init( Cipher.ENCRYPT_MODE, m_key );
                m_cipherOut = Cipher.getInstance( DEFAULT_CIPHER_NAME );
                m_cipherOut.init( Cipher.DECRYPT_MODE, m_key );
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }
    
        public SimpleEncDec() throws NoSuchAlgorithmException, NoSuchPaddingException {
            this( null );
        }
    
        public String encrypt( String arg, String password ) {
            try {
                byte[] res = m_cipherIn.doFinal( arg.getBytes() );
                return Base64.encode( res );
            } catch ( Exception e ) {
                System.err.println(e);
            }
            return null;
        }
    
        public String decrypt( String arg, String password ) {
            byte[] res = Base64.decode( arg );
            try {
                res = m_cipherOut.doFinal( res );
                return new String( res );
            } catch ( Exception e ) {
                System.err.println(e);
            }
            return null;
        }
    
        public static String strPad16( String strToPad ) {
            final int len = strToPad.length();
            if (len % 16 == 0)
                return strToPad;
            return StringUtils.rightPad( strToPad, (len + 16 - (len%16)) );
        }
    
        public static void main( String[] args ) throws NoSuchAlgorithmException {
    
            SimpleEncDec sed = new SimpleEncDec( "AES" );
            // check functionality
            final String password = "simple_password";
    
            String checkStr = "String to encode/decode for algorithms test";
            System.out.println( String.format( "String to encrypt: \"%s\"", strPad16(checkStr) ) );
    
            // encode string
            String encStr = sed.encrypt( strPad16(checkStr), password );
            System.out.println(String.format( "Encrypted into \"%s\"", encStr ) );
    
            final String decrypt = sed.decrypt( encStr, password );
            String decStr = Text.trimRight( decrypt );
            System.out.println( String.format( "Decrypted to \"%s\"", decStr ) );
    
            if ( decStr.equals( checkStr ) )
                System.out.println( "+++ Encryption/decryption works well +++" );
            else
                System.err.println( "--- Encryption/decryption doesn't work ---" );
        }
    
    }

    Делал давно и все детали забыл напрочь :o) .
    Не указанный Text.trimRight(String) просто убирает все пустые символы справа, от последнего значащего символа. Text.isEmpty(String) проверяет, пуста ли строка ("", null) или нет.
    Класс Base64 переводит байтовые массивы в массив символов Basе64 и обратно.

    В выбранном Вами алгоритме требуются входные строки длиной кратной 16 символам, т.е. 16,32,48,64 и т.д.
    Ответ написан
    Комментировать