3

希望对 python 中的短字符串进行 RSA 加密。这是针对我想要存储的一段用户数据,而员工(包括我自己)无法看到它。当我们被传唤时,私钥将在我的保险箱中的拇指驱动器上。

我的问题:是否有用于非对称密钥 RSA 的“可能正确”的 python 包?我会更安全地使用 C 库(如果是的话)。

4

4 回答 4

3

PyCrypto

于 2010-08-29T20:12:30.297 回答
2

pycryptopp

于 2010-08-29T21:22:40.830 回答
1

使用 RSA 加密短字符串可能会出现问题。您可以使用 RSA 加密某些数据,这些数据会显示有关您的私钥的详细信息。在您的情况下,它可能会很好,因为它足够晦涩,您的员工不会弄明白。但在一般情况下,如果您希望对数据保密,则对于知识渊博和/或资金充足的对手,您不希望使用 RSA 直接加密数据。

我建议只使用gnupg。它为您解决了所有这些问题。

于 2010-08-30T01:07:30.560 回答
1
def gcd (a, b):
    "Compute GCD of two numbers"

    if b == 0: return a
    else: return gcd(b, a % b)

def multiplicative_inverse(a, b):
    """ Find multiplicative inverse of a modulo b (a > b)
        using Extended Euclidean Algorithm """

    origA = a
    X = 0
    prevX = 1
    Y = 1
    prevY = 0

    while b != 0:

        temp = b
        quotient = a/b
        b = a % b
        a = temp

        temp = X
        a = prevX - quotient * X
        prevX = temp

        temp = Y
        Y = prevY - quotient * Y
        prevY = temp

    return origA + prevY

def generateRSAKeys(p, q):
    "Generate RSA Public and Private Keys from prime numbers p & q"

    n = p * q
    m = (p - 1) * (q - 1)

    # Generate a number e so that gcd(n, e) = 1, start with e = 3
    e = 3

    while 1:

        if gcd(m, e) == 1: break
        else: e = e + 2

    d = multiplicative_inverse(m, e)   

    # Return a tuple of public and private keys 
    return ((n,e), (n,d))           

if __name__ == "__main__":

    print "RSA Encryption algorithm...."
    p = long(raw_input("Enter the value of p (prime number):"))
    q = long(raw_input("Enter the value of q (prime number):"))

    print "Generating public and private keys...."
    (publickey, privatekey) = generateRSAKeys(p, q)

    print "Public Key (n, e) =", publickey
    print "Private Key (n, d) =", privatekey

    n, e = publickey
    n, d = privatekey

    input_num = long(raw_input("Enter a number to be encrypted:"))
    encrypted_num = (input_num ** e) % n
    print "Encrypted number using public key =", encrypted_num
    decrypted_num = encrypted_num ** d % n
    print "Decrypted (Original) number using private key =", decrypted_num
于 2011-12-26T14:05:34.580 回答