thank you for your hard work! I'm Ishiguro from GMO Research!
By the way, I think that you may suddenly be urged to encrypt with AES. For those of you, this time we will write encryption / composite code in a total of three ways: Java and PHP, PHP is OpenSSL and Mcrypt. It is implemented with AES256 algorithm, ECB mode, PKCS5 padding.
Java 1.8 PHP 5.6
Java
By default, Java can only handle AES key lengths of 128 bits. In order to handle 256bit keys, it is necessary to replace the Java policy file.
For details, refer to articles such as Making AES256 available in Java.
aes01.java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class aes01 {
public static void main(String[] args) {
try {
byte[] key = DatatypeConverter.parseBase64Binary(
"Base64 encoded string of keys");
SecretKeySpec sks = new SecretKeySpec(key, "AES");
byte[] input = "The string you want to encrypt".getBytes();
//encryption
Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, sks);
byte encrypted[] = c.doFinal(input);
System.out.println(DatatypeConverter.printBase64Binary(encrypted));
//Decryption
c.init(Cipher.DECRYPT_MODE, sks);
byte decrypted[] = c.doFinal(encrypted);
System.out.println(new String(decrypted));
} catch (Exception e) {
e.printStackTrace();
}
}
}
PHP(OpenSSL)
Encryption with PHP 5.3 or later is done with OpenSSL.
aes01.php
<?php
$key = base64_decode("Base64 encoded string of keys");
$input = 'The string you want to encrypt';
//encryption
$encrypted = openssl_encrypt($input, 'aes-256-ecb', $key);
echo $encrypted;
//Decryption
$decrypted = openssl_decrypt($encrypted, 'aes-256-ecb', $key);
echo $decrypted;
Encryption in a deprecated way. Do not use it except when necessary. Or rather, it's too annoying.
aes02.php
<?php
$key = base64_decode('Base64 encoded string of keys');
$input = 'The string you want to encrypt';
$crypt = new Crypt($key);
//encryption
$encrypted = base64_encode($crypt->encrypt($input));
echo $encrypted;
//Decryption
$decrypted = $crypt->decrypt(base64_decode($encrypted));
echo $decrypted;
class Crypt {
private $__encrypt_key = null;
public $iv = null;
public function __construct($encrypt_key) {
$this->__encrypt_key = $encrypt_key;
}
public function encrypt($input, $algo = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_ECB) {
list($size, $td) = $this->__open($algo, $mode);
$input = $this->__pkcs5Pad($input, $size);
$data = mcrypt_generic($td, $input);
$this->__close($td);
return $data;
}
public function decrypt($input, $algo = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_ECB) {
list ($size, $td) = $this->__open($algo, $mode);
$input = mdecrypt_generic($td, $input);
$data = $this->__pkcs5Unpad($input);
$this->__close($td);
return $data;
}
private function __open($algo, $mode) {
$size = mcrypt_get_block_size($algo, $mode);
$td = mcrypt_module_open($algo, '', $mode, '');
$this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $this->__encrypt_key, $this->iv);
return array($size, $td);
}
private function __close($td){
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
public static function __pkcs5Pad($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public static function __pkcs5Unpad($text) {
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) !== $pad) return false;
return substr($text, 0, -1 * $pad);
}
}
MCRYPT_RIJNDAEL_128 seems to be equivalent to AES256. There is also an algorithm called MCRYPT_RIJNDAEL_256, but note that this is not the case.
For the time being, it is possible to change the algorithm and mode so that it can be used universally, I'm not sure if it really works.
that's all! It's okay to be driven by the urge to AES encrypt.
I'm glad that PHP's OpenSSL is very simple. (Small feeling)
Recommended Posts