加密/解密RSA

时间:2019-05-27 17:45:34

标签: python encryption cryptography rsa public-key-encryption

我已经实现了一些代码来加密和解密字符串。我选择了

p = 1606938044258990275541962092341162602522202993782792835835611

q = 3213876088517980551083924184682325205044405987565585670603103

当我在下面的函数中输入String时,我得到正确的结果,但前提是String的字符数不超过40个字符。当String的字符数超过40个时,结果如下:

  

ÜŞϟʐͶz̽ć

有人知道出什么问题吗?

from pip._vendor.distlib.compat import raw_input


def xgcd(a, b):
    """return (g, x, y) such that a*x + b*y = g = gcd(a, b)"""
    x0, x1, y0, y1 = 0, 1, 1, 0
    while a != 0:
        q, b, a = b // a, a, b % a
        y0, y1 = y1, y0 - q * y1
        x0, x1 = x1, x0 - q * x1
    return y0


def genKeypair(p, q):
    n = p * q
    phiN = (p - 1) * (q - 1)
    e = 65537

    d = xgcd(phiN, e)
    print("A", n, e, d)
    return n, e, d


# encrypt message and return cipher
def encrypt(m, n, e):
    m1 = ""
    # Turn message string into ascii so it can be used for encryption
    for x in range(len(m)):
        m1 += '{0:03}'.format(ord(m[x]))
    # encrypt
    c = pow(int(m1), e, n)
    print(c)
    return c


# decrypt cipher and return message
def decrypt(c, n, d):
    # decrypt c
    m = pow(c, d, n)
    # put decryption result into ascii format and use ascii to decode it
    m = str(m)
    tmp = ""
    message = ""
    i = 0
    if int(m[0] + m[1] + m[3]) > 255:
        m = "0" + m
    for x in range(len(m)):
        tmp = tmp + m[x]
        i += 1
        if i % 3 == 0:
            message += chr(int(tmp))
            tmp = ""
    return message


def main():
    # Constants of menu
    function = -1
    GENERATE_KEYPAIR = 1
    ENCRYPT = 2
    DECRYPT = 3
    END = 0
    subFunction1 = -1

    p = 0
    q = 0
    # Key = n + exponent
    n = 0
    # Public exponent
    e = 0
    # Private exponent
    d = 0
    # Cipher
    c = 0

    # Mainloop/Main menu
    while function != END:
        print("What do you want to do?\nGenerate Keypair:", GENERATE_KEYPAIR, "\nEncrypt message:", ENCRYPT,
              "\nDecrypt message:", DECRYPT, "\nEnd programm:", END)
        function = int(input())

        if function == GENERATE_KEYPAIR:
            c = 0
        while subFunction1 != END:
            p = int(raw_input("Enter the first primenumber:\n"))
            q = int(raw_input("Enter the secound primenumber\n"))
            n, e, d = genKeypair(p, q)
            print("The keypairs for", p, "and", q, "will be generated")
            subFunction1 = 0

        if function == ENCRYPT:
            if n != 0:
                c = encrypt(raw_input("Enter message you wish to encrypt:\n"), n, e)
                print("Cipher:", c, "\n")
            else:
                print("Need keypair first.\n")
        if function == DECRYPT:
            if c != 0:
                print(c, n, d)
                m = decrypt(c, n, d)
                print("Decrypted message:", m, "\n")
            else:
                print("Need encrypted message first.\n")
        elif function == END:
            print("Ending programm...\n")


main()

0 个答案:

没有答案