2

I'm using AES 256 to encrypt the integers in my application. When I convert encrypted text to integer it gives me too long integer like:

127510614367469717778923839884853125321

Even if the input plain text is a single digit Integer.

I'm using following code:

from Crypto.Cipher import AES
import binascii
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
message = "0000000000000001" 
ct = obj.encrypt(message)
hexa = binascii.hexlify(ct)
i = int(hexa,16)
print(i)

The output cipher text is too long. I want output cipher text as 3 or 4 digit number rather than this long numbers.

Is there any alternatives for this? Can I encrypt using AES 8 or AES 16? So that out cipher text contains less number of digits?

Kaushal28
  • 503
  • 1
  • 6
  • 18
  • What security guarantees are you expecting? A 3 digit number may be guessed or brute forced. Anything that you do would not be secure and would only thwart casual attackers. If you understand that, you might have luck with [hashids](http://hashids.org/). Anyway, do you really need them to be recoverable? Perhaps a cropped hash might work too. – Artjom B. Apr 06 '17 at 18:51

1 Answers1

5

The "256" in "AES256" refers to the key size, the "problem" you have is with the block size.

AES is a block cipher with a 128 bit (16 byte) block size. This means that it expects as input, and produces as output 16 byte blocks of data. This is feature of the algorithm, and is the same regardless of the key size variant (AES 128/192/256). This is an inherent feature of block ciphers, and whilst there exist ciphers with smaller block sizes than AES, this in fact makes them less secure (see for example Sweet32).

You could however use AES in a mode that results in it becoming a stream cipher with input size = output size, e.g. CTR (Counter) mode.

Iridium
  • 23,323
  • 6
  • 52
  • 74
  • thank you. but can you provide an example of CTR mode with AES? – Kaushal28 Apr 06 '17 at 07:54
  • http://stackoverflow.com/questions/3154998/pycrypto-problem-using-aesctr I refered this link but it is giving error – Kaushal28 Apr 06 '17 at 08:12
  • If you're having issues with a different piece of code, I would suggest that you post another question for it, being sure to give the code you're using and details of the error(s) you are encountering. – Iridium Apr 06 '17 at 10:34