import unittest
import base64
from py3rijndael import Rijndael
import os
import codecs
#plain text
a=input("Please Enter Plain text: ")
plain_text = a.encode('utf-8')
padded_text = plain_text.ljust(32, b'\x1b')
#key
key=input()
key_bytes = codecs.encode(key, 'UTF-8')
#key_bytes = key.encode('utf-8') #two change string to byte
#base64_key_bytes = base64.b64encode(key_bytes) #base 64 encryption
key_bytes = key_bytes[2:-1]
print(key_bytes)
#encryption
rijndael_key = Rijndael(key_bytes, block_size=32)
cipher = rijndael_key.encrypt(padded_text)
print(cipher)
I m changing a string (generated using os.urandom() and passed the form ) to byte to use as a key for aes algorithm but its resulting in adding one extra "/" to the actual required values please help me with this
Guess you are using python3. (It's totally different on python2 and python3.)
Firstly, let's try to explain why it happens.
You copy a b"\x08\xda...
from else where and paste it to prompt. input
will return an str object (unicode of python2), whose first char is 'b', second is '"', third is '\', forth is 'x', etc.
When it encodes, it becomes a byte string with same characters(represented in bytes), as there are only ascii chars.
Then there is print. What is a byte '\'
be printed? It's printed as b'\\'
that's why you see double back slashes.
An easy (but dangerous) way.
After reading from prompt, eval
it. That is, execute it just like python codes. And then you will get what it was when it was first generated by random. In fact input
in python2 did this for you.
But this is of course insecure because the input your users give may contain any possible string and it may contains some malicious snippet.
A better way I can think of.
As a key is always byte string, and byte strings are generally unprintable. So you need to transform it to something printable, copy-and-paste it here, and de-transform it back. maybe base64 encoding may help you do this.