📄 rsa.java
字号:
import java.io.*;
import java.math.*;
import java.security.*;
import javax.swing.*;
public class RSA
implements Serializable
{
private final static BigInteger one = BigInteger.ONE;
private final static SecureRandom random = new SecureRandom();
private BigInteger privateKey = null;
private BigInteger publicKey = null;
private BigInteger modulus = null;
private BigInteger p1 = null;
private BigInteger p2 = null;
//Ces variables doivent 阾re initialis?pour l'encrytage de donn閑s.
private BigInteger modulusE;
private BigInteger publicKeyE;
private int N;
private BigInteger phi0;
public RSA(int N) //输入想要的密钥的比特N,自动生成大素数和密钥
{
this.N = N;
// generate an N-bit (roughly) public and private key
p1 = BigInteger.probablePrime(N / 2, random); //p1和p2即是两个大素数
p2 = BigInteger.probablePrime(N / 2, random);
phi0 = (p1.subtract(one)).multiply(p2.subtract(one));
modulus = p1.multiply(p2);
setPublicKey();
privateKey = publicKey.modInverse(phi0); //私钥
modulusE = modulus;
publicKeyE = publicKey;
}
/* public RSA(BigInteger inputp1,BigInteger inputp2)
{
N=10;
p1=inputp1;
p2=inputp2;
phi0 = (p1.subtract(one)).multiply(p2.subtract(one));
modulus = p1.multiply(p2);
setPublicKey();
privateKey=publicKey.modInverse(phi0);
modulusE = modulus;
publicKeyE = publicKey;
}
*/
public RSA(BigInteger inputp1,BigInteger inputp2,BigInteger inputpublicKey)
{
N=100;
p1=inputp1;
p2=inputp2;
phi0 = (p1.subtract(one)).multiply(p2.subtract(one));
modulus = p1.multiply(p2);
publicKey=inputpublicKey;
privateKey=publicKey.modInverse(phi0);
modulusE = modulus;
publicKeyE = publicKey;
}
public BigInteger getModulus()
{
return modulus;
}
//renvois la variable public publicKey utilis?par d'autre pour l'encryption
public BigInteger getPublicKey()
{
return publicKey;
}
//Cette variable doit 阾re initialis?pour 阾re en mesure d'encrypt?
//Ces
public void setPublicKey(BigInteger p, BigInteger n)
{
publicKeyE = p;
modulusE = n;
}
/**
* @param xor BigInteger
*
* Cette m閠hode est employer pour encrypt?et pour d閏rypt?les cl閑s
* C'est l'avantage du ou exclusif il sufi de r閜閠?la m阭e opr閞ation pour
* encod?et d閏od?
**/
public void xOrClePrive(BigInteger xor)
{
xor = xor.pow(4);
privateKey = privateKey.xor(xor);
}
/**
* s'assure que privateKey
* 1. n'a aucun autre diviseur que 1
* 2. qu'il est plus grand que le plus grand entre p1 et p2
* 3. qu'il est plus petit que p1*p2
* */
private void setPrivateKey()
{
do
{
privateKey = BigInteger.probablePrime(N / 2, random);
}
while (privateKey.gcd(phi0).intValue() != 1 ||
privateKey.compareTo(modulus) != -1 ||
privateKey.compareTo(p1.max(p2)) == -1);
}
private void setPublicKey()
{
do
{
publicKey = BigInteger.probablePrime(N / 2, random);
}
while (publicKey.gcd(phi0).intValue() != 1 ||
publicKey.compareTo(phi0) != -1 ||
publicKey.compareTo(one) !=1);
}
/*
Encrypte le message avec les cl閟 public
pour l'encryption les cl閟 public doivent etre initialis閑s
le message doit 阾re divis?en paket de N / 8 octects ou bytes
*/
public BigInteger encrypt(BigInteger message)
{
BigInteger rep = null;
String str_message = new String(message.toByteArray());
if (message != null)
{
if (str_message.length() <= (N / 8))
if (publicKeyE != null && modulusE != null &&
message.toByteArray().length < Integer.MAX_VALUE)
rep = message.modPow(publicKeyE, modulusE);
}
return rep;
}
public BigInteger decrypt(BigInteger encrypted)
{
return encrypted.modPow(privateKey, modulus);
}
public String toString()
{
String s = "";
s+="p ="+ p1+"\n";
s+="q ="+ p2+"\n";
s += "publickey = " + publicKey + "\n";
s += "Privatekey = " + privateKey+"\n";
s += "modulus = " + modulus+ "\n";
return s;
}
public static void main( String[] args )
{
BigInteger inming=new BigInteger("19");
BigInteger miwen=null;
BigInteger mingwen=null;
BigInteger inp1=new BigInteger("7");
BigInteger inp2=new BigInteger("17");
BigInteger inpubkey=new BigInteger("5");
RSA rr=new RSA(10);
//RSA rr=new RSA(inp1,inp2);
//RSA rr=new RSA(inp1,inp2,inpubkey);
System.out.println(rr);
miwen=rr.encrypt(inming);
System.out.println("密文:"+miwen);
mingwen=rr.decrypt(miwen);
System.out.println("明文:"+mingwen+"\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -