2017-10-19 23 views
0

주어진 params {e, n, d를 사용하여 PKCS # 1에서 RSA 개인 키를 생성하기위한 Java (기본 또는 BouncyCastle 공급자) 구현을 찾으려고합니다. }.Java 보안을 사용하여 모듈러스, 공용 및 개인 지수로 RSA 개인 키 복원

이렇게하는 알고리즘을 설명하는 Dan Boneh의 paper이 있습니다. 이 솔루션은 PyCrypto (Python)에서 사용할 수 있으며 SFM 형식 (n, e, d)과 CRT 형식 (p, q, dp, dq, u) 사이의 RSA 키를 변환하는 Mounir IDRASSI가 게시 한 독립 실행 형 utility도 있습니다.), 그리고 그 반대의 경우. 그러나 나는 자바를 사용할 준비가 된 것을 찾을 수 없었다.

업데이트 : 나는 https://github.com/martinpaljak/RSAKeyConverter/blob/master/src/opensc/RSAKeyConverter.java

+0

그뿐만 아니라, 직접 자바의 다른 코드를 보지 않고 단지 논문 일 뿐이지 만 알고리즘을 빌리는 것일 수도 있습니다. 그러나 다른 컴퓨터에 있습니다. 그래서 나는 당신이 CRT로 전환하기를 원한다고 생각합니다. 왜냐하면 당신이 무엇을하고 있는지 분명하지 않기 때문입니다. –

답변

0

에서 이러한 구현을 발견 나는 여기에 재현 this 대답에 몇 가지 코드를 제공 : 내가 구현

/** 
* Find a factor of n by following the algorithm outlined in Handbook of Applied Cryptography, section 
* 8.2.2(i). See http://cacr.uwaterloo.ca/hac/about/chap8.pdf. 
* 
*/ 

private static BigInteger findFactor(BigInteger e, BigInteger d, BigInteger n) { 
    BigInteger edMinus1 = e.multiply(d).subtract(BigInteger.ONE); 
    int s = edMinus1.getLowestSetBit(); 
    BigInteger t = edMinus1.shiftRight(s); 

    for (int aInt = 2; true; aInt++) { 
     BigInteger aPow = BigInteger.valueOf(aInt).modPow(t, n); 
     for (int i = 1; i <= s; i++) { 
      if (aPow.equals(BigInteger.ONE)) { 
       break; 
      } 
      if (aPow.equals(n.subtract(BigInteger.ONE))) { 
       break; 
      } 
      BigInteger aPowSquared = aPow.multiply(aPow).mod(n); 
      if (aPowSquared.equals(BigInteger.ONE)) { 
       return aPow.subtract(BigInteger.ONE).gcd(n); 
      } 
      aPow = aPowSquared; 
     } 
    } 

} 

public static RSAPrivateCrtKey createCrtKey(RSAPublicKey rsaPub, RSAPrivateKey rsaPriv) throws NoSuchAlgorithmException, InvalidKeySpecException { 

    BigInteger e = rsaPub.getPublicExponent(); 
    BigInteger d = rsaPriv.getPrivateExponent(); 
    BigInteger n = rsaPub.getModulus(); 
    BigInteger p = findFactor(e, d, n); 
    BigInteger q = n.divide(p); 
    if (p.compareTo(q) > 1) { 
     BigInteger t = p; 
     p = q; 
     q = t; 
    } 
    BigInteger exp1 = d.mod(p.subtract(BigInteger.ONE)); 
    BigInteger exp2 = d.mod(q.subtract(BigInteger.ONE)); 
    BigInteger coeff = q.modInverse(p); 
    RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, exp1, exp2, coeff); 
    KeyFactory kf = KeyFactory.getInstance("RSA"); 
    return (RSAPrivateCrtKey) kf.generatePrivate(keySpec); 

}