Skip to content

Instantly share code, notes, and snippets.

@MainasuK
Created May 16, 2017 09:00
Show Gist options
  • Save MainasuK/8d238efebc3b30116340e6d07e269ecf to your computer and use it in GitHub Desktop.
Save MainasuK/8d238efebc3b30116340e6d07e269ecf to your computer and use it in GitHub Desktop.
Simple RSA demo for learning purpose.
import java.io.*;
import java.math.BigInteger;
import java.security.SecureRandom;
/**
* Created by MainasuK on 2017-5-9.
*/
public class RSA {
// public key
private BigInteger n;
private BigInteger e;
// private key
private BigInteger d;
public RSA(int length) {
SecureRandom secureRandom = new SecureRandom();
// Choose prime p and q
BigInteger p = BigInteger.probablePrime(length, secureRandom);
BigInteger q = BigInteger.probablePrime(length, secureRandom);
// n = q * p
this.n = p.multiply(q);
// phi = (p - 1) * (q - 1)
BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
// Choose e which gcd(e, phi) is 1 and great than 1 & less than phi
do {
this.e = new BigInteger(phi.bitLength(), secureRandom);
} while (e.compareTo(BigInteger.ONE) <= 0 || e.compareTo(phi) >= 0 ||
!e.gcd(phi).equals(BigInteger.ONE));
// Choose d which de = 1 (mod phi), it's must exists due to gcd(e, phi) = 1
this.d = e.modInverse(phi);
}
public void encode(File plaintextFile, File encryptFile) throws IOException {
encode(plaintextFile, encryptFile, this.e, this.n);
}
public void encode(File plaintextFile, File encryptFile, BigInteger keyE, BigInteger keyN) throws IOException {
System.out.println("对以下内容进行加密,公钥 (e, n) " + keyE + ", " + keyN + "): ");
conver(plaintextFile, encryptFile, keyE, keyN);
System.out.println("");
}
public void decode(File encryptFile, File decodeFile) throws IOException {
decode(encryptFile, decodeFile, this.d, this.n);
}
public void decode(File encryptFile, File decodeFile, BigInteger keyD, BigInteger keyN) throws IOException {
System.out.println("对以下内容进行解密,私钥 (d, n) " + keyD + ", " + keyN + "): ");
conver(encryptFile, decodeFile, keyD, keyN);
System.out.println("");
}
private void conver(File inputFile, File outputFile, BigInteger exponent, BigInteger mod) throws IOException {
FileOutputStream fos = new FileOutputStream(outputFile);
if (!outputFile.exists()) {
outputFile.createNewFile();
}
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] fromBytes = new byte[(int) inputFile.length()];
inputStream.read(fromBytes);
System.out.println("--------- 源文件 ---------");
System.out.println(new String(fromBytes, "UTF-8"));
System.out.println("--------- 结束 ---------");
BigInteger convertContent = new BigInteger(fromBytes).modPow(exponent, mod);
String content = new String(convertContent.toByteArray(), "UTF-8");
System.out.println("--------- 目标文件 ---------");
System.out.println(content);
System.out.println("--------- 结束 ---------");
fos.write(convertContent.toByteArray());
fos.close();
}
public BigInteger getKeyE() {
return e;
}
public BigInteger getKeyN() {
return n;
}
public static void main(String args[]) throws IOException {
RSA rsa = new RSA(100);
File plaintextFile = new File("plain.txt");
if (!plaintextFile.exists()) {
plaintextFile.createNewFile();
FileWriter writer = new FileWriter(plaintextFile);
writer.append("This is plaintext");
writer.flush();
}
File encryptFile = new File("encrypt.txt");
File decodeFile = new File("decode.txt");
rsa.encode(plaintextFile, encryptFile);
rsa.decode(encryptFile, decodeFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment