java.security.spec.RSAKeyGenParameterSpec Java Examples

The following examples show how to use java.security.spec.RSAKeyGenParameterSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpecTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        int size = 0;

        if (args.length >= 1) {
            size = Integer.parseInt(args[0]);
        } else {
            throw new RuntimeException("Missing keysize to test with");
        }

        BigInteger publicExponent
                = (args.length >= 2) ? new BigInteger(args[1]) : RSAKeyGenParameterSpec.F4;

        System.out.println("Running test with key size: " + size
                + " and public exponent: " + publicExponent);

        KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
        kpg1.initialize(new RSAKeyGenParameterSpec(size, publicExponent));
        if (!specTest(kpg1.generateKeyPair(), publicExponent)) {
            throw new RuntimeException("Test failed.");
        }
    }
 
Example #2
Source File: KeyPairGeneratorSpi.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public void initialize(
    AlgorithmParameterSpec params,
    SecureRandom random)
    throws InvalidAlgorithmParameterException
{
    if (!(params instanceof RSAKeyGenParameterSpec))
    {
        throw new InvalidAlgorithmParameterException("parameter object not a RSAKeyGenParameterSpec");
    }
    RSAKeyGenParameterSpec rsaParams = (RSAKeyGenParameterSpec)params;

    param = new RSAKeyGenerationParameters(
        rsaParams.getPublicExponent(),
        random, rsaParams.getKeysize(), defaultTests);

    engine.init(param);
}
 
Example #3
Source File: LoginController.java    From L2jBrasil with GNU General Public License v3.0 6 votes vote down vote up
private LoginController() throws GeneralSecurityException
{
	_log.info("Loading LoginContoller...");

	_hackProtection = new HashMap<>();

	_keyPairs = new ScrambledKeyPair[10];

	KeyPairGenerator keygen = null;

	keygen = KeyPairGenerator.getInstance("RSA");
	RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
	keygen.initialize(spec);

	//generate the initial set of keys
	for (int i = 0; i < 10; i++)
	{
		_keyPairs[i] = new ScrambledKeyPair(keygen.generateKeyPair());
	}
	_log.info("Cached 10 KeyPairs for RSA communication");

	testCipher((RSAPrivateKey) _keyPairs[0]._pair.getPrivate());

	// Store keys for blowfish communication
	generateBlowFishKeys();
}
 
Example #4
Source File: SpecTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        int size = 0;

        if (args.length >= 1) {
            size = Integer.parseInt(args[0]);
        } else {
            throw new RuntimeException("Missing keysize to test with");
        }

        BigInteger publicExponent
                = (args.length >= 2) ? new BigInteger(args[1]) : RSAKeyGenParameterSpec.F4;

        System.out.println("Running test with key size: " + size
                + " and public exponent: " + publicExponent);

        KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
        kpg1.initialize(new RSAKeyGenParameterSpec(size, publicExponent));
        if (!specTest(kpg1.generateKeyPair(), publicExponent)) {
            throw new RuntimeException("Test failed.");
        }
    }
 
Example #5
Source File: KeyPairGeneratorSpi.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public void initialize(
    AlgorithmParameterSpec params,
    SecureRandom random)
    throws InvalidAlgorithmParameterException
{
    if (!(params instanceof RSAKeyGenParameterSpec))
    {
        throw new InvalidAlgorithmParameterException("parameter object not a RSAKeyGenParameterSpec");
    }
    RSAKeyGenParameterSpec rsaParams = (RSAKeyGenParameterSpec)params;

    param = new RSAKeyGenerationParameters(
        rsaParams.getPublicExponent(),
        random, rsaParams.getKeysize(), defaultTests);

    engine.init(param);
}
 
Example #6
Source File: KeyGen.java    From aion-germany with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Initialize Key Generator (Blowfish keygen and RSA keygen)
 *
 * @throws GeneralSecurityException
 */
public static void init() throws GeneralSecurityException {
    log.info("Initializing Key Generator...");

    blowfishKeyGen = KeyGenerator.getInstance("Blowfish");

    KeyPairGenerator rsaKeyPairGenerator = KeyPairGenerator.getInstance("RSA");

    RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
    rsaKeyPairGenerator.initialize(spec);
    encryptedRSAKeyPairs = new EncryptedRSAKeyPair[10];

    for (int i = 0; i < 10; i++) {
        encryptedRSAKeyPairs[i] = new EncryptedRSAKeyPair(
                rsaKeyPairGenerator.generateKeyPair());
    }

    // Pre-init RSA cipher.. saving about 300ms
    Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
    rsaCipher.init(Cipher.DECRYPT_MODE, encryptedRSAKeyPairs[0].getRSAKeyPair().getPrivate());
}
 
Example #7
Source File: RsaPssTest.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an AlgorithmParameterSpec for generating a RSASSA-PSS key,
 * which include the PSSParameters.
 * Requires jdk11.
 * 
 * @param keySizeInBits the size of the modulus in bits.
 * @param sha the name of the hash function for hashing the input (e.g. "SHA-256")
 * @param mgf the name of the mask generating function (typically "MGF1")
 * @param mgfSha the name of the hash function for the mask generating function
 *        (typically the same as sha).
 * @param saltLength the length of the salt in bytes (typically the digest size of sha,
 *        i.e. 32 for "SHA-256")
 * @throws NoSuchMethodException if the AlgorithmParameterSpec is not
 *   supported (i.e. this happens before jdk11).
 */
public RSAKeyGenParameterSpec getPssAlgorithmParameters(
    int keySizeInBits,
    String sha,
    String mgf,
    String mgfSha,
    int saltLength) throws Exception {
  BigInteger publicExponent = new BigInteger("65537");
  PSSParameterSpec params = 
      new PSSParameterSpec(sha, mgf, new MGF1ParameterSpec(mgfSha), saltLength, 1);
  // Uses reflection to call 
  // public RSAKeyGenParameterSpec(int keysize, BigInteger publicExponent,
  //        AlgorithmParameterSpec keyParams)
  // because this method is only supported in jdk11. This throws a NoSuchMethodException
  // for older jdks.
  Constructor<RSAKeyGenParameterSpec> c =
      RSAKeyGenParameterSpec.class.getConstructor(
          int.class, BigInteger.class, AlgorithmParameterSpec.class);
  return c.newInstance(keySizeInBits, publicExponent, params);
}
 
Example #8
Source File: KeyUtil.java    From snowblossom with Apache License 2.0 6 votes vote down vote up
public static WalletKeyPair generateWalletRSAKey(int key_len)
{
  try
  {
    RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(key_len, RSAKeyGenParameterSpec.F4);

    KeyPairGenerator key_gen = KeyPairGenerator.getInstance("RSA", Globals.getCryptoProviderName());

    key_gen.initialize(spec);
    KeyPair key_pair = key_gen.genKeyPair();

    WalletKeyPair wkp = WalletKeyPair.newBuilder()
      .setPublicKey(ByteString.copyFrom(key_pair.getPublic().getEncoded()))
      .setPrivateKey(ByteString.copyFrom(key_pair.getPrivate().getEncoded()))
      .setSignatureType(SignatureUtil.SIG_TYPE_RSA)
      .build();
    return wkp;
  }
  catch(Exception e)
  {
    throw new RuntimeException(e);
  }
}
 
Example #9
Source File: SpecTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        int size = 0;

        if (args.length >= 1) {
            size = Integer.parseInt(args[0]);
        } else {
            throw new RuntimeException("Missing keysize to test with");
        }

        BigInteger publicExponent
                = (args.length >= 2) ? new BigInteger(args[1]) : RSAKeyGenParameterSpec.F4;

        System.out.println("Running test with key size: " + size
                + " and public exponent: " + publicExponent);

        KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
        kpg1.initialize(new RSAKeyGenParameterSpec(size, publicExponent));
        if (!specTest(kpg1.generateKeyPair(), publicExponent)) {
            throw new RuntimeException("Test failed.");
        }
    }
 
Example #10
Source File: RSAKeyPairGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    int tmpSize;
    if (params == null) {
        tmpSize = KEY_SIZE_DEFAULT;
    } else if (params instanceof RSAKeyGenParameterSpec) {

        if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
            throw new InvalidAlgorithmParameterException
                ("Exponent parameter is not supported");
        }
        tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();

    } else {
        throw new InvalidAlgorithmParameterException
            ("Params must be an instance of RSAKeyGenParameterSpec");
    }

    try {
        RSAKeyFactory.checkKeyLengths(tmpSize, null,
            KEY_SIZE_MIN, KEY_SIZE_MAX);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid Key sizes", e);
    }

    this.keySize = tmpSize;
}
 
Example #11
Source File: GenerateRSAKeyPair.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        RSAKeyGenParameterSpec rsaSpec =
        new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
        kpg.initialize(rsaSpec);

        // test generateKeyPair
        KeyPair kpair = kpg.generateKeyPair();
        if (kpair == null) {
            throw new Exception("no keypair generated");
        }
    }
 
Example #12
Source File: RSAKeyPairGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(int keySize, SecureRandom random) {

        // do not allow unreasonably small or large key sizes,
        // probably user error
        try {
            RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
                512, 64 * 1024);
        } catch (InvalidKeyException e) {
            throw new InvalidParameterException(e.getMessage());
        }

        this.keySize = keySize;
        this.random = random;
        this.publicExponent = RSAKeyGenParameterSpec.F4;
    }
 
Example #13
Source File: RSAKeyPairGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    if (params instanceof RSAKeyGenParameterSpec == false) {
        throw new InvalidAlgorithmParameterException
            ("Params must be instance of RSAKeyGenParameterSpec");
    }

    RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
    int tmpKeySize = rsaSpec.getKeysize();
    BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();

    if (tmpPublicExponent == null) {
        tmpPublicExponent = RSAKeyGenParameterSpec.F4;
    } else {
        if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
            throw new InvalidAlgorithmParameterException
                    ("Public exponent must be 3 or larger");
        }
        if (tmpPublicExponent.bitLength() > tmpKeySize) {
            throw new InvalidAlgorithmParameterException
                    ("Public exponent must be smaller than key size");
        }
    }

    // do not allow unreasonably large key sizes, probably user error
    try {
        RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent,
            512, 64 * 1024);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid key sizes", e);
    }

    this.keySize = tmpKeySize;
    this.publicExponent = tmpPublicExponent;
    this.random = random;
}
 
Example #14
Source File: RSAKeygen.java    From NuVotifier with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generates an RSA key pair.
 *
 * @param bits The amount of bits
 * @return The key pair
 */
public static KeyPair generate(int bits) throws Exception {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(bits,
            RSAKeyGenParameterSpec.F4);
    keygen.initialize(spec);
    return keygen.generateKeyPair();
}
 
Example #15
Source File: RSAKeyPairGenerator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(int keySize, SecureRandom random) {

        // do not allow unreasonably small or large key sizes,
        // probably user error
        try {
            RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
                512, 64 * 1024);
        } catch (InvalidKeyException e) {
            throw new InvalidParameterException(e.getMessage());
        }

        this.keySize = keySize;
        this.random = random;
        this.publicExponent = RSAKeyGenParameterSpec.F4;
    }
 
Example #16
Source File: RSAKeyPairGenerator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    int tmpSize;
    if (params == null) {
        tmpSize = KEY_SIZE_DEFAULT;
    } else if (params instanceof RSAKeyGenParameterSpec) {

        if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
            throw new InvalidAlgorithmParameterException
                ("Exponent parameter is not supported");
        }
        tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();

    } else {
        throw new InvalidAlgorithmParameterException
            ("Params must be an instance of RSAKeyGenParameterSpec");
    }

    try {
        RSAKeyFactory.checkKeyLengths(tmpSize, null,
            KEY_SIZE_MIN, KEY_SIZE_MAX);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid Key sizes", e);
    }

    this.keySize = tmpSize;
}
 
Example #17
Source File: GenerateRSAKeyPair.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        RSAKeyGenParameterSpec rsaSpec =
        new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
        kpg.initialize(rsaSpec);

        // test generateKeyPair
        KeyPair kpair = kpg.generateKeyPair();
        if (kpair == null) {
            throw new Exception("no keypair generated");
        }
    }
 
Example #18
Source File: RSAKeyPairGenerator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(int keySize, SecureRandom random) {

        // do not allow unreasonably small or large key sizes,
        // probably user error
        try {
            RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
                512, 64 * 1024);
        } catch (InvalidKeyException e) {
            throw new InvalidParameterException(e.getMessage());
        }

        this.keySize = keySize;
        this.random = random;
        this.publicExponent = RSAKeyGenParameterSpec.F4;
    }
 
Example #19
Source File: RSAKeyPairGenerator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    if (params instanceof RSAKeyGenParameterSpec == false) {
        throw new InvalidAlgorithmParameterException
            ("Params must be instance of RSAKeyGenParameterSpec");
    }

    RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
    int tmpKeySize = rsaSpec.getKeysize();
    BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();

    if (tmpPublicExponent == null) {
        tmpPublicExponent = RSAKeyGenParameterSpec.F4;
    } else {
        if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
            throw new InvalidAlgorithmParameterException
                    ("Public exponent must be 3 or larger");
        }
        if (tmpPublicExponent.bitLength() > tmpKeySize) {
            throw new InvalidAlgorithmParameterException
                    ("Public exponent must be smaller than key size");
        }
    }

    // do not allow unreasonably large key sizes, probably user error
    try {
        RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent,
            512, 64 * 1024);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid key sizes", e);
    }

    this.keySize = tmpKeySize;
    this.publicExponent = tmpPublicExponent;
    this.random = random;
}
 
Example #20
Source File: InsufficientKeySizeRsa.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public KeyPair weakKeySize4ParameterSpec() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(new RSAKeyGenParameterSpec(128,RSAKeyGenParameterSpec.F4), new SecureRandom()); //BAD

    KeyPair key = keyGen.generateKeyPair();
    return key;
}
 
Example #21
Source File: InsufficientKeySizeRsa.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public KeyPair weakKeySize3ParameterSpec() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(new RSAKeyGenParameterSpec(128,RSAKeyGenParameterSpec.F4)); //BAD

    KeyPair key = keyGen.generateKeyPair();
    return key;
}
 
Example #22
Source File: SpecTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    int failCount = 0;

    // Test key size.
    int size = Integer.parseInt(args[0]);

    try {
        KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
        kpg1.initialize(new RSAKeyGenParameterSpec(size,
                RSAKeyGenParameterSpec.F4));
        if (!specTest(kpg1.generateKeyPair(),
                RSAKeyGenParameterSpec.F4)) {
            failCount++;
        }

        KeyPairGenerator kpg2 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
        kpg2.initialize(new RSAKeyGenParameterSpec(size,
                RSAKeyGenParameterSpec.F0));
        if (!specTest(kpg2.generateKeyPair(), RSAKeyGenParameterSpec.F0)) {
            failCount++;
        }
    } catch (NoSuchAlgorithmException | NoSuchProviderException
            | InvalidAlgorithmParameterException ex) {
        ex.printStackTrace(System.err);
        failCount++;
    }

    if (failCount != 0) {
        throw new RuntimeException("There are " + failCount
                + " tests failed.");
    }
}
 
Example #23
Source File: GenerateRSAKeyPair.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        RSAKeyGenParameterSpec rsaSpec =
        new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
        kpg.initialize(rsaSpec);

        // test generateKeyPair
        KeyPair kpair = kpg.generateKeyPair();
        if (kpair == null) {
            throw new Exception("no keypair generated");
        }
    }
 
Example #24
Source File: RSAKeyPairGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    if (params instanceof RSAKeyGenParameterSpec == false) {
        throw new InvalidAlgorithmParameterException
            ("Params must be instance of RSAKeyGenParameterSpec");
    }

    RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
    int tmpKeySize = rsaSpec.getKeysize();
    BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();

    if (tmpPublicExponent == null) {
        tmpPublicExponent = RSAKeyGenParameterSpec.F4;
    } else {
        if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
            throw new InvalidAlgorithmParameterException
                    ("Public exponent must be 3 or larger");
        }
        if (tmpPublicExponent.bitLength() > tmpKeySize) {
            throw new InvalidAlgorithmParameterException
                    ("Public exponent must be smaller than key size");
        }
    }

    // do not allow unreasonably large key sizes, probably user error
    try {
        RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent,
            512, 64 * 1024);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid key sizes", e);
    }

    this.keySize = tmpKeySize;
    this.publicExponent = tmpPublicExponent;
    this.random = random;
}
 
Example #25
Source File: RSAKeyPairGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    int tmpSize;
    if (params == null) {
        tmpSize = KEY_SIZE_DEFAULT;
    } else if (params instanceof RSAKeyGenParameterSpec) {

        if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
            throw new InvalidAlgorithmParameterException
                ("Exponent parameter is not supported");
        }
        tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();

    } else {
        throw new InvalidAlgorithmParameterException
            ("Params must be an instance of RSAKeyGenParameterSpec");
    }

    try {
        RSAKeyFactory.checkKeyLengths(tmpSize, null,
            KEY_SIZE_MIN, KEY_SIZE_MAX);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid Key sizes", e);
    }

    this.keySize = tmpSize;
}
 
Example #26
Source File: RSAKeyPairGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    if (params instanceof RSAKeyGenParameterSpec == false) {
        throw new InvalidAlgorithmParameterException
            ("Params must be instance of RSAKeyGenParameterSpec");
    }

    RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
    int tmpKeySize = rsaSpec.getKeysize();
    BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();

    if (tmpPublicExponent == null) {
        tmpPublicExponent = RSAKeyGenParameterSpec.F4;
    } else {
        if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
            throw new InvalidAlgorithmParameterException
                    ("Public exponent must be 3 or larger");
        }
        if (tmpPublicExponent.bitLength() > tmpKeySize) {
            throw new InvalidAlgorithmParameterException
                    ("Public exponent must be smaller than key size");
        }
    }

    // do not allow unreasonably large key sizes, probably user error
    try {
        RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent,
            512, 64 * 1024);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid key sizes", e);
    }

    this.keySize = tmpKeySize;
    this.publicExponent = tmpPublicExponent;
    this.random = random;
}
 
Example #27
Source File: RSAKeyPairGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(int keySize, SecureRandom random) {

        // do not allow unreasonably small or large key sizes,
        // probably user error
        try {
            RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
                512, 64 * 1024);
        } catch (InvalidKeyException e) {
            throw new InvalidParameterException(e.getMessage());
        }

        this.keySize = keySize;
        this.random = random;
        this.publicExponent = RSAKeyGenParameterSpec.F4;
    }
 
Example #28
Source File: CryptographicUtilities.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static AsymmetricCipherKeyPair generateRsaKeyPair(int keyStrength) throws Exception {
	Security.addProvider(new BouncyCastleProvider());

	try {
		RSAKeyPairGenerator generator = new RSAKeyPairGenerator();
		generator.init(new RSAKeyGenerationParameters(RSAKeyGenParameterSpec.F4, SecureRandom.getInstance("SHA1PRNG"), keyStrength, 80));
		AsymmetricCipherKeyPair keyPair = generator.generateKeyPair();
		return keyPair;
	} catch (Exception e) {
		throw new Exception("Cannot create RSA keypair", e);
	}
}
 
Example #29
Source File: RSAKeyPairGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    int tmpSize;
    if (params == null) {
        tmpSize = KEY_SIZE_DEFAULT;
    } else if (params instanceof RSAKeyGenParameterSpec) {

        if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
            throw new InvalidAlgorithmParameterException
                ("Exponent parameter is not supported");
        }
        tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();

    } else {
        throw new InvalidAlgorithmParameterException
            ("Params must be an instance of RSAKeyGenParameterSpec");
    }

    try {
        RSAKeyFactory.checkKeyLengths(tmpSize, null,
            KEY_SIZE_MIN, KEY_SIZE_MAX);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid Key sizes", e);
    }

    this.keySize = tmpSize;
}
 
Example #30
Source File: RSAKeyPairGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void initialize(int keySize, SecureRandom random) {

        // do not allow unreasonably small or large key sizes,
        // probably user error
        try {
            RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
                512, 64 * 1024);
        } catch (InvalidKeyException e) {
            throw new InvalidParameterException(e.getMessage());
        }

        this.keySize = keySize;
        this.random = random;
        this.publicExponent = RSAKeyGenParameterSpec.F4;
    }