Browse Source

add the standard public exponent form in RSAUtils

tags/1.0.0
zhanglin33 6 years ago
parent
commit
66a58d44af
4 changed files with 49 additions and 20 deletions
  1. +16
    -0
      source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/utils/classic/RSAUtils.java
  2. +1
    -3
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/CertTest.java
  3. +13
    -17
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/RSAUtilsTest.java
  4. +19
    -0
      source/crypto/crypto-pki/src/main/java/com/jd/blockchain/crypto/CSRBuilder.java

+ 16
- 0
source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/utils/classic/RSAUtils.java View File

@@ -49,6 +49,7 @@ public class RSAUtils {
private static final int QINV_LENGTH = 1024 / 8;

private static final BigInteger PUBEXP_0X03 = BigInteger.valueOf(0x03);
private static final BigInteger PUBEXP_0X010001 = BigInteger.valueOf(0x010001);

private static final BigInteger VERSION_2PRIMES = BigInteger.valueOf(0);

@@ -71,6 +72,21 @@ public class RSAUtils {
}

public static AsymmetricCipherKeyPair generateKeyPair(SecureRandom random){
AsymmetricCipherKeyPairGenerator kpGen = new RSAKeyPairGenerator();
kpGen.init(new RSAKeyGenerationParameters(PUBEXP_0X010001, random, KEYSIZEBITS, CERTAINTY));
return kpGen.generateKeyPair();
}

/**
* key pair generation with short public exponent, resulting in verifying and encrypting more efficiently
*
* @return key pair
*/
public static AsymmetricCipherKeyPair generateKeyPair_shortExp(){
return generateKeyPair(new SecureRandom());
}

public static AsymmetricCipherKeyPair generateKeyPair_shortExp(SecureRandom random){
AsymmetricCipherKeyPairGenerator kpGen = new RSAKeyPairGenerator();
kpGen.init(new RSAKeyGenerationParameters(PUBEXP_0X03, random, KEYSIZEBITS, CERTAINTY));
return kpGen.generateKeyPair();


+ 1
- 3
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/CertTest.java View File

@@ -50,9 +50,7 @@ public class CertTest {
String name = cert.getIssuerX500Principal().getName();
System.out.println(name);
bIn.close();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (CertificateException | IOException e) {
e.printStackTrace();
}
return dn;


+ 13
- 17
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/utils/classic/RSAUtilsTest.java View File

@@ -33,10 +33,20 @@ import static org.junit.Assert.*;
public class RSAUtilsTest {

@Test
public void generateKeyPairTest() throws IOException {
public void generateKeyPairTest() {
AsymmetricCipherKeyPair kp = RSAUtils.generateKeyPair();
RSAKeyParameters pubKey = (RSAKeyParameters) kp.getPublic();
RSAPrivateCrtKeyParameters privKey = (RSAPrivateCrtKeyParameters) kp.getPrivate();
keyPairTest(kp);
}

@Test
public void generateKeyPair_ShortExpTest() {
AsymmetricCipherKeyPair kp = RSAUtils.generateKeyPair_shortExp();
keyPairTest(kp);
}

private void keyPairTest(AsymmetricCipherKeyPair keyPair) {
RSAKeyParameters pubKey = (RSAKeyParameters) keyPair.getPublic();
RSAPrivateCrtKeyParameters privKey = (RSAPrivateCrtKeyParameters) keyPair.getPrivate();

byte[] pubKeyBytes_RawKey = RSAUtils.pubKey2Bytes_RawKey(pubKey);
byte[] pubKeyBytesConverted_RawKey =
@@ -48,9 +58,6 @@ public class RSAUtilsTest {
RSAUtils.privKey2Bytes_RawKey(RSAUtils.bytes2PrivKey_RawKey(privKeyBytes_RawKey));
assertArrayEquals(privKeyBytes_RawKey,privKeyBytesConverted_RawKey);

System.out.println(pubKeyBytes_RawKey.length);
System.out.println(privKeyBytes_RawKey.length);

byte[] pubKeyBytes_PKCS1 = RSAUtils.pubKey2Bytes_PKCS1(pubKey);
byte[] pubKeyBytesConverted_PKCS1 =
RSAUtils.pubKey2Bytes_PKCS1(RSAUtils.bytes2PubKey_PKCS1(pubKeyBytes_PKCS1));
@@ -65,17 +72,6 @@ public class RSAUtilsTest {
byte[] pubKeyBytesConverted_PKCS8 =
RSAUtils.pubKey2Bytes_PKCS8(RSAUtils.bytes2PubKey_PKCS8(pubKeyBytes_PKCS8));
assertArrayEquals(pubKeyBytes_PKCS8,pubKeyBytesConverted_PKCS8);
//
//// String str = "1.2.840.113549.1.1";
//// System.out.println(Hex.toHexString(str.getBytes()));
// byte[] bytes = null;
// AlgorithmIdentifier RSA_ALGORITHM_IDENTIFIER =
// new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE);
//// byte[] result = KeyUtil.getEncodedSubjectPublicKeyInfo(RSA_ALGORITHM_IDENTIFIER, bytes);
// byte[] algo = RSA_ALGORITHM_IDENTIFIER.getEncoded();
// System.out.println(Hex.toHexString(algo));
//// System.out.println(Hex.toHexString(result));
// System.out.println(Hex.toHexString(pubKeyBytes_PKCS8));

byte[] privKeyBytes_PKCS8 = RSAUtils.privKey2Bytes_PKCS8(privKey);
byte[] privKeyBytesConverted_PKCS8 =


+ 19
- 0
source/crypto/crypto-pki/src/main/java/com/jd/blockchain/crypto/CSRBuilder.java View File

@@ -1,5 +1,7 @@
package com.jd.blockchain.crypto;

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;

/**
* @author zhanglin33
* @title: CSRBuilder
@@ -7,4 +9,21 @@ package com.jd.blockchain.crypto;
* @date 2019-05-10, 15:10
*/
public class CSRBuilder {

private String C;
private String ST;
private String L;
private String O;
private String OU;
private String CN;
private String E;

public AsymmetricCipherKeyPair init() {
return null;
}

public String buildRequest(String keyName, AsymmetricCipherKeyPair keyPair, String algoName,
String[] applicantInfo) {
return null;
}
}

Loading…
Cancel
Save