Home > other >  For help! Questions about encrypt!
For help! Questions about encrypt!

Time:11-02

Run error:
Raise ValueError (" Incorrect AES key length bytes (% d) "% len (key))
ValueError: Incorrect AES key length (51 bytes)

 
Token=encrypt (uuid. Uuid4 () hex. The upper (), '\ xd7 \ XDF \ xca2 \ xd0Vhu \ xeb \ x06 \ xa0 \ xba \ n \ xa2 \ x07O \ xc1 \ x8b \ XCF \ x8f2 & amp; T \ xc0 \ x92 \ xc4 \ xa5 \ x0b & gt; \ xb4 \ xe7 \ XBC ')

Def encrypt (x, key, pad=False) :
If the pad:
Pad=16 - (len (x) % 16)
X=x + CRH (pad) * pad
Iv.=OS urandom (16)
Y=AES. New (key. Encode (" utf8 "), the AES. MODE_CBC, iv) # this line error, 32-bit why transcoding becomes a key 51? Isn't the utf8 characters?
Return base64. B64encode (iv + y.e ncrypt (x))

CodePudding user response:

To utf8, depending on the different characters, do different changes, normal character code 0-127) (as if it is the same code, Chinese characters change 2 times the length of the
Look at this example, I take in your KEY,


 

K1='\ x79 \ x0b'
K2=k1. Encode (" utf8 ")
Print (len (k1))
Print (k1)

Print (len (k2))
Print (k2)

K1='\ xd7 \ XDF \ xca \ xd0 \ xeb \ xa0 \ xba \ xa2 \ xc1 \ x8b \ XCF \ x8f \ xc0 \ x92 \ xc4 \ xa5 \ xb4 \ xe7'
K2=k1. Encode (" utf8 ")
Print (len (k1))
Print (k1)

Print (len (k2))
Print (k2)

  • Related