BcRSAKeyPairGenerator extends AbstractBcKeyPairGenerator { @Override public AsymmetricKeyPair generate() { return generate(new RSAKeyGenerationParameters()); }  @Override AsymmetricKeyPair generate(); @Override AsymmetricKeyPair generate(KeyGenerationParameters parameters);  }
@Test void generateWithoutArgument() { AsymmetricKeyPair kp1 = generator.generate(); AsymmetricKeyPair kp2 = generator.generate(); assertThat(kp1, not(nullValue())); assertThat(kp2, not(nullValue())); assertThat(kp1.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(kp2.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(kp1.getPublic(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(kp2.getPublic(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters(), instanceOf( RSAPrivateCrtKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters(), instanceOf( RSAPrivateCrtKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters(), instanceOf(RSAKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters(), instanceOf(RSAKeyParameters.class)); assertThat( ((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters()).getModulus() .bitLength(), equalTo(2048)); assertThat( ((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters()).getModulus().bitLength(), equalTo(2048)); assertThat( ((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters()).getModulus() .bitLength(), equalTo(2048)); assertThat( ((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters()).getModulus().bitLength(), equalTo(2048)); assertThat(((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters()) .getPublicExponent(), equalTo(BigInteger.valueOf(0x10001))); assertThat(((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters()).getExponent(), equalTo(BigInteger.valueOf(0x10001))); assertThat(((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters()) .getPublicExponent(), equalTo(BigInteger.valueOf(0x10001))); assertThat(((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters()).getExponent(), equalTo(BigInteger.valueOf(0x10001))); }
@Test void generateWithStrengthParameter() { AsymmetricKeyPair kp = generator.generate(new RSAKeyGenerationParameters(64)); assertThat(kp, not(nullValue())); assertThat(kp.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(kp.getPublic(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters(), instanceOf( RSAPrivateCrtKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp.getPublic()).getParameters(), instanceOf(RSAKeyParameters.class)); assertThat( ((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters()).getModulus() .bitLength(), equalTo(512)); assertThat( ((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPublic()).getParameters()).getModulus().bitLength(), equalTo(512)); assertThat(((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters()) .getPublicExponent(), equalTo(BigInteger.valueOf(0x10001))); assertThat(((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPublic()).getParameters()).getExponent(), equalTo(BigInteger.valueOf(0x10001))); kp = generator.generate(new RSAKeyGenerationParameters(128)); assertThat(kp, not(nullValue())); assertThat(kp.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(kp.getPublic(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters(), instanceOf( RSAPrivateCrtKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp.getPublic()).getParameters(), instanceOf(RSAKeyParameters.class)); assertThat( ((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters()).getModulus() .bitLength(), equalTo(1024)); assertThat( ((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPublic()).getParameters()).getModulus().bitLength(), equalTo(1024)); assertThat(((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters()) .getPublicExponent(), equalTo(BigInteger.valueOf(0x10001))); assertThat(((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPublic()).getParameters()).getExponent(), equalTo(BigInteger.valueOf(0x10001))); }
@Test void generateWithPublicExponentParameter() { AsymmetricKeyPair kp = generator.generate(new RSAKeyGenerationParameters(64, BigInteger.valueOf(0x11))); assertThat(kp, not(nullValue())); assertThat(kp.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(kp.getPublic(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters(), instanceOf( RSAPrivateCrtKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp.getPublic()).getParameters(), instanceOf(RSAKeyParameters.class)); assertThat(((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters()) .getPublicExponent(), equalTo(BigInteger.valueOf(0x11))); assertThat(((RSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPublic()).getParameters()).getExponent(), equalTo(BigInteger.valueOf(0x11))); }
@Test void generateWithCertaintyParameter() { AsymmetricKeyPair kp = generator.generate(new RSAKeyGenerationParameters(64, 1)); assertThat(kp, not(nullValue())); assertThat(kp.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(kp.getPublic(), instanceOf(BcAsymmetricKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters(), instanceOf(RSAPrivateCrtKeyParameters.class)); assertThat(((BcAsymmetricKeyParameters) kp.getPublic()).getParameters(), instanceOf(RSAKeyParameters.class)); }