public String encrypt ( final String pText ) throws GeneralSecurityException { byte [ ] clearBytes = pText . getBytes ( Charset . forName ( "UTF-8" ) ) ; byte [ ] salt = getSalt ( SALT_SIZE ) ; Cipher cipher = createCipher ( salt , Cipher . ENCRYPT_MODE ) ; byte [ ] encryptedBytes = cipher . doFinal ( clearBytes ) ; int len = encryptedBytes . length ; byte padLen = ( byte ) ( CHUNK_SIZE - ( SALT_SIZE + len + 1 ) % CHUNK_SIZE ) ; int totalLen = SALT_SIZE + len + padLen + 1 ; byte [ ] allEncryptedBytes = getSalt ( totalLen ) ; System . arraycopy ( salt , 0 , allEncryptedBytes , 0 , SALT_SIZE ) ; allEncryptedBytes [ SALT_SIZE ] = padLen ; System . arraycopy ( encryptedBytes , 0 , allEncryptedBytes , SALT_SIZE + 1 , len ) ; return Base64Util . encode ( allEncryptedBytes ) ; }
Encrypt a string with a password .