0

I have two strings in python stored in variables a and b. a is an argument passed to the script and b is the result from decoding the result of win32crypt. I've finally used repr to compare them and no surprise the argument comes back as the word 'windows' but b comes back as 'w\x00i\x00n\x00d\x00o\x00w\x00s\x00'. I know that I can probably write some code to get rid of the \x00s and I have found online that this is some way of essentially adding accents to characters but was wondering if there is a better way to convert b to plain text or to convert a to be in the same format that b is in. Below is a basic representation of the code without the imports.

def decrypt(password):
    _, decrypted_password_string = win32crypt.CryptUnprotectData(binascii.unhexlify(password), None, None, None, 0)
    #print(decrypted_password_string.decode())
    return decrypted_password_string.decode()

main(a):
    b = encryptedstringfromcsv
    b=decrypt(b)
    if a == b:
        print('true')
    else:
        print('false')

always prints false, im assuming due to the repr value. I want it to print true

Dihmz
  • 1
  • 3
  • Do no remove any characters when dealing with security (as in your case: password). I just think the API is not well designed for Python, so you may get UTF-16 encoding, and python want own Unicode string. Or maybe you read from cvs with wrong encoding. You should be more explicit on the encoding at every step – Giacomo Catenazzi May 23 '23 at 15:17
  • `b'w\x00i\x00n\x00d\x00o\x00w\x00s\x00'.decode("utf-16")` ---> 'windows' – JonSG May 23 '23 at 15:20
  • You're returning a value from `decrypt` but you're not storing it in a variable anywhere – byxor May 23 '23 at 15:20
  • I see. So the result is UTF-16. I wonder if I just do a encode/decode on a to utf-16 if that will fix it. I generally don't touch encoding so I need to investigate a bit. @byxor, sorry the example code doesn't have the storing of the variable but it is stored in the code. Going to try your resolution Jon, appreciate the quick replies – Dihmz May 23 '23 at 15:21
  • 1
    I added "utf-16" to the decode at the end of the decrypt function. Thank you so much for the help @JonSG – Dihmz May 23 '23 at 15:26
  • @juanpa.arrivillaga: if you tell Python that it is UTF-16. And good Python program will decode to Unicode strings at beginning, and encode when outputing data. We had Python3 to forget all nightmares of keeping track of encodings. And in any case: *explicit is better then implicit*, so always specify the encoding. – Giacomo Catenazzi May 23 '23 at 15:27
  • Thank you giacomo. I need to be a little better with explicit types. Pytjon 3 has spoiled me – Dihmz May 23 '23 at 15:37

2 Answers2

0

@JonSG specified above that I can decode utf-16. Adding that to the decode section in my decrypt function fixed it. Thanks a lot :)

Dihmz
  • 1
  • 3
  • I cant accept my own answer for two days. If someone else answers essentially the same thing I will accept it as the answer. Otherwise I'll go ahead and accept my answer in two days. – Dihmz May 23 '23 at 16:33
0

Looks like the decrypted value in main() isn't actually getting saved anywhere. Try changing

decrypt(b)

to

b = decrypt(b)
  • Hey the rockers. I forgot to add the b= in the simplified code here but edited it. I answered what ended up fixing it but i cant accept that for 2 days. Ty for the response though – Dihmz May 23 '23 at 15:36