Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES encryption in Perl 6?

Tags:

raku

I'm trying to convert a module writing in Python to Perl 6, I find there is no AES method in Perl 6:

from Cryptodome.Cipher import AES
import base64

def aes(text, key):
    pad = 16 - len(text) % 16
    text = text + bytearray([pad] * pad)
    encryptor = AES.new(key, 2, b"0102030405060708")
    ciphertext = encryptor.encrypt(text)
    return base64.b64encode(ciphertext)

Is there any module writing in Perl 6 that implemented AES method?

like image 867
chenyf Avatar asked Jan 01 '19 15:01

chenyf


1 Answers

It seems that the OpenSSL module provides access to various AES ciphers. It depends on the openssl library being available (although on Windows I believe it downloads the DLL as part of the module installation process).

With that installed (zef install OpenSSL), one can:

use OpenSSL::CryptTools;

And then use encrypt/decrypt:

# Fake IV and key
my $iv = ('0' x 16).encode;
my $key = ('xy' x 16).encode;

# Encrypt.
my $ciphertext = encrypt("asdf".encode, :aes256, :$iv, :$key);
say $ciphertext;

# Decrypt.
say decrypt($ciphertext, :aes256, :$iv, :$key).decode;

See these tests for more examples.

like image 92
Jonathan Worthington Avatar answered Oct 30 '22 01:10

Jonathan Worthington