Click here to Skip to main content
15,910,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a C# form which has 2 buttons. The first button will generate a matrix , so i need to fill a textbox called rTb. rTb textbox will get a number which be the matrix range. If rTb is 10, so the matrix will randomly generate in range from -10 to 10.But the method is from python script.
C#
Process p = new Process(); // create process (i.e., the python program
                p.StartInfo.FileName = "C:\\Python27\\python.exe";
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false; // make sure we can read the output from stdout
                p.StartInfo.Arguments = "script.py " + rTb.Text; // start the python program with two parameters
                p.Start(); // start the process (the python program)
                StreamReader s = p.StandardOutput;
                String output = s.ReadToEnd();
                string[] r = output.Split(new char[] { ' ' }); // get the parameter
                string q = r[0];
                string[] words = q.Split('.');
                mTb1.Text = words[0];
                mTb2.Text = words[1];
                mTb3.Text = words[2];
                mTb4.Text = words[3];
                mTb5.Text = words[4];
                mTb6.Text = words[5];
                mTb7.Text = words[6];
                mTb8.Text = words[7];
                mTb9.Text = words[8];
                p.WaitForExit();


and the python script
import sys
from random import randrange


# check the get_determinant if 1 or -1
def get_determinant(key):  
    result = (key[0] * ((key[4] * key[8]) - (key[5] * key[7]))) - \
        (key[1] * ((key[3] * key[8]) - (key[5] * key[6]))) + \
        (key[2] * ((key[3] * key[7]) - (key[4] * key[6])))
    return result


# generate random matrix with key range and get_determinant must either 1 or -1 
# for singularity
key_range = int(sys.argv[1])
a = 0
while a == 0:
    key_matrix = []
    for i in range (9): key_matrix.append(randrange(-key_range,key_range))
    if get_determinant(key_matrix) == 1 or get_determinant(key_matrix) == -1: break   
m = ''
for i in key_matrix:
    m = m + str(i) + '.'
print m + str(get_determinant(key_matrix))

No problem with the output in the C# form.
The problem is when i do it with different button, called it RSA button, it'll randomly generate p, q, e, and set it to each textbox. But i need to fill the digitTb for p, q key length. Says it 2, so the p, q digit will get ex: 12,43,23 all in 2 digits. but when i pass the argument
C#
Process p = new Process(); // create process (i.e., the python program
                p.StartInfo.FileName = "C:\\Python27\\python.exe";
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false; // make sure we can read the output from stdout
                p.StartInfo.Arguments = "script2.py" + digitTb.text;// start the python program with two parameters
                p.Start(); // start the process (the python program)
                StreamReader s = p.StandardOutput;
                String output = s.ReadToEnd();
                string[] r = output.Split(new char[] { ' ' }); // get the parameter
                string q = r[0];
                string[] words = q.Split('.');
                pTb.Text = words[0];
                qTb.Text = words[1];
                eTb.Text = words[2];
                p.WaitForExit();


and the python script2

Python
import sys
from random import randrange
from fractions import gcd

# lehmann primality check method
def lehmann(p):
    a = randrange(1, p)
    res = (a ** ((p - 1)/2)) % p
    if res == 1 or res == p - 1: return True
    else: return False

# check a random number p givren on parameter with lehmann
# will return p as prime and none if p is composite
def check_prime(p):  
    error = []    
    for i in range(10):
        if lehmann(p) == False: 
            error.append(0) 
            pass
        else: error.append(1)
    i = 0
    for e in error:
        i += 1
        if e != 1: break
        if i == len(error): return p

a = 10**str(sys.argv[1])
# generate 2 prime number for unique p, q	

prime = []

while len(prime) != 2:
    result = check_prime(randrange(50,a))
    if result != None: prime.append(result)
    
# RSA Scheme	
p = prime[0]
q = prime[1]
n = p * q
totient = ( p - 1 ) * ( q - 1 )

# choose e
e = []
for i in range( 2, n ): 
    if gcd ( i, totient ) == 1: e.append( i )
e = e[0]   
m = ''
for i in prime:
    m = m + str(i) + '.'
print m + str(e)

the error says "Index was outside the bounds of the array". i think the argument cant pass to python script. i will appreciate if anyone help.
Posted

1 solution

you may need space after script2.py
C#
p.StartInfo.Arguments = "script2.py " + digitTb.text;// start the python program with two parameters

and also add few validations
C#
string[] words = q.Split('.');
if(words.Length>0) 
  pTb.Text = words[0];
if(words.Length>1) 
   qTb.Text = words[1];
if(words.Length>2) 
  eTb.Text = words[2];
 
Share this answer
 
Comments
newbiejo 28-Nov-14 23:31pm    
Thanks alot! It's work. I'd appreciate your help DamithSL

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900