Skip to content

Commit 83d440d

Browse files
committed
minirsa library functions
1 parent 3d0541c commit 83d440d

File tree

11 files changed

+287
-287
lines changed

11 files changed

+287
-287
lines changed
Binary file not shown.

.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs

Lines changed: 4 additions & 285 deletions
Large diffs are not rendered by default.

.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Wed Apr 18 17:54:47 EDT 2012
1+
#Wed Apr 18 17:57:27 EDT 2012
22
useQuickDiffPrefPage=true
33
proposalOrderMigrated=true
44
tabWidthPropagated=true
@@ -9,6 +9,7 @@ useAnnotationsPrefPage=true
99
org.eclipse.jdt.ui.editor.tab.width=
1010
org.eclipse.jface.textfont=1|Consolas|11.25|0|WINDOWS|1|-15|0|0|0|400|0|0|0|0|3|2|1|49|Consolas;
1111
org.eclipse.jdt.ui.formatterprofiles.version=12
12+
content_assist_number_of_computers=16
1213
formatter_settings_version=12
1314
formatter_profile=_a
1415
spelling_locale_initialized=true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
3+
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
4+
<listEntry value="/MiniRSA Chat/src/minirsa/MiniRSATest.java"/>
5+
</listAttribute>
6+
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
7+
<listEntry value="1"/>
8+
</listAttribute>
9+
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
10+
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
11+
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
12+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="minirsa.MiniRSATest"/>
13+
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="MiniRSA Chat"/>
14+
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value=""/>
15+
</launchConfiguration>

.metadata/.plugins/org.eclipse.epp.usagedata.recording/usagedata.csv

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,28 @@ started,bundle,org.eclipse.wb.discovery.ui,1.1.0.r37x201109091028,"org.eclipse.w
9999
started,bundle,org.eclipse.debug.core,3.7.0.v20110518,"org.eclipse.debug.core",1334785989064
100100
started,bundle,org.eclipse.jdt.launching,3.6.1.v20110803_r371,"org.eclipse.jdt.launching",1334785989090
101101
deactivated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334785990825
102+
started,bundle,org.eclipse.core.filesystem,1.3.100.v20110423-0524,"org.eclipse.core.filesystem",1334785998946
103+
started,bundle,org.eclipse.jgit,1.1.0.201109151100-r,"org.eclipse.jgit",1334785999864
104+
started,bundle,org.eclipse.egit.core,1.1.0.201109151100-r,"org.eclipse.egit.core",1334785999895
105+
started,bundle,org.eclipse.egit.ui,1.1.0.201109151100-r,"org.eclipse.egit.ui",1334785999980
106+
started,bundle,org.eclipse.jdt.junit,3.7.0.v20110505-0800,"org.eclipse.jdt.junit",1334786003687
107+
started,bundle,org.eclipse.jdt.junit.core,3.7.0.v20110518-0800,"org.eclipse.jdt.junit.core",1334786003737
108+
started,bundle,org.eclipse.equinox.simpleconfigurator.manipulator,2.0.0.v20110502-1955,"org.eclipse.equinox.simpleconfigurator.manipulator",1334786003754
109+
started,bundle,org.eclipse.equinox.frameworkadmin,2.0.0.v20110502-1955,"org.eclipse.equinox.frameworkadmin",1334786003758
110+
started,bundle,org.eclipse.ltk.core.refactoring,3.5.201.r371_v20110824-0800,"org.eclipse.ltk.core.refactoring",1334786008193
111+
started,bundle,org.eclipse.ltk.ui.refactoring,3.6.0.v20110505-0800,"org.eclipse.ltk.ui.refactoring",1334786008196
112+
activated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786009710
113+
started,bundle,org.eclipse.jdt.apt.core,3.3.500.v20110420-1015,"org.eclipse.jdt.apt.core",1334786010274
114+
started,bundle,org.eclipse.jdt.apt.pluggable.core,1.0.400.v20110305-1450,"org.eclipse.jdt.apt.pluggable.core",1334786010285
115+
deactivated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786017213
116+
activated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786086927
117+
executed,command,org.eclipse.ui,3.7.0.I20110602-0100,"org.eclipse.ui.window.preferences",1334786087110
118+
deactivated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786091639
119+
activated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786094091
120+
deactivated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786095125
121+
activated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786095796
122+
deactivated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786108520
123+
activated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786129519
124+
deactivated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786130013
125+
activated,workbench,org.eclipse.ui.workbench,3.7.0.I20110519-0100,"",1334786208999
126+
started,bundle,org.eclipse.compare.core,3.5.200.I20110208-0800,"org.eclipse.compare.core",1334786216894

.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ INDEX VERSION 1.126+D:\University of Pennsylvania\2011-2012 Spring Semester\CIT
1010
1115467115.index
1111
30765161.index
1212
3065916431.index
13-
3270485571.index
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testrun name="MiniRSATest" project="MiniRSA Chat" tests="7" started="7" failures="0" errors="0" ignored="0">
3+
<testsuite name="minirsa.MiniRSATest" time="0.045">
4+
<testcase name="testEndecrypt" classname="minirsa.MiniRSATest" time="0.003"/>
5+
<testcase name="testModInverse" classname="minirsa.MiniRSATest" time="0.001"/>
6+
<testcase name="testTotientFromPrimes" classname="minirsa.MiniRSATest" time="0.0"/>
7+
<testcase name="testGCD" classname="minirsa.MiniRSATest" time="0.008"/>
8+
<testcase name="testCoprime" classname="minirsa.MiniRSATest" time="0.03"/>
9+
<testcase name="testModulo" classname="minirsa.MiniRSATest" time="0.001"/>
10+
<testcase name="testCrackKey" classname="minirsa.MiniRSATest" time="0.001"/>
11+
</testsuite>
12+
</testrun>
2.49 KB
Binary file not shown.
2.23 KB
Binary file not shown.

MiniRSA Chat/src/minirsa/MiniRSA.java

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package minirsa;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* This class provides static library functions for the RSA algorithm.
7+
*
8+
* @author Robert Li
9+
* @version April 17, 2012
10+
*/
11+
public class MiniRSA {
12+
13+
/**
14+
* Picks a random integer that is coprime to x, greater than 1, and less than x.
15+
*
16+
* @param x The integer for which a coprime integer is selected.
17+
* @return An integer that is coprime to x, greater than 1, and less than x.
18+
*/
19+
public static long coprime(long x) {
20+
Random rand = new Random();
21+
long result = rand.nextLong() % x;
22+
while (result <= 1 || GCD(x, result) != 1) {
23+
result = rand.nextLong() % x;
24+
}
25+
return result;
26+
}
27+
28+
/**
29+
* Encrypts the integer msg using the RSA key (key, c).
30+
*
31+
* @param msg The message to be encrypted.
32+
* @param key The exponent in the RSA key.
33+
* @param c The modulus in the RSA key.
34+
* @return The encrypted message.
35+
*/
36+
public static long endecrypt(int msg, long key, long c) {
37+
return modulo(msg, key, c);
38+
}
39+
40+
/**
41+
* Calculates the greatest common divisor of a and b.
42+
*
43+
* @param a
44+
* @param b
45+
* @return The greatest common divisor of a and b, or -1 if a and b are
46+
* both 0.
47+
*/
48+
public static long GCD(long a, long b) {
49+
// Implemented using the Euclidean Algorithm. (Ugh CIS 160...)
50+
if (a == 0 && b == 0) {
51+
return 0;
52+
}
53+
long x = Math.abs(a);
54+
long y = Math.abs(b);
55+
long r;
56+
while (y > 0) {
57+
r = x % y;
58+
x = y;
59+
y = r;
60+
}
61+
return x;
62+
}
63+
64+
/**
65+
* Calculates the modular inverse <code>base<super>-1</super> % m</code>.
66+
*
67+
* @param base
68+
* @param m
69+
* @return The modular inverse of base (mod m).
70+
*/
71+
public static long modInverse(long base, long m) {
72+
long x = 0, y = 1, lastX = 1, lastY = 0, mod = m;
73+
long quotient, temp;
74+
while (mod != 0) {
75+
quotient = base / mod;
76+
77+
temp = base;
78+
base = mod;
79+
mod = temp % mod;
80+
81+
temp = x;
82+
x = lastX - quotient * x;
83+
lastX = temp;
84+
85+
temp = y;
86+
y = lastY - quotient * y;
87+
lastY = temp;
88+
}
89+
if (lastX < 0) {
90+
lastX += m;
91+
}
92+
return lastX;
93+
}
94+
95+
/**
96+
* Computes a<super>b</super> % c for large values of a, b, and c.
97+
*
98+
* @param a
99+
* @param b
100+
* @param c
101+
* @return a<super>b</super> % c.
102+
*/
103+
public static long modulo(long a, long b, long c) {
104+
// TODO is there a better algo?
105+
long result = 1;
106+
for (long i = 0; i < b; i++) {
107+
result = (result * a) % c;
108+
}
109+
return result;
110+
}
111+
112+
/**
113+
* Computes Euler's totient of the product of primes p and q.
114+
*
115+
* @param p A prime number.
116+
* @param q A prime number.
117+
* @return Euler's totient of n = p * q.
118+
*/
119+
public static long totient(long p, long q) {
120+
return (p - 1) * (q - 1);
121+
}
122+
123+
/**
124+
* Generates an RSA public/private key pair using primes p and q.
125+
*
126+
* @param p A prime number.
127+
* @param q A prime number.
128+
* @return A 3-element array <code>a</code>, where <code>a[0]</code> is the
129+
* public exponent, <code>a[1]</code> is the private exponent, and
130+
* <code>a[2]</code> is the modulus.
131+
*/
132+
public static long[] generateKey(long p, long q) {
133+
long n = p * q;
134+
long m = totient(p, q);
135+
long e = coprime(m);
136+
long d = modInverse(e, m);
137+
System.out.println("Public: (" + e + ", " + n + "); Private: (" +
138+
d + ", " + n + ").");
139+
return new long[] {e, d, n};
140+
}
141+
142+
/**
143+
* Given a public key (e, n), returns the private exponent d. Beware, this
144+
* can take a really <i>really</i> long time...
145+
*
146+
* @param e The public exponent.
147+
* @param n The modulus.
148+
* @return The private exponent.
149+
*/
150+
public static long crackKey(long e, long n) {
151+
long q = -1;
152+
long p;
153+
for (p = 2; p < n / 2; p++) {
154+
// find factors of n
155+
q = n / p;
156+
if (p * q == n) {
157+
break;
158+
}
159+
}
160+
if (q == -1) {
161+
return -1;
162+
}
163+
long m = totient(p, q);
164+
return modInverse(e, m);
165+
}
166+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package minirsa;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class MiniRSATest {
8+
9+
@Test
10+
public void testCoprime() {
11+
for (int i = 0; i < 1000; i++) {
12+
long coprime1 = MiniRSA.coprime(1234);
13+
assertTrue("Coprime: 1234 -> " + coprime1,
14+
MiniRSA.GCD(1234, coprime1) == 1);
15+
16+
long coprime2 = MiniRSA.coprime(8470);
17+
assertTrue("Coprime: 1234 -> " + coprime2,
18+
MiniRSA.GCD(8470, coprime2) == 1);
19+
}
20+
}
21+
22+
@Test
23+
public void testEndecrypt() {
24+
assertEquals(1148, MiniRSA.endecrypt(72, 451, 2623));
25+
assertEquals(326, MiniRSA.endecrypt(101, 451, 2623));
26+
assertEquals(2041, MiniRSA.endecrypt(33, 451, 2623));
27+
}
28+
29+
@Test
30+
public void testGCD() {
31+
assertEquals(0, MiniRSA.GCD(0, 0));
32+
assertEquals(1, MiniRSA.GCD(0, 1));
33+
assertEquals(13, MiniRSA.GCD(169, 91));
34+
assertEquals(14, MiniRSA.GCD(2842, 12698));
35+
assertEquals(1, MiniRSA.GCD(451, 2520));
36+
}
37+
38+
@Test
39+
public void testModInverse() {
40+
assertEquals(47, MiniRSA.modInverse(23, 120));
41+
assertEquals(1531, MiniRSA.modInverse(451, 2520));
42+
assertEquals(2753, MiniRSA.modInverse(17, 3120));
43+
}
44+
45+
@Test
46+
public void testModulo() {
47+
assertEquals(1148, MiniRSA.modulo(72, 451, 2623));
48+
assertEquals(326, MiniRSA.modulo(101, 451, 2623));
49+
assertEquals(2041, MiniRSA.modulo(33, 451, 2623));
50+
}
51+
52+
@Test
53+
public void testTotientFromPrimes() {
54+
assertEquals(2520, MiniRSA.totient(43, 61));
55+
assertEquals(192, MiniRSA.totient(13, 17));
56+
}
57+
58+
@Test
59+
public void testCrackKey() {
60+
assertEquals(1531, MiniRSA.crackKey(451, 2623));
61+
}
62+
63+
}

0 commit comments

Comments
 (0)