Encrypt and decrypt text with AES algorithm
As you see this implementation is using openssl instead of mcrypt and the result of the encryption/decryption
is not compatible with each other.
The mcrypt function will be deprecated feature in PHP 7.1.x
It is a webtool to encrypt and decrypt text using AES encryption algorithm. You can chose 128, 192 or 256-bit long key size for encryption and decryption. The result of the process is downloadable in a text file.
If you want to encrypt a text put it in the white textarea above, set the key of the encryption then push the Encrypt button.
The result of the encryption will appear in base64 encoded to prevent character encoding problems.
If you want to decrypt a text be sure it is in base64 encoded and is encrypted with AES algorithm!
Put the encrypted text in the white textarea, set the key and push the Decrypt button.
When you want to encrypt a confidential text into a decryptable format, for example when you need to send sensitive data in e-mail.
The decryption of the encrypted text it is possible only if you know the right password.
AES (acronym of Advanced Encryption Standard) is a symmetric encryption algorithm.
The algorithm was developed by two Belgian cryptographer Joan Daemen and Vincent Rijmen.
AES was designed to be efficient in both hardware and software, and supports a block length of 128 bits and key lengths of 128, 192, and 256 bits.
AES encryption is used by U.S. for securing sensitive but unclassified material, so we can say it is enough secure.
/** Aes encryption */ class AES { protected $key; protected $data; protected $method; /** * Available OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING * * @var type $options */ protected $options = 0; /** * * @param type $data * @param type $key * @param type $blockSize * @param type $mode */ function __construct($data = null, $key = null, $blockSize = null, $mode = 'CBC') { $this->setData($data); $this->setKey($key); $this->setMethode($blockSize, $mode); } /** * * @param type $data */ public function setData($data) { $this->data = $data; } /** * * @param type $key */ public function setKey($key) { $this->key = $key; } /** * CBC 128 192 256 CBC-HMAC-SHA1 128 256 CBC-HMAC-SHA256 128 256 CFB 128 192 256 CFB1 128 192 256 CFB8 128 192 256 CTR 128 192 256 ECB 128 192 256 OFB 128 192 256 XTS 128 256 * @param type $blockSize * @param type $mode */ public function setMethode($blockSize, $mode = 'CBC') { if($blockSize==192 && in_array('', array('CBC-HMAC-SHA1','CBC-HMAC-SHA256','XTS'))){ $this->method=null; throw new Exception('Invlid block size and mode combination!'); } $this->method = 'AES-' . $blockSize . '-' . $mode; } /** * * @return boolean */ public function validateParams() { if ($this->data != null && $this->method != null ) { return true; } else { return FALSE; } } //it must be the same when you encrypt and decrypt protected function getIV() { return '1234567890123456'; //return mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->mode), MCRYPT_RAND); return openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->method)); } /** * @return type * @throws Exception */ public function encrypt() { if ($this->validateParams()) { return trim(openssl_encrypt($this->data, $this->method, $this->key, $this->options,$this->getIV())); } else { throw new Exception('Invlid params!'); } } /** * * @return type * @throws Exception */ public function decrypt() { if ($this->validateParams()) { $ret=openssl_decrypt($this->data, $this->method, $this->key, $this->options,$this->getIV()); return trim($ret); } else { throw new Exception('Invlid params!'); } } }
/** Aes encryption */ class AES { const M_CBC = 'cbc'; const M_CFB = 'cfb'; const M_ECB = 'ecb'; const M_NOFB = 'nofb'; const M_OFB = 'ofb'; const M_STREAM = 'stream'; protected $key; protected $cipher; protected $data; protected $mode; protected $IV; /** * * @param type $data * @param type $key * @param type $blockSize * @param type $mode */ function __construct($data = null, $key = null, $blockSize = null, $mode = null) { $this->setData($data); $this->setKey($key); $this->setBlockSize($blockSize); $this->setMode($mode); $this->setIV(""); } /** * * @param type $data */ public function setData($data) { $this->data = $data; } /** * * @param type $key */ public function setKey($key) { $this->key = $key; } /** * * @param type $blockSize */ public function setBlockSize($blockSize) { switch ($blockSize) { case 128: $this->cipher = MCRYPT_RIJNDAEL_128; break; case 192: $this->cipher = MCRYPT_RIJNDAEL_192; break; case 256: $this->cipher = MCRYPT_RIJNDAEL_256; break; } } /** * * @param type $mode */ public function setMode($mode) { switch ($mode) { case AES::M_CBC: $this->mode = MCRYPT_MODE_CBC; break; case AES::M_CFB: $this->mode = MCRYPT_MODE_CFB; break; case AES::M_ECB: $this->mode = MCRYPT_MODE_ECB; break; case AES::M_NOFB: $this->mode = MCRYPT_MODE_NOFB; break; case AES::M_OFB: $this->mode = MCRYPT_MODE_OFB; break; case AES::M_STREAM: $this->mode = MCRYPT_MODE_STREAM; break; default: $this->mode = MCRYPT_MODE_ECB; break; } } /** * * @return boolean */ public function validateParams() { if ($this->data != null && $this->key != null && $this->cipher != null) { return true; } else { return FALSE; } } public function setIV($IV) { $this->IV = $IV; } protected function getIV() { if ($this->IV == "") { $this->IV = mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->mode), MCRYPT_RAND); } return $this->IV; } /** * @return type * @throws Exception */ public function encrypt() { if ($this->validateParams()) { return trim(base64_encode( mcrypt_encrypt( $this->cipher, $this->key, $this->data, $this->mode, $this->getIV()))); } else { throw new Exception('Invlid params!'); } } /** * * @return type * @throws Exception */ public function decrypt() { if ($this->validateParams()) { return trim(mcrypt_decrypt( $this->cipher, $this->key, base64_decode($this->data), $this->mode, $this->getIV())); } else { throw new Exception('Invlid params!'); } } }
include 'AES.php's; $inputText = "My text to encrypt"; $inputKey = "My text to encrypt"; $blockSize = 256; $aes = new AES($inputText, $inputKey, $blockSize); $enc = $aes->encrypt(); $aes->setData($enc); $dec=$aes->decrypt(); echo "After encryption: ".$enc."<br/>"; echo "After decryption: ".$dec."<br/>";Download PHP AES encryption example
import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; /** Aes encryption */ public class AES { private static SecretKeySpec secretKey ; private static byte[] key ; private static String decryptedString; private static String encryptedString; public static void setKey(String myKey){ MessageDigest sha = null; try { key = myKey.getBytes("UTF-8"); System.out.println(key.length); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); // use only first 128 bit System.out.println(key.length); System.out.println(new String(key,"UTF-8")); secretKey = new SecretKeySpec(key, "AES"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String getDecryptedString() { return decryptedString; } public static void setDecryptedString(String decryptedString) { AES.decryptedString = decryptedString; } public static String getEncryptedString() { return encryptedString; } public static void setEncryptedString(String encryptedString) { AES.encryptedString = encryptedString; } public static String encrypt(String strToEncrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")))); } catch (Exception e) { System.out.println("Error while encrypting: "+e.toString()); } return null; } public static String decrypt(String strToDecrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, secretKey); setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)))); } catch (Exception e) { System.out.println("Error while decrypting: "+e.toString()); } return null; } public static void main(String args[]) { final String strToEncrypt = "My text to encrypt"; final String strPssword = "encryptor key"; AES.setKey(strPssword); AES.encrypt(strToEncrypt.trim()); System.out.println("String to Encrypt: " + strToEncrypt); System.out.println("Encrypted: " + AES.getEncryptedString()); final String strToDecrypt = AES.getEncryptedString(); AES.decrypt(strToDecrypt.trim()); System.out.println("String To Decrypt : " + strToDecrypt); System.out.println("Decrypted : " + AES.getDecryptedString()); } }Download Java AES encryption example
2024-04-12