Java 维纳';s攻击RSA
我找到了维纳攻击RSA的代码Java 维纳';s攻击RSA,java,cryptography,rsa,Java,Cryptography,Rsa,我找到了维纳攻击RSA的代码 import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; public class WienerAttack { // Four ArrayList for finding proper p/q which later on for guessing k/dg List<BigInteger>
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class WienerAttack {
// Four ArrayList for finding proper p/q which later on for guessing k/dg
List<BigInteger> d = new ArrayList<BigInteger>();
List<Fraction> a = new ArrayList<Fraction>();
List<BigInteger> p = new ArrayList<BigInteger>();
List<BigInteger> q = new ArrayList<BigInteger>();
Fraction kDdg = new Fraction(BigInteger.ZERO, BigInteger.ONE); // k/dg, D
// means
// "divide"
private BigInteger e;
private BigInteger N;
// get the root of BigInteger number
public static BigInteger sqrt(BigInteger number) {
BigInteger localBigInteger1 = BigInteger.valueOf(0L);
BigInteger localBigInteger2 = localBigInteger1.setBit(2 * number.bitLength());
do {
BigInteger localBigInteger3 = localBigInteger1.add(localBigInteger2);
if (localBigInteger3.compareTo(number) != 1) {
number = number.subtract(localBigInteger3);
localBigInteger1 = localBigInteger3.add(localBigInteger2);
}
localBigInteger1 = localBigInteger1.shiftRight(1);
localBigInteger2 = localBigInteger2.shiftRight(2);
} while (localBigInteger2.bitCount() != 0);
return localBigInteger1;
}
/**
* Attacks given public key and modulos.
*
* @param e
* public key
* @param N
* modulos which needs to be factorized
* @return
*/
public BigInteger attack(BigInteger e, BigInteger N) {
this.e = e;
this.N = N;
int i = 0;
BigInteger temp1;
// This loop keeps going unless the privateKey is calculated or no
// privateKey is generated
// When no privateKey is generated, temp1 == -1
while ((temp1 = step(i)) == null) {
i++;
}
return temp1;
}
public BigInteger step(int iteration) {
if (iteration == 0) {
// initialization for iteration 0
Fraction ini = new Fraction(e, N);
d.add(ini.floor());
a.add(ini.remainder());
p.add(d.get(0));
q.add(BigInteger.ONE);
} else if (iteration == 1) {
// iteration 1
Fraction temp2 = new Fraction(a.get(0).denominator, a.get(0).numerator);
d.add(temp2.floor());
a.add(temp2.remainder());
p.add((d.get(0).multiply(d.get(1))).add(BigInteger.ONE));
q.add(d.get(1));
} else {
if (a.get(iteration - 1).numerator.equals(BigInteger.ZERO)) {
return BigInteger.ONE.negate(); // Finite continued fraction.
// and no proper privateKey
// could be generated. Return -1
}
// go on calculating p and q for iteration i by using formulas
// stating on the paper
Fraction temp3 = new Fraction(a.get(iteration - 1).denominator, a.get(iteration - 1).numerator);
d.add(temp3.floor());
a.add(temp3.remainder());
p.add((d.get(iteration).multiply(p.get(iteration - 1)).add(p.get(iteration - 2))));
q.add((d.get(iteration).multiply(q.get(iteration - 1)).add(q.get(iteration - 2))));
}
// if iteration is even, assign <q0, q1, q2,...,qi+1> to kDdg
if (iteration % 2 == 0) {
if (iteration == 0) {
kDdg = new Fraction(d.get(0).add(BigInteger.ONE), BigInteger.ONE);
} else {
kDdg = new Fraction((d.get(iteration).add(BigInteger.ONE)).multiply(p.get(iteration - 1)).add(
p.get(iteration - 2)), (d.get(iteration).add(BigInteger.ONE)).multiply(q.get(iteration - 1))
.add(q.get(iteration - 2)));
}
}
// if iteration is odd, assign <q0, q1, q2,...,qi> to kDdg
else {
kDdg = new Fraction(p.get(iteration), q.get(iteration));
}
System.out.println(kDdg);
BigInteger phi = e.multiply(kDdg.denominator).subtract(BigInteger.ONE).divide(kDdg.numerator);
BigInteger b = N.subtract(phi).add(BigInteger.ONE);
BigInteger c = N;
BigInteger descriminant = b.multiply(b).subtract(new BigInteger("4").multiply(c));
BigInteger x1 = b.negate().add(sqrt(descriminant)).divide(new BigInteger("2")).abs();
BigInteger x2 = b.negate().subtract(sqrt(descriminant)).divide(new BigInteger("2")).abs();
if (x1.multiply(x2).equals(N)) {
return e.modInverse(phi);
}
**BigInteger edg = this.e.multiply(kDdg.denominator); // get edg from e *
// dg
// dividing edg by k yields a quotient of (p-1)(d-1) and a remainder of
// g
BigInteger fy = (new Fraction(this.e, kDdg)).floor();
BigInteger g = edg.mod(kDdg.numerator);
// get (p+d)/2 and check whether (p+d)/2 is integer or not
BigDecimal pAqD2 = (new BigDecimal(this.N.subtract(fy))).add(BigDecimal.ONE).divide(new BigDecimal("2"));
if (!pAqD2.remainder(BigDecimal.ONE).equals(BigDecimal.ZERO))
return null;
// get [(p-d)/2]^2 and check [(p-d)/2]^2 is a perfect square or not
BigInteger pMqD2s = pAqD2.toBigInteger().pow(2).subtract(N);
BigInteger pMqD2 = sqrt(pMqD2s);
if (!pMqD2.pow(2).equals(pMqD2s))
return null;
// get private key q from edg/eg
BigInteger privateKey = edg.divide(e.multiply(g));
return privateKey;**
}
}
import java.math.BigDecimal;
导入java.math.biginger;
导入java.util.ArrayList;
导入java.util.List;
公共类Wieneratck{
//四个数组列表,用于查找合适的p/q,随后用于猜测k/dg
列表d=新的ArrayList();
列表a=新的ArrayList();
列表p=新的ArrayList();
列表q=新的ArrayList();
分数kDdg=新分数(biginger.ZERO,biginger.ONE);//k/dg,D
//意味着
//“分割”
私有大整数;
私有大整数N;
//获取BigInteger数的根
公共静态BigInteger sqrt(BigInteger编号){
BigInteger LocalBigInteger 1=BigInteger.valueOf(0L);
biginger localbiginger2=localbiginger1.setBit(2*number.bitLength());
做{
biginger localbiginger3=localbiginger1.add(localbiginger2);
如果(LocalBigInteger 3.compareTo(数字)!=1){
number=number.subtract(localBigInteger3);
localbiginger1=localbiginger3.add(localbiginger2);
}
localBigInteger1=localBigInteger1.shiftRight(1);
localBigInteger2=localBigInteger2.shiftRight(2);
}while(localbiginger2.bitCount()!=0);
返回localbiginger1;
}
/**
*给定公钥和模的攻击。
*
*@param e
*公钥
*@param N
*需要分解的模
*@返回
*/
公共BigInteger攻击(BigInteger e,BigInteger N){
这个。e=e;
这个,N=N;
int i=0;
大整数temp1;
//除非计算了私钥或没有私钥,否则此循环将继续
//生成私钥
//当没有生成privateKey时,temp1=-1
而((temp1=步骤(i))==null){
i++;
}
返回temp1;
}
公共大整数步长(整数迭代){
如果(迭代==0){
//迭代0的初始化
分数ini=新分数(e,N);
d、 添加(ini.floor());
a、 添加(ini.rements());
p、 加(d.get(0));
q、 加法(biginger.ONE);
}else if(迭代==1){
//迭代1
分数temp2=新分数(a.get(0).分母,a.get(0).分子);
d、 添加(temp2.floor());
a、 添加(temp2.rements());
p、 加法(d.get(0)、乘法(d.get(1))、加法(biginger.ONE));
q、 添加(d.get(1));
}否则{
if(a.get(迭代-1).分子等于(BigInteger.ZERO)){
返回BigInteger.ONE.negate();//有限连分数。
//而且没有合适的私钥
//无法生成。返回-1
}
//继续用公式计算迭代i的p和q
//写在纸上
分数temp3=新分数(a.get(迭代-1).分母,a.get(迭代-1).分子);
d、 添加(temp3.floor());
a、 添加(temp3.rements());
p、 加法((d.get(迭代)。乘法(p.get(迭代-1))。加法(p.get(迭代-2)));
q、 加法((d.get(迭代)。乘法(q.get(迭代-1))。加法(q.get(迭代-2)));
}
//若迭代是偶数,则分配给kDdg
如果(迭代%2==0){
如果(迭代==0){
kDdg=新分数(d.get(0).add(biginger.ONE),biginger.ONE);
}否则{
kDdg=新分数((d.get(迭代).add(biginger.ONE)).multiply(p.get(迭代-1)).add(
p、 get(迭代-2)),(d.get(迭代).add(biginger.ONE)).multiply(q.get(迭代-1))
.add(q.get(迭代-2));
}
}
//若迭代为奇数,则分配给kDdg
否则{
kDdg=新分数(p.get(迭代),q.get(迭代));
}
系统输出打印LN(kDdg);
BigInteger phi=e.multiply(kDdg.分母)。减法(BigInteger.一)。除法(kDdg.分子);
BigInteger b=N.subtract(φ).add(BigInteger.ONE);
大整数c=N;
BigInteger描述符=b.乘(b).减(新的BigInteger(“4”).乘(c));
biginger x1=b.negate().add(sqrt(descriminant)).divide(新的biginger(“2”)).abs();
BigInteger x2=b.否定().减法(sqrt(描述符)).除法(新的BigInteger(“2”)).abs();
如果(x1.乘(x2).等于(N)){
返回e.modInverse(phi);
}
**BigInteger edg=this.e.multiply(kDdg.densor);//从e获取edg*
//危险品
//将edg除以k得到商(p-1)(d-1)和余数
//g
BigInteger fy=(新分数(this.e,kDdg)).floor();
BigInteger g=边缘模(kDdg分子);
//获取(p+d)/2并检查(p+d)/2是否为整数
BigDecimal pAqD2=(新的BigDecimal(this.N.subtract(fy))).add(BigDecimal.ONE).divide(新的BigDecimal(“2”));
如果(!pAqD2.余数(BigDecimal.ONE).等于(BigDecimal.ZERO))
返回null;
//获取[(p-d)/2]^2并检查[(p-d)/2]^2是否为完美正方形
大整数pMqD2s=pAqD2.toBigInteger().pow(2).减法(N);
大整数pMqD2=sqrt(pMqD2s);
如果(!pMqD2.pow(2).等于(pMqD2s))
返回null;
//从edg/eg获取私钥q
BigInteger privateKey=edg.除法(如乘法(g));
返回私钥**
}
}
在粗体代码中,他们做了一些额外的测试,比如x1.multiply(x2).equals(N),这意味着N被分解
我把下面的代码:
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class WienerAttack {
// Four ArrayList for finding proper p/q which later on for guessing k/dg
List<BigInteger> d = new ArrayList<BigInteger>();
List<Fraction> a = new ArrayList<Fraction>();
List<BigInteger> p = new ArrayList<BigInteger>();
List<BigInteger> q = new ArrayList<BigInteger>();
Fraction kDdg = new Fraction(BigInteger.ZERO, BigInteger.ONE); // k/dg, D
// means
// "divide"
private BigInteger e;
private BigInteger N;
// get the root of BigInteger number
public static BigInteger sqrt(BigInteger number) {
BigInteger localBigInteger1 = BigInteger.valueOf(0L);
BigInteger localBigInteger2 = localBigInteger1.setBit(2 * number
.bitLength());
do {
BigInteger localBigInteger3 = localBigInteger1
.add(localBigInteger2);
if (localBigInteger3.compareTo(number) != 1) {
number = number.subtract(localBigInteger3);
localBigInteger1 = localBigInteger3.add(localBigInteger2);
}
localBigInteger1 = localBigInteger1.shiftRight(1);
localBigInteger2 = localBigInteger2.shiftRight(2);
} while (localBigInteger2.bitCount() != 0);
return localBigInteger1;
}
/**
* Attacks given public key and modulos.
*
* @param e
* public key
* @param N
* modulos which needs to be factorized
* @return
*/
public BigInteger attack(BigInteger e, BigInteger N) {
this.e = e;
this.N = N;
int i = 0;
BigInteger temp1;
// This loop keeps going unless the privateKey is calculated or no
// privateKey is generated
// When no privateKey is generated, temp1 == -1
while ((temp1 = step(i)) == null) {
i++;
}
return temp1;
}
public BigInteger step(int iteration) {
if (iteration == 0) {
// initialization for iteration 0
Fraction ini = new Fraction(e, N);
d.add(ini.floor());
a.add(ini.remainder());
p.add(d.get(0));
q.add(BigInteger.ONE);
} else if (iteration == 1) {
// iteration 1
Fraction temp2 = new Fraction(a.get(0).denominator,
a.get(0).numerator);
d.add(temp2.floor());
System.out.println("d = " + temp2.floor());
a.add(temp2.remainder());
System.out.println("remainder = " + temp2.remainder());
p.add((d.get(0).multiply(d.get(1))).add(BigInteger.ONE));
System.out.println("p = "
+ (d.get(0).multiply(d.get(1))).add(BigInteger.ONE));
q.add(d.get(1));
System.out.println("q = " + d.get(1));
} else {
if (a.get(iteration - 1).numerator.equals(BigInteger.ZERO)) {
return BigInteger.ONE.negate(); // Finite continued fraction.
// and no proper privateKey
// could be generated. Return -1
}
// go on calculating p and q for iteration i by using formulas
// stating on the paper
Fraction temp3 = new Fraction(a.get(iteration - 1).denominator,
a.get(iteration - 1).numerator);
d.add(temp3.floor());
a.add(temp3.remainder());
p.add((d.get(iteration).multiply(p.get(iteration - 1)).add(p
.get(iteration - 2))));
q.add((d.get(iteration).multiply(q.get(iteration - 1)).add(q
.get(iteration - 2))));
}
// if iteration is even, assign <q0, q1, q2,...,qi+1> to kDdg
if (iteration % 2 == 0) {
if (iteration == 0) {
kDdg = new Fraction(d.get(0).add(BigInteger.ONE),
BigInteger.ONE);
} else {
kDdg = new Fraction((d.get(iteration).add(BigInteger.ONE))
.multiply(p.get(iteration - 1)).add(
p.get(iteration - 2)),
(d.get(iteration).add(BigInteger.ONE)).multiply(
q.get(iteration - 1)).add(q.get(iteration - 2)));
}
}
// if iteration is odd, assign <q0, q1, q2,...,qi> to kDdg
else {
kDdg = new Fraction(p.get(iteration), q.get(iteration));
}
System.out.println(kDdg);
BigInteger phi = e.multiply(kDdg.denominator).subtract(BigInteger.ONE)
.divide(kDdg.numerator); // from formula phi = (ed -1) / t
BigInteger b = N.subtract(phi).add(BigInteger.ONE);
BigInteger c = N;
BigInteger descriminant = b.multiply(b).subtract(
new BigInteger("4").multiply(c));
BigInteger x1 = b.negate().add(sqrt(descriminant))
.divide(new BigInteger("2")).abs();
BigInteger x2 = b.negate().subtract(sqrt(descriminant))
.divide(new BigInteger("2")).abs();
if (x1.multiply(x2).equals(N)) {
return e.modInverse(phi); // private key found
} else {
return null;
}
}
import java.math.BigDecimal;
导入java.math.biginger;
导入java.util.ArrayList;
导入java.util.List;
公共类Wieneratck{
//四个数组列表,用于查找合适的p/q,随后用于猜测k/dg
列表d=新的ArrayList();
列表a=新的ArrayList();
列表p=新的ArrayList();
列表q=新的ArrayList();
分数kDdg=新分数(biginger.ZERO,biginger.ONE);//k/dg,D
//意味着