I have found a module pycrypto which has an AES encryption and decryption function, in commandline everything works fine both encryption and decryption,
but if I want to implement it in my program, it fails =(

def aes(txt):
    global crypt
    message="".join(txt) #format the message which has to be encrypted/decrypted to a string
    c=0
    plist="".join(pflist)
    while len(plist)<15:
        plist+="a"
        c+=1
    #loop is to be sure the password has at least 16 characters.
    plist+=str(c)
    obj=AES.new(plist[:16], AES.MODE_ECB)
    if crypt == "en":
        c=0
        while len(message)/16 != len(message)//16:
            message+="a"
            c+=1
        #loop is to be sure the message is a multiple of 16
        ciphertext=obj.encrypt(message)
    else:
        print message,len(message)
        ciphertext=obj.decrypt(message)
    return ciphertext

The encryption system succeeds, but the decryption system fails
if I decrypt it with \xb4\xe0\xa0LH/\xefO\xc9|y7&"Af
the program says it has a length of 31 (which strictly seen is also true),
but if you do the following in command line it will say that the length is 16

string='\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'
len(string)

I don't know what's wrong... I think it has to do something with the type in which the \xb4\xe0\xa0LH/\xefO\xc9|y7&"Af is passed but I'm not sure...

make it a raw string
"\" escapes!

string=r'\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'
len(string)

Lol you did exactly the reverse of what I want :D
I want to know how to make it length 16 like command prompt says

I'm not sure,
maybe do this?

string=b'\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'
len(string)

I'm not sure about what you mean by

if I decrypt it with \xb4\xe0\xa0LH/\xefO\xc9|y7&"Af

is there more to your code?

string=b'\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'
str=u'\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'
len(string) # 16
len(str)     #also 16

Which one is the best to use?
btw in this case I can append the b or u before the quotes but what if it's a variable?

string=input #string = binput or uinpit won't work...
string=b'\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'
str=u'\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'
len(string) # 16
len(str)     #also 16

Which one is the best to use?
btw in this case I can append the b or u before the quotes but what if it's a variable?

string=input #string = binput or uinpit won't work...

b'' would be the best, since you are working with hex strings
'\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af' is actually:
0x180, 0x224, 0x160, 0x76, 0x72, 0x47, 0x239, 0x79, 0x201, 0x124, 0x121, 0x55, 0x38, 0x34,, 0x65, 0x102

So if someone were to input that,
they would type 180, then 224, then etc,
and you unichr it into one string

b'' would be the best, since you are working with hex strings
'\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af' is actually:
0x180, 0x224, 0x160, 0x76, 0x72, 0x47, 0x239, 0x79, 0x201, 0x124, 0x121, 0x55, 0x38, 0x34,, 0x65, 0x102

So if someone were to input that,
they would type 180, then 224, then etc,
and you unichr it into one string

Okay I can follow a bit,
suppose someone encrypts a text with AES and get as output:\xb4\xe0\xa0lh/\xefo\xc9|y7&"af

After a while the person wants to get the original text and wants to decrypt it with AES, he inserts as text \xb4\xe0\xa0lh/\xefo\xc9|y7&"af and uses the same pass as he previously used. How should my program format the input? It has to give as length 16.

Nevermind, there's the solution:

string='\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'


code = [ord(char) for char in string]

print("This is your code:"," ".join([str(char) for char in code]))

#User inputs the code later:

usercode = input("What's your code, seperated by spaces: ")
usercode = usercode.split(" ")

decode = [chr(int(char)) for char in usercode]
str2 = "".join(decode)

print(string == str2)

God Dammit, I'm a 2.6 programmer ;p
2.6 doesn't support the for loop on the end in the first code part.
You don't know perhaps how it should be in 2.6?

Here's the 2.0 version (I hope):

string='\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'


code = [ord(char) for char in string]

print "This is your code:"," ".join([str(char) for char in code])

#User inputs the code later:

usercode = raw_input("What's your code, seperated by spaces: ")
usercode = usercode.split(" ")

decode = [chr(int(char)) for char in usercode]
str2 = "".join(decode)

print string == str2
## The user inputted string becomes the original
## string at line 1

Here's the 2.0 version (I hope):

string='\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af'


code = [ord(char) for char in string]

print "This is your code:"," ".join([str(char) for char in code])

#User inputs the code later:

usercode = raw_input("What's your code, seperated by spaces: ")
usercode = usercode.split(" ")

decode = [chr(int(char)) for char in usercode]
str2 = "".join(decode)

print string == str2
## The user inputted string becomes the original
## string at line 1

That works perfect :) thanks
there is though one thing I don't get,
Why is in this case the '\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af' interpreted different as in my first code?

That works perfect :) thanks
there is though one thing I don't get,
Why is in this case the '\xb4\xe0\xa0LH/\xefO\xc9|y7&"Af' interpreted different as in my first code?

I really have no idea.
I don't have much experience in encoding strings and stuff like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.