Click here to Skip to main content
14,634,917 members
Rate this:
Please Sign up or sign in to vote.
See more:
import timeit

   input = raw_input
except NameError:
   chr = unichr
except NameError:
p=int(input('Enter prime p: '))
q=int(input('Enter prime q: '))
print("Choosen primes:\np=" + str(p) + ", q=" + str(q) + "\n")
print("n = p * q = " + str(n) + "\n")
print("Euler's function (totient) [phi(n)]: " + str(phi) + "\n")
def gcd(a, b):
    while b != 0:
        c = a % b
        a = b
        b = c
    return a
def modinv(a, m):
    for x in range(1, m):
        if (a * x) % m == 1:
            return x
    return None
def coprimes(a):
    l = []
    for x in range(2, a):
        if gcd(a, x) == 1 and modinv(x,phi) != None:
    for x in l:
        if x == modinv(x,phi):
    return l
print("Choose an e from a below coprimes array:\n")
print(str(coprimes(phi)) + "\n")

start = timeit.timeit()


print("\nYour public key is a pair of numbers (e=" + str(e) + ", n=" + str(n) + ").\n")
print("Your private key is a pair of numbers (d=" + str(d) + ", n=" + str(n) + ").\n")
def encrypt_block(m):
    c = modinv(m**e, n)
    if c == None: print('No modular multiplicative inverse for block ' + str(m) + '.')
    return c
def decrypt_block(c):
    m = modinv(c**d, n)
    if m == None: print('No modular multiplicative inverse for block ' + str(c) + '.')
    return m
def encrypt_string(s):
    return ''.join([chr(encrypt_block(ord(x))) for x in list(s)])
def decrypt_string(s):
    return ''.join([chr(decrypt_block(ord(x))) for x in list(s)])
s = input("Enter a message to encrypt: ")
print("\nPlain message: " + s + "\n")
enc = encrypt_string(s)
print("Encrypted message: " + enc + "\n")
dec = decrypt_string(enc)
print("Decrypted message: " + dec + "\n")

end = timeit.timeit()

print(end -- start)

What I have tried:

I have tried to encrypt and decrypt some codes with RSA Encryption System. I also put some codes to calculate the time that passed during this process. However, I could not find a way to convert this code from Python to C++ or C#. Could you please help me to convert these codes into C++ or C# ? Thank you in advance.
Updated 8-May-20 7:48am
F-ES Sitecore 8-May-20 10:04am
This isn't a code-conversion site, but looking at the code it isn't complicated, if you understand c++ and c# I'm not sure why you can't convert it yourself. If there is any syntax\functions you don't understand just google what they do and use a c\c# alternative.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Easy, you rewrite it in C++ or C#, whichever you prefer.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You have to completely understand what the Python code is doing and why. Then you have to write equivalent code in either C or C#.

DO NOT ATTEMPT TO DO A LINE-BY-LINE CONVERSION. It usually doesn't go very well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100