|
| 1 | +import aesio |
| 2 | +from binascii import hexlify, unhexlify |
| 3 | + |
| 4 | +# doc example |
| 5 | +key = b"Sixteen byte key" |
| 6 | +inp = b"CircuitPython!!!" # Note: 16-bytes long |
| 7 | +outp = bytearray(len(inp)) |
| 8 | +cipher = aesio.AES(key, aesio.MODE_ECB) |
| 9 | +cipher.encrypt_into(inp, outp) |
| 10 | +print(str(hexlify(outp), "")) |
| 11 | + |
| 12 | +cipher = aesio.AES(key, aesio.MODE_ECB) |
| 13 | +cipher.decrypt_into(outp, outp) |
| 14 | +print(str(outp, "")) |
| 15 | +print() |
| 16 | + |
| 17 | +print("ECB") |
| 18 | +# ECB mode test vector, from the aes.c source |
| 19 | +plaintext = unhexlify( |
| 20 | + "6bc1bee22e409f96e93d7e117393172a" |
| 21 | + "ae2d8a571e03ac9c9eb76fac45af8e51" |
| 22 | + "30c81c46a35ce411e5fbc1191a0a52ef" |
| 23 | + "f69f2445df4f9b17ad2b417be66c3710" |
| 24 | +) |
| 25 | + |
| 26 | +key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c") |
| 27 | + |
| 28 | +cyphertext = bytearray(len(plaintext)) |
| 29 | +cipher = aesio.AES(key, aesio.MODE_ECB) |
| 30 | +for i in range(0, len(plaintext), 16): |
| 31 | + output = memoryview(cyphertext)[i : i + 16] |
| 32 | + cipher.encrypt_into(plaintext[i : i + 16], output) |
| 33 | + print(str(hexlify(output), "")) |
| 34 | +print() |
| 35 | + |
| 36 | +plaintext = bytearray(len(plaintext)) |
| 37 | +cipher = aesio.AES(key, aesio.MODE_ECB) |
| 38 | +for i in range(0, len(plaintext), 16): |
| 39 | + output = memoryview(plaintext)[i : i + 16] |
| 40 | + cipher.decrypt_into(cyphertext[i : i + 16], output) |
| 41 | + print(str(hexlify(output), "")) |
| 42 | +print() |
| 43 | + |
| 44 | +print("CBC") |
| 45 | +# CBC128-AES128 test vector from NIST Special Publication 800-38A, 2001 edition, p50 |
| 46 | + |
| 47 | +plaintext = unhexlify( |
| 48 | + "6bc1bee22e409f96e93d7e117393172a" |
| 49 | + "ae2d8a571e03ac9c9eb76fac45af8e51" |
| 50 | + "30c81c46a35ce411e5fbc1191a0a52ef" |
| 51 | + "f69f2445df4f9b17ad2b417be66c3710" |
| 52 | +) |
| 53 | + |
| 54 | +key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c") |
| 55 | +iv = unhexlify("000102030405060708090a0b0c0d0e0f") |
| 56 | +cyphertext = bytearray(len(plaintext)) |
| 57 | +cipher = aesio.AES(key, aesio.MODE_CBC, IV=iv) |
| 58 | +for i in range(0, len(plaintext), 16): |
| 59 | + output = memoryview(cyphertext)[i : i + 16] |
| 60 | + cipher.encrypt_into(plaintext[i : i + 16], output) |
| 61 | + print(str(hexlify(output), "")) |
| 62 | +print() |
| 63 | + |
| 64 | +plaintext = bytearray(len(plaintext)) |
| 65 | +cipher = aesio.AES(key, aesio.MODE_CBC, IV=iv) |
| 66 | +for i in range(0, len(plaintext), 16): |
| 67 | + output = memoryview(plaintext)[i : i + 16] |
| 68 | + cipher.decrypt_into(cyphertext[i : i + 16], output) |
| 69 | + print(str(hexlify(output), "")) |
| 70 | +print() |
| 71 | + |
| 72 | + |
| 73 | +print("CTR") |
| 74 | +# CTR128-AES128 test vector from NIST Special Publication 800-38A, 2001 edition, p55 |
| 75 | + |
| 76 | +plaintext = unhexlify( |
| 77 | + "6bc1bee22e409f96e93d7e117393172a" |
| 78 | + "ae2d8a571e03ac9c9eb76fac45af8e51" |
| 79 | + "30c81c46a35ce411e5fbc1191a0a52ef" |
| 80 | + "f69f2445df4f9b17ad2b417be66c3710" |
| 81 | +) |
| 82 | + |
| 83 | +key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c") |
| 84 | +counter = unhexlify("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff") |
| 85 | +cyphertext = bytearray(len(plaintext)) |
| 86 | +cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter) |
| 87 | +for i in range(0, len(plaintext), 16): |
| 88 | + output = memoryview(cyphertext)[i : i + 16] |
| 89 | + cipher.encrypt_into(plaintext[i : i + 16], output) |
| 90 | + print(str(hexlify(output), "")) |
| 91 | +print() |
| 92 | + |
| 93 | +plaintext = bytearray(len(plaintext)) |
| 94 | +cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter) |
| 95 | +for i in range(0, len(plaintext), 16): |
| 96 | + output = memoryview(plaintext)[i : i + 16] |
| 97 | + cipher.decrypt_into(cyphertext[i : i + 16], output) |
| 98 | + print(str(hexlify(output), "")) |
| 99 | +print() |
0 commit comments