本文介绍了根据密码对字符串进行编码的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python是否具有使用密码编码/解码字符串的内置简单方法?

Does Python have a built-in, simple way of encoding/decoding strings using a password?

这样做:

>>> encode('John Doe', password = 'mypass')
'sjkl28cn2sx0'
>>> decode('sjkl28cn2sx0', password = 'mypass')
'John Doe'

所以字符串John Doe被加密为sjkl28cn2sx0。要获取原始字符串,我将使用密钥'mypass'解锁该字符串,这是我的源代码中的一个密码。我想要这样,我可以使用密码加密/解密Word文档。

So the string "John Doe" gets encrypted as 'sjkl28cn2sx0'. To get the original string, I would "unlock" that string with the key 'mypass', which is a password in my source code. I'd like this to be the way I can encrypt/decrypt a Word document with a password.

我想使用这些加密字符串作为URL参数。我的目标是混淆,安全性不强;没有任何关键的任务被编码。我意识到我可以使用数据库表来存储键和值,但是我试图是极简的。

I would like to use these encrypted strings as URL parameters. My goal is obfuscation, not strong security; nothing mission critical is being encoded. I realize I could use a database table to store keys and values, but am trying to be minimalist.

推荐答案

假设你是只寻求简单的混淆,这将掩盖从非常休闲观察者的东西,而您不是想使用第三方库。我会推荐像Vigenere密码。它是最古老的密码之一。

Assuming you are only looking for simple obfuscation that will obscure things from the very casual observer, and you aren't looking to use third party libraries. I'd recommend something like the Vigenere cipher. It is one of the strongest of the simple ancient ciphers.

实现起来很简单。一些东西:

It's quick and easy to implement. Something like:

import base64

def encode(key, string):
    encoded_chars = []
    for i in xrange(len(string)):
        key_c = key[i % len(key)]
        encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
        encoded_chars.append(encoded_c)
    encoded_string = "".join(encoded_chars)
    return base64.urlsafe_b64encode(encoded_string)

除了你减去密钥,解码几乎是一样的。

Decode is pretty much the same, except you subtract the key.

如果您编码的字符串很短,和/或如果很难猜出所使用的密码短语的长度,则更难打破。

It is much harder to break if the strings you are encoding are short, and/or if it is hard to guess the length of the passphrase used.

如果您正在寻找加密的东西,PyCrypto可能是您最好的选择,尽管以前的答案忽略了一些细节:PyCyrpto中的ECB模式需要您的消息是16的倍数长度字符。所以,你必须垫。另外,如果要将它们用作URL参数,请使用 base64.urlsafe_b64_encode(),而不是标准的。这取代了base64字母表中的几个字符,其中包含URL安全字符(如其名称所示)。

If you are looking for something cryptographic, PyCrypto is probably your best bet, though previous answers overlook some details: ECB mode in PyCyrpto requires your message to be a multiple of 16 characters in length. So, you must pad. Also, if you want to use them as URL parameters, use base64.urlsafe_b64_encode(), rather than the standard one. This replaces a few of the characters in the base64 alphabet with URL-safe characters (as it's name suggests).

但是,您应该绝对确定这个在使用之前,非常薄层的混淆就足以满足您的需求。我链接的维基百科文章提供了打破密码的详细说明,所以任何一个具有适度决心的人都可以轻易地破坏它。

However, you should be ABSOLUTELY certain that this very thin layer of obfuscation suffices for your needs before using this. The Wikipedia article I linked to provides detailed instructions for breaking the cipher, so anyone with a moderate amount of determination could easily break it.

这篇关于根据密码对字符串进行编码的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:35
查看更多