php openssal to java abs cbc encrypt /
CTR mode from php openssl to pycrypto
php openssl ctr mode to python pycripto
To convert the php AES.CTR encryption to python AES.CTR encryption
php openssl code
$cipher = 'aes-256-ctr';
if (in_array($cipher, openssl_get_cipher_methods()))
{
// Usually the below code is used for $iv
// $ivlen = openssl_cipher_iv_length($cipher);
// $iv = openssl_random_pseudo_bytes($ivlen);
$key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
// to start Counter from 1
$iv0 = [00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01];
$iv = implode(array_map("chr", $iv0));
$plaintext = 'test.test';
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA, $iv);
$encodedCT = base64_encode($ciphertext); // $encodedCT => 'izQmJQ8nkPMP'
//store $cipher, $iv, and $tag for decryption later
$original_plaintext = openssl_decrypt($encodedCT, $cipher, $key, 0, $iv);
$original_plaintext2 = openssl_decrypt($ciphertext , $cipher, $key, OPENSSL_RAW_DATA, $iv);
}
python pycripto code
from Crypto.Cipher import AES
from Crypto.Util import Counter
import base64
def encrypt_token_ctr(data):
key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
iv = 1
# to convert string 'iv' to int value to use for initial_value
# iv = b'AAAAAAAAAAAAAAAA'
# import binascii
# hexval = binascii.hexlify(iv)
# iv = int(hexval, 16)
aes = AES.new(key=key,
mode=AES.MODE_CTR,
counter=Counter.new(128, initial_value=iv)
)
return aes.encrypt(data.encode('utf8'))
def decrypt_token_ctr(data):
key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
iv = 1
aes = AES.new(key=key,
mode=AES.MODE_CTR,
counter=Counter.new(128, initial_value=iv)
)
return aes.decrypt(data)
print('Python encrypt: ' + str(base64.b64encode(encrypt_token_ctr('test.test'))) )
print('Python encrypt: ' + str(decrypt_token_ctr(base64.b64decode('izQmJQ8nkPMP'))) )
>> Python encrypt2: b'mzwsKw88h+c='
>> Python decrypt2: b'test.test'
AES CBC mode
php code
class AES
{
var $key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
var $iv = 'AAAAAAAAAAAAAAAA';
function encryptToken($data)
{
// Mcrypt library has been DEPRECATED since PHP 7.1, use openssl:
// return openssl_encrypt($data, 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
$message_padded = $data;
if (strlen($message_padded) % 8) {
$message_padded = str_pad($message_padded,
strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
}
// $padding = 16 - (strlen($data) % 16);
// $data .= str_repeat(chr($padding), $padding);
return openssl_encrypt($message_padded, 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
}
function decryptToken($data)
{
// Mcrypt library has been DEPRECATED since PHP 7.1, use openssl:
// return openssl_decrypt(base64_decode($data), 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
$data = openssl_decrypt(base64_decode($data), 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
$padding = ord($data[strlen($data) - 1]);
return substr($data, 0, -$padding);
}
}
python code
from Crypto.Cipher import AES
def _pad(s): return (s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)).encode('utf8')
def _cipher():
key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
iv = b'AAAAAAAAAAAAAAAA'
return AES.new(key=key, mode=AES.MODE_CBC, IV=iv)
def encrypt_token(data):
return _cipher().encrypt(_pad(data))
if __name__ == '__main__':
print('Python encrypt: ' + str(base64.b64encode(encrypt_token('test.test'))) )
print('Python decrypt: ' + str(decrypt_token(base64.b64decode('1pRLMUTqMCdBM4EyNyBTuQ=='))) )
See Also
- Encrypt and decrypt content with Nodejs · Christoph Hartmann
- AES/CBC/PKCS5Padding encrypt/decrypt PHP and JAVA example classes : java code <--> php code
댓글 없음:
댓글 쓰기