Skip to content

Instantly share code, notes, and snippets.

@johnelf
Created September 1, 2014 05:41
Show Gist options
  • Save johnelf/0cb40ae9ede3c4d9b844 to your computer and use it in GitHub Desktop.
Save johnelf/0cb40ae9ede3c4d9b844 to your computer and use it in GitHub Desktop.
简单RSA算法
package com.dcl;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static Map int2Char = new HashMap<Integer, Character>() {{
put(0, 'a');
put(1, 'b');
put(2, 'c');
put(3, 'd');
put(4, 'e');
put(5, 'f');
put(6, 'g');
put(7, 'h');
put(8, 'i');
put(9, 'j');
put(10, 'k');
put(11, 'l');
put(12, 'm');
put(13, 'n');
put(14, 'o');
put(15, 'p');
put(16, 'q');
put(17, 'r');
put(18, 's');
put(19, 't');
put(20, 'u');
put(21, 'v');
put(22, 'w');
put(23, 'x');
put(24, 'y');
put(25, 'z');
}};
public static Map char2Int = new HashMap<Character, Integer>() {{
put('a', 0);
put('b', 1);
put('c', 2);
put('d', 3);
put('e', 4);
put('f', 5);
put('g', 6);
put('h', 7);
put('i', 8);
put('j', 9);
put('k', 10);
put('l', 11);
put('m', 12);
put('n', 13);
put('o', 14);
put('p', 15);
put('q', 16);
put('r', 17);
put('s', 18);
put('t', 19);
put('u', 20);
put('v', 21);
put('w', 22);
put('x', 23);
put('y', 24);
put('z', 25);
}};
public static int getSecretKey(int e, int oula) {
int secretKey = 1, n = 1;
while (true) {
if ((oula * n) + 1 == e * secretKey) {
return secretKey;
}
secretKey++;
if ((oula * n) + 1 < e * secretKey) {
n++;
}
}
}
public static void main(String[] args) {
int p, q, N;
p = 7;
q = 11;
N = p * q;
int oula = (p - 1) * (q - 1);
int e = 13;
int d;
d = getSecretKey(e, oula);
System.out.println("p: " + p);
System.out.println("q: " + q);
System.out.println("N: " + N);
System.out.println("oula: " + oula);
System.out.println("e: " + e);
System.out.println("d: " + d);
System.out.println("encoded key is: {" + e + ", " + N + "}");
System.out.println("public key is: {" + d + ", " + N + "}");
System.out.println(encodeMessage("hello", e, N));
System.out.println(decodeMessage(encodeMessage("hello", e, N), d, N));
}
private static String decodeMessage(String encodedMessage, int e, int N) {
StringBuilder sb = new StringBuilder();
int step = getBitNum(N);
int start = 0;
int length = encodedMessage.length();
while (start < length) {
String word = encodedMessage.substring(start, start + step);
BigInteger messageInNum = new BigInteger(word).pow(e).mod(new BigInteger(String.valueOf(N)));
sb.append(int2Char.get(messageInNum.intValue()));
start += step;
}
return sb.toString();
}
private static int getBitNum(int N) {
int num = 1;
while (N / 10 > 0) {
N /= 10;
num++;
}
return num;
}
private static String encodeMessage(String message, int sec, int N) {
StringBuilder sb = new StringBuilder();
int num = getBitNum(N);
for (char ch : message.toCharArray()) {
BigInteger temp = new BigInteger(char2Int.get(ch).toString()).pow(sec).mod(new BigInteger(String.valueOf(N)));
sb.append(String.format("%0" + num + "d", temp.intValue()));
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment