How to encode a string for saving into cookie

Hi, guys,
Do you know how to encode and decode a string with a key,
i want to encode a string and save it to the customize cookie, then get
it
back from this cookie and decode ,
such as ,

str = ‘myString’
key = ‘234d98’

encode(str, key) #maybe output SX23CVX23VXC422V234S
decode(str, key) #myString

The result of encode string is difference when the key is difference .
thanks all.