【问题标题】:How to Encrypt and UnEncrypt text in Python [duplicate]如何在 Python 中加密和解密文本 [重复]
【发布时间】:2016-07-07 11:22:35
【问题描述】:

我想知道是否可以加密原始数据,例如:

message = encrypt(raw_input("what is your message")) <---- I want this encrypted

然后在我的服务器代码中,我希望它不加密:

print unencrypt(str(message)) <----- I want this 

我希望它与此类似。如何在 Python 2.7 上做到这一点?

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    您可以使用以下代码:

    import base64
    def encode(key, clear):
        enc = []
        for i in range(len(clear)):
            key_c = key[i % len(key)]
            enc_c = chr((ord(clear[i]) + ord(key_c)) % 256)
            enc.append(enc_c)
        return base64.urlsafe_b64encode("".join(enc))
    
    def decode(key, enc):
        dec = []
        enc = base64.urlsafe_b64decode(enc)
        for i in range(len(enc)):
            key_c = key[i % len(key)]
            dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
            dec.append(dec_c)
        return "".join(dec)
    
    encoded_string=encode('password','text text')
    print encoded_string
    decoded_string=decode('password',encoded_string)
    print decoded_string
    

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 2019-04-20
      • 2013-05-31
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2013-01-02
      • 2015-02-04
      • 2020-08-05
      相关资源
      最近更新 更多