Menu

[r103]: / phprpc_2.1 / keygen / keygen_java.cs  Maximize  Restore  History

Download this file

54 lines (53 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.IO;
using Mono.Math;
class KeyGen
{
static void Main(string[] args)
{
StreamWriter sw = new StreamWriter("KeyPairGen.java");
sw.WriteLine("package coolcode.phprpc;");
sw.WriteLine();
sw.WriteLine("import java.lang.*;");
sw.WriteLine("import java.util.*;");
sw.WriteLine();
sw.WriteLine("public class KeyPairGen {");
sw.WriteLine(" private static HashMap[] __keypair = new HashMap[100];");
sw.WriteLine(" private static Random __rand = new Random();");
sw.WriteLine(" private KeyPairGen() {}");
sw.WriteLine(" static {");
BigInteger n1 = new BigInteger(1);
BigInteger n2 = new BigInteger(2);
for (int i = 0; i < 100; i++)
{
bool f = false;
BigInteger q = null, p = null, g = null;
while (!f)
{
q = BigInteger.GeneratePseudoPrime(127);
p = q << 1;
p.SetBit(0);
if (p.IsProbablePrime()) f = true;
}
while (f)
{
g = BigInteger.GenerateRandom(127);
if (g.ModPow(n2, p) != n1 && g.ModPow(q, p) != n1)
{
f = false;
}
}
Console.WriteLine("p=" + p.ToString());
Console.WriteLine("g=" + g.ToString());
sw.WriteLine(" __keypair[" + i.ToString() + "] = new HashMap();");
sw.WriteLine(" __keypair[" + i.ToString() + "].put(\"p\", \"" + p.ToString() + "\");");
sw.WriteLine(" __keypair[" + i.ToString() + "].put(\"g\", \"" + g.ToString() + "\");");
}
sw.WriteLine(" }");
sw.WriteLine(" public static HashMap genRandomKeyPair() {");
sw.WriteLine(" return __keypair[__rand.nextInt(100)];");
sw.WriteLine(" }");
sw.WriteLine("}");
sw.Close();
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.