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 に答える