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(); }
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); }
public static KeyPair generateRSAKeypair(int keysize, BigInteger publicExponent, SecureRandom random) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { BigInteger tmpPublicExponent = publicExponent; if (tmpPublicExponent == null) { tmpPublicExponent = RSAKeyGenParameterSpec.F4; } AlgorithmParameterSpec params = new RSAKeyGenParameterSpec(keysize, tmpPublicExponent); KeyPairGenerator kpGen = getKeyPairGenerator("RSA"); synchronized (kpGen) { if (random == null) { kpGen.initialize(params); } else { kpGen.initialize(params, random); } return kpGen.generateKeyPair(); } }
@TargetApi(Build.VERSION_CODES.M) static void createKeysM(String alias, boolean requireAuth) { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE); keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setAlgorithmParameterSpec( new RSAKeyGenParameterSpec(1024, F4)) .setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1) .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA384, KeyProperties.DIGEST_SHA512) // Only permit the private key to be used if the user authenticated // within the last five minutes. .setUserAuthenticationRequired(requireAuth) .build()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); Log.d(TAG, "Public Key is: " + keyPair.getPublic().toString()); } catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } }
/** * Inits the RSA keys. */ private void initRSAKeys() { try { final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(new RSAKeyGenParameterSpec(512, RSAKeyGenParameterSpec.F4)); _keyPairs = new KeyPair[KEYS_SIZE]; for (int i = 0; i < KEYS_SIZE; i++) { _keyPairs[i] = keyGen.genKeyPair(); } } catch (Exception e) { LOGGER.severe(GameServerTable.class.getSimpleName() + ": Error loading RSA keys for Game Server communication!"); } }
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."); } }
@TargetApi(Build.VERSION_CODES.M) private static void createKeysM(String alias, boolean requireAuth) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE); keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setAlgorithmParameterSpec( new RSAKeyGenParameterSpec(1024, F4)) .setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1) .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA384, KeyProperties.DIGEST_SHA512) // Only permit the private key to be used if the user authenticated // within the last five minutes. .setUserAuthenticationRequired(requireAuth) .build()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); Log.d(TAG, "Public Key is: " + keyPair.getPublic().toString()); }
@Override public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (!(params instanceof RSAKeyGenParameterSpec)) { throw new InvalidAlgorithmParameterException("Only RSAKeyGenParameterSpec supported"); } RSAKeyGenParameterSpec spec = (RSAKeyGenParameterSpec) params; final BigInteger publicExponent = spec.getPublicExponent(); if (publicExponent != null) { this.publicExponent = publicExponent.toByteArray(); } this.modulusBits = spec.getKeysize(); }
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { HashMap attributes = new HashMap(); if (params != null) { if (! (params instanceof RSAKeyGenParameterSpec)) throw new InvalidAlgorithmParameterException("params"); attributes.put(RSAKeyPairGenerator.RSA_PARAMETERS, params); } if (random != null) attributes.put(RSAKeyPairGenerator.SOURCE_OF_RANDOMNESS, random); attributes.put(RSAKeyPairGenerator.PREFERRED_ENCODING_FORMAT, Integer.valueOf(Registry.ASN1_ENCODING_ID)); adaptee.setup(attributes); }
public CertRASession(String emailPrivate, String phonePrivate) throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4); SecureRandom secureRandom = new SecureRandom(); secureRandom.setSeed(System.currentTimeMillis()); keyPairGenerator.initialize(params, secureRandom); KeyPair keyPair = keyPairGenerator.genKeyPair(); this.privateKey = keyPair.getPrivate(); this.publicKey = keyPair.getPublic(); this.emailPrivate = emailPrivate; this.phonePrivate = phonePrivate; }
@Test public void test() throws Exception { // install BouncyCastle provider Security.addProvider(new BouncyCastleProvider()); // generate a keypair KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "BC"); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(STRENGTH, PUBLIC_EXP); gen.initialize(params, new SecureRandom()); KeyPair keyPair = gen.generateKeyPair(); FSTConfiguration fst = FSTConfiguration.createDefaultConfiguration(); // serialize byte[] serialized = fst.asByteArray(keyPair); // deserialize --> crash KeyPair deserialized = (KeyPair) fst.asObject(serialized); }
private void genKeys(int keyLength, String folderName) { if (keyLength > 0) this.keyLength = keyLength; if (folderName != null) this.folderName = folderName; if (!this.folderName.endsWith("/")) this.folderName += "/"; System.out.printf("Generating %d-bit RSA keys in folder '%s'\n", this.keyLength, this.folderName); try { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); // Generate a key that is keyLength bits long RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(this.keyLength, RSAKeyGenParameterSpec.F4); keyGen.initialize(spec); KeyPair pair = keyGen.generateKeyPair(); printKeyPair(pair); } catch (Exception e) { e.printStackTrace(); } }
private void loadRSAKeys() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(512,RSAKeyGenParameterSpec.F4); keyGen.initialize(spec); _keyPairs = new KeyPair[KEYS_SIZE]; for (int i = 0; i < KEYS_SIZE; i++) { _keyPairs[i] = keyGen.genKeyPair(); } }
/** * Setup the test. */ @BeforeClass public static void setup() throws Exception { Provider bc = new BouncyCastleProvider(); Security.addProvider(bc); final byte[] preimage = "Hello World!".getBytes(Charset.defaultCharset()); final byte[] prefix = "Ying ".getBytes(Charset.defaultCharset()); final byte[] message = "Yang".getBytes(Charset.defaultCharset()); //final byte[] prefixedMessage = "Ying Yang".getBytes(Charset.defaultCharset()); final MessageDigest sha256Digest = MessageDigest.getInstance("SHA-256"); final MessageDigest sha512Digest = MessageDigest.getInstance("SHA-512"); //final byte[] fingerprint = sha256Digest.digest(preimage); final KeyPairGenerator rsaKpg = KeyPairGenerator.getInstance("RSA"); rsaKpg.initialize(new RSAKeyGenParameterSpec(2048, new BigInteger("65537"))); KeyPair rsaKeyPair = rsaKpg.generateKeyPair(); Signature rsaSigner = Signature.getInstance("SHA256withRSA/PSS"); rsaSigner.initSign(rsaKeyPair.getPrivate()); rsaSigner.update(message); net.i2p.crypto.eddsa.KeyPairGenerator edDsaKpg = new net.i2p.crypto.eddsa.KeyPairGenerator(); KeyPair edDsaKeyPair = edDsaKpg.generateKeyPair(); Signature edDsaSigner = new EdDSAEngine(sha512Digest); edDsaSigner.initSign(edDsaKeyPair.getPrivate()); edDsaSigner.update(prefix); edDsaSigner.update(message); preimageCondition = new PreimageSha256Condition(preimage); rsaCondition = new RsaSha256Condition((RSAPublicKey) rsaKeyPair.getPublic()); ed25519Condition = new Ed25519Sha256Condition((EdDSAPublicKey) edDsaKeyPair.getPublic()); prefixSha256Condition = new PrefixSha256Condition(prefix, 1000, ed25519Condition); thresholdCondition = new ThresholdSha256Condition( 2, Lists.newArrayList(preimageCondition, rsaCondition, prefixSha256Condition) ); byte[] rsaSignature = rsaSigner.sign(); byte[] edDsaSignature = edDsaSigner.sign(); preimageFulfillment = new PreimageSha256Fulfillment(preimage); rsaFulfillment = new RsaSha256Fulfillment((RSAPublicKey) rsaKeyPair.getPublic(), rsaSignature); ed25519Fulfillment = new Ed25519Sha256Fulfillment((EdDSAPublicKey) edDsaKeyPair.getPublic(), edDsaSignature); prefixSha256Fulfillment = new PrefixSha256Fulfillment(prefix, 1000, ed25519Fulfillment); thresholdFulfillment = new ThresholdSha256Fulfillment( Lists.newArrayList(rsaCondition), Lists.newArrayList(preimageFulfillment, prefixSha256Fulfillment) ); }
private LoginController() throws GeneralSecurityException { _log.info("Loading LoginController..."); _keyPairs = new ScrambledKeyPair[10]; final KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); final 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(); final Thread purge = new PurgeThread(); purge.setDaemon(true); purge.start(); }
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; }
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; }
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; }
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"); } }
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."); } }
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; }