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.
Process p = new Process();
p.StartInfo.FileName = "C:\\Python27\\python.exe";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "script.py " + rTb.Text;
p.Start();
StreamReader s = p.StandardOutput;
String output = s.ReadToEnd();
string[] r = output.Split(new char[] { ' ' });
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
Process p = new Process();
p.StartInfo.FileName = "C:\\Python27\\python.exe";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "script2.py" + digitTb.text;
p.Start();
StreamReader s = p.StandardOutput;
String output = s.ReadToEnd();
string[] r = output.Split(new char[] { ' ' });
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
import sys
from random import randrange
from fractions import gcd
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
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])
prime = []
while len(prime) != 2:
result = check_prime(randrange(50,a))
if result != None: prime.append(result)
p = prime[0]
q = prime[1]
n = p * q
totient = ( p - 1 ) * ( q - 1 )
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.