0

is there away to solve this problem even the code is work prefect for encrypted and decrypted the data when they are in same file, the problem happen when i divide the code into two parts, one part encryption and second part decryption, but still i'm getting wrong decryption data even i use same public key and n value was generated in encryption part. suppose:

  data='hello'
    p=23
    q=19
    public key=(185,437)
    private key=(533,437)

when i use the public key for decryption the data is wrong!! i have also try use private same also wrong!! any suggestion !

encryption code:

import random

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a


def multiplicative_inverse(e, phi):
    d = 0
    x1 = 0
    x2 = 1
    y1 = 1
    temp_phi = phi

    while e > 0:
        temp1 = temp_phi//e
        temp2 = temp_phi - temp1 * e
        temp_phi = e
        e = temp2

        x = x2- temp1* x1
        y = d - temp1 * y1

        x2 = x1
        x1 = x
        d = y1
        y1 = y

    if temp_phi == 1:
        return d + phi

'''
Tests to see if a number is prime.
'''
def is_prime(num):
    if num == 2:
        return True
    if num < 2 or num % 2 == 0:
        return False
    for n in range(3, int(num**0.5)+2, 2):
        if num % n == 0:
            return False
    return True

def generate_keypair(p, q):
    if not (is_prime(p) and is_prime(q)):
        raise ValueError('Both numbers must be prime.')
    elif p == q:
        raise ValueError('p and q cannot be equal')
    #n = pq
    n = p * q

    #Phi is the totient of n
    phi = (p-1) * (q-1)

    #Choose an integer e such that e and phi(n) are coprime
    e = random.randrange(1, phi)

    #Use Euclid's Algorithm to verify that e and phi(n) are comprime
    g = gcd(e, phi)
    while g != 1:
        e = random.randrange(1, phi)
        g = gcd(e, phi)

    #Use Extended Euclid's Algorithm to generate the private key
    d = multiplicative_inverse(e, phi)

    #Return public and private keypair
    #Public key is (e, n) and private key is (d, n)
    return ((e, n), (d, n))

def encrypt(pk, plaintext):
    #Unpack the key into it's components
    key, n = pk
    #Convert each letter in the plaintext to numbers based on the character using a^b mod m
    cipher = [(ord(char) ** key) % n for char in plaintext]
    #Return the array of bytes
    return cipher


if __name__ == '__main__':

    print ("RSA Encrypter/ Decrypter")
    p =  int(23)
    q = int(19)
    print ("Generating your public/private keypairs now . . .")
    public, private = generate_keypair(p, q)
    print ("Your public key is ", public ," and your private key is ", private)
    message = str('hello')
    encrypted_msg = encrypt(private, message)
    print ("Your encrypted message is: ")
    print (''.join(map(lambda x: str(x), encrypted_msg)))

decryption code:

    def decrypt(k,pk, ciphertext):
    #Unpack the key into its components
    key=k
    n = pk
    #Generate the plaintext based on the ciphertext and key using a^b mod m
    plain = [chr((ord(char) ** key)  % n) for char in ciphertext]
    return ''.join(plain)


if __name__ == '__main__':
    '''
    Detect if the script is being run directly by the user
    '''
    print ("RSA Encrypter/ Decrypter")
    key =  int(533)
    n = int(437)
    public=(key,n)
    message = '271169420420218'

    print ("Decrypting message with public key ", public ," . . .")
    print ("Your message is:")
    print (decrypt(key,n, message))

i'm using python 3.6 spyder

natalie
  • 13
  • 4
  • @James Reinstate Monica Polk ,it's python 3.6, i have try also in python 2.7 same problem in decryption, so i convert to work in python 3.6 – natalie Feb 02 '20 at 06:36
  • @James Reinstate Monica Polk ,so could you please to help me to make it work in python 3, because im getting wrong decryption data when i make 2 file one for encryption and second for decryption – natalie Feb 02 '20 at 14:03
  • i have change the xrange to range to be compatible with python 3, but still the decryption data is wrong – natalie Feb 02 '20 at 14:05
  • @James Reinstate Monica Polk, i have done it and still getting wrong decryption data – natalie Feb 02 '20 at 14:27
  • You can't encode the ciphertext the way you're doing, you have no way to figure out where the number boundaries are when you are ready to decrypt. And your decryption must undo each step of the encryption which it isn't. – President James K. Polk Feb 02 '20 at 18:11
  • so what is require to decrypt the code??, i want one file encryption and second file decryption , by using the public key should be decrypt the data! so whats wrong with code,? – natalie Feb 02 '20 at 19:57
  • I've explained this already. I'm sorry if my comments are not understandable. – President James K. Polk Feb 02 '20 at 21:22
  • could you please tell me what the modify require for decryption part?i understand your comments well, but still don't know from where i should start to modify the code. – natalie Feb 03 '20 at 06:25

0 Answers0