miniAES with hexadecimal inputs

asked 0 years ago

Aneela gravatar image

Hi, I am new to sagemath and having a hard time in giving mini-AES a hexadecimal input rather than string as mentioned in example. Below is my code.

def hex_to_binary(hex_str):
num = int(hex_str, 16)  
binary_str = format(num, '0{}b'.format(len(hex_str) * 4))
return binary_str

P = hex_to_binary("4645"); P
K = hex_to_binary("7365"); K

from sage.all import *
from sage.crypto.block_cipher.miniaes import MiniAES

maes = MiniAES()
C = maes(P, K, algorithm='encrypt'); C

The error is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_823/1348971521.py in <module>
  3 
  4 maes = MiniAES()
----> 5 C = maes(P, K, algorithm='encrypt'); C

/ext/sage/9.6/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/crypto/block_cipher/miniaes.py in __call__(self, B, key, algorithm)
349         from sage.rings.finite_rings.integer_mod import Mod
350         if not isinstance(B, StringMonoidElement):
--> 351             raise TypeError("input B must be a non-empty binary string with number of bits a multiple of 16")
352         if (len(B) == 0) or (Mod(len(B), self._key_size).lift() != 0):
353             raise ValueError("the number of bits in the binary string B must be positive and a multiple of 16")
TypeError: input B must be a non-empty binary string with number of bits a multiple of 16

My inputs are both nonempty and multiple of 16. However same code runs when given string input like:

bin = BinaryStrings()
key = bin.encoding("KE");key

One thing more which I noticed is that sagemath miniAES implementation gives different result for the example mentioned in miniAES research paper.

P_example = 1001110001100011 (9C63)
K_example = 1100001111110000 (C3F0)
C_example = 0111001011000110 (72C6)

But sageMath gives different result. (In order to follow example, I have changed binaries of P and K to string) C_sage = 0011010111111100 (35FC)

Preview: (hide)