List of usage examples for org.bouncycastle.crypto.params RSAPrivateCrtKeyParameters getPublicExponent
public BigInteger getPublicExponent()
From source file:co.lqnt.lockbox.key.KeyFactory.java
License:Open Source License
/** * Generate a new private key./* ww w . j av a 2s . c om*/ * * @param size The size of the key in bits. * * @return The private key. */ public PrivateKey generatePrivateKey(final int size) { this.keyGenerator().init(new RSAKeyGenerationParameters(BigInteger.valueOf(65537), this.random().jceSecureRandom(), size, 80)); AsymmetricKeyParameter keyParameters = this.keyGenerator().generateKeyPair().getPrivate(); RSAPrivateCrtKeyParameters rsaKeyParameters; if (keyParameters instanceof RSAPrivateCrtKeyParameters) { rsaKeyParameters = (RSAPrivateCrtKeyParameters) keyParameters; } else { throw new RuntimeException("Invalid key pair generated by Bouncy Castle."); } return new PrivateKey(rsaKeyParameters.getModulus(), rsaKeyParameters.getPublicExponent(), rsaKeyParameters.getExponent(), rsaKeyParameters.getP(), rsaKeyParameters.getQ(), rsaKeyParameters.getDP(), rsaKeyParameters.getDQ(), rsaKeyParameters.getQInv()); }
From source file:co.lqnt.lockbox.key.KeyFactory.java
License:Open Source License
/** * Convert Bouncy Castle private key info to a Lockbox private key. * * @param keyInformation The private key information. * * @return The private key.// ww w . j av a 2 s.c o m * @throws PrivateKeyReadException If reading of the private key fails. */ protected PrivateKey convertPrivateKey(final PrivateKeyInfo keyInformation) throws PrivateKeyReadException { AsymmetricKeyParameter keyParameter; try { keyParameter = this.bcKeyParametersFactory().createPrivateKeyParameters(keyInformation); } catch (IOException e) { throw new PrivateKeyReadException(e); } RSAPrivateCrtKeyParameters privateKeyParameters; if (keyParameter instanceof RSAPrivateCrtKeyParameters) { privateKeyParameters = (RSAPrivateCrtKeyParameters) keyParameter; } else { throw new PrivateKeyReadException(); } return new PrivateKey(privateKeyParameters.getModulus(), privateKeyParameters.getPublicExponent(), privateKeyParameters.getExponent(), privateKeyParameters.getP(), privateKeyParameters.getQ(), privateKeyParameters.getDP(), privateKeyParameters.getDQ(), privateKeyParameters.getQInv()); }
From source file:com.foilen.smalltools.crypt.bouncycastle.cert.RSATools.java
License:Open Source License
/** * Create a {@link Key} from the private {@link AsymmetricKeys}. * * @param asymmetricKeys//from w w w. jav a 2s .c om * the asymmetric keys * @return the Java key */ public static PrivateKey createPrivateKey(AsymmetricKeys asymmetricKeys) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPrivateCrtKeyParameters privateKeyParameters = (RSAPrivateCrtKeyParameters) asymmetricKeys .getPrivateKey(); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(privateKeyParameters.getModulus(), privateKeyParameters.getPublicExponent(), privateKeyParameters.getExponent(), privateKeyParameters.getP(), privateKeyParameters.getQ(), privateKeyParameters.getDP(), privateKeyParameters.getDQ(), privateKeyParameters.getQInv()); return keyFactory.generatePrivate(keySpec); } catch (Exception e) { throw new SmallToolsException("Problem generating the key", e); } }
From source file:com.joyent.http.signature.crypto.NativeRSABlindedEngine.java
License:Open Source License
/** * Process a single block using the basic RSA algorithm. * * @param in the input array.//w w w .ja v a 2 s . co m * @param inOff the offset into the input buffer where the data starts. * @param inLen the length of the data to be processed. * @return the result of the RSA process. * @exception DataLengthException the input block is too large. */ @Override public byte[] processBlock(final byte[] in, final int inOff, final int inLen) { if (key == null) { throw new IllegalStateException("RSA engine not initialised"); } BigInteger input = core.convertInput(in, inOff, inLen); BigInteger result; if (key instanceof RSAPrivateCrtKeyParameters) { RSAPrivateCrtKeyParameters k = (RSAPrivateCrtKeyParameters) key; BigInteger e = k.getPublicExponent(); // can't do blinding without a public exponent if (e != null) { BigInteger m = k.getModulus(); BigInteger r = BigIntegers.createRandomInRange(ONE, m.subtract(ONE), random); // This is a modification to use the GMP native library method BigInteger blindedModPow = Gmp.modPowSecure(r, e, m); BigInteger blindedInput = blindedModPow.multiply(input).mod(m); BigInteger blindedResult = core.processBlock(blindedInput); // This is a modification to use the GMP native library method BigInteger rInv = Gmp.modInverse(r, m); result = blindedResult.multiply(rInv).mod(m); // defence against Arjen Lenstras CRT attack // This is a modification to use the GMP native library method if (!input.equals(Gmp.modPowInsecure(result, e, m))) { throw new IllegalStateException("RSA engine faulty decryption/signing detected"); } } else { result = core.processBlock(input); } } else { result = core.processBlock(input); } return core.convertOutput(result); }
From source file:dorkbox.util.crypto.CryptoRSA.java
License:Apache License
@SuppressWarnings("RedundantIfStatement") public static boolean compare(RSAPrivateCrtKeyParameters private1, RSAPrivateCrtKeyParameters private2) { if (!private1.getModulus().equals(private2.getModulus())) { return false; }//from w w w . ja v a2 s.c o m if (!private1.getExponent().equals(private2.getExponent())) { return false; } if (!private1.getDP().equals(private2.getDP())) { return false; } if (!private1.getDQ().equals(private2.getDQ())) { return false; } if (!private1.getP().equals(private2.getP())) { return false; } if (!private1.getPublicExponent().equals(private2.getPublicExponent())) { return false; } if (!private1.getQ().equals(private2.getQ())) { return false; } if (!private1.getQInv().equals(private2.getQInv())) { return false; } return true; }
From source file:dorkbox.util.serialization.RsaPrivateKeySerializer.java
License:Apache License
@Override public void write(Kryo kryo, Output output, RSAPrivateCrtKeyParameters key) { byte[] bytes; int length;/*from w ww.j a v a 2 s. co m*/ ///////////// bytes = key.getDP().toByteArray(); length = bytes.length; output.writeInt(length, true); output.writeBytes(bytes, 0, length); ///////////// bytes = key.getDQ().toByteArray(); length = bytes.length; output.writeInt(length, true); output.writeBytes(bytes, 0, length); ///////////// bytes = key.getExponent().toByteArray(); length = bytes.length; output.writeInt(length, true); output.writeBytes(bytes, 0, length); ///////////// bytes = key.getModulus().toByteArray(); length = bytes.length; output.writeInt(length, true); output.writeBytes(bytes, 0, length); ///////////// bytes = key.getP().toByteArray(); length = bytes.length; output.writeInt(length, true); output.writeBytes(bytes, 0, length); ///////////// bytes = key.getPublicExponent().toByteArray(); length = bytes.length; output.writeInt(length, true); output.writeBytes(bytes, 0, length); ///////////// bytes = key.getQ().toByteArray(); length = bytes.length; output.writeInt(length, true); output.writeBytes(bytes, 0, length); ///////////// bytes = key.getQInv().toByteArray(); length = bytes.length; output.writeInt(length, true); output.writeBytes(bytes, 0, length); }
From source file:frost.crypt.FrostCrypt.java
License:Open Source License
/** * Generate a new RSA 1024 bit key pair. * @returns String[0] is private key; String[1] is public key *//*from w w w .ja v a 2 s .c om*/ public synchronized String[] generateKeys() { RSAKeyPairGenerator keygen = new RSAKeyPairGenerator(); keygen.init(new RSAKeyGenerationParameters( new BigInteger("3490529510847650949147849619903898133417764638493387843990820577"), getSecureRandom(), 1024, 80)); //this big integer is the winner of some competition as far as I remember AsymmetricCipherKeyPair keys = keygen.generateKeyPair(); //extract the keys RSAKeyParameters pubKey = (RSAKeyParameters) keys.getPublic(); RSAPrivateCrtKeyParameters privKey = (RSAPrivateCrtKeyParameters) keys.getPrivate(); //the return value String[] result = new String[2]; StringBuffer temp = new StringBuffer(); //create the keys temp.append(new String(Base64.encode(pubKey.getExponent().toByteArray()))); temp.append(":"); temp.append(new String(Base64.encode(pubKey.getModulus().toByteArray()))); result[1] = temp.toString(); // public key //rince and repeat, this time exactly the way its done in the constructor temp = new StringBuffer(); temp.append(new String(Base64.encode(privKey.getModulus().toByteArray()))); temp.append(":"); temp.append(new String(Base64.encode(privKey.getPublicExponent().toByteArray()))); temp.append(":"); temp.append(new String(Base64.encode(privKey.getExponent().toByteArray()))); temp.append(":"); temp.append(new String(Base64.encode(privKey.getP().toByteArray()))); temp.append(":"); temp.append(new String(Base64.encode(privKey.getQ().toByteArray()))); temp.append(":"); temp.append(new String(Base64.encode(privKey.getDP().toByteArray()))); temp.append(":"); temp.append(new String(Base64.encode(privKey.getDQ().toByteArray()))); temp.append(":"); temp.append(new String(Base64.encode(privKey.getQInv().toByteArray()))); result[0] = temp.toString(); // private key return result; }
From source file:org.ourfilesystem.security.KeySet.java
License:Open Source License
public boolean equals(Object obj) { if (!(obj instanceof KeySet)) return false; KeySet ks = (KeySet) obj;/*from ww w. j a v a 2 s .c o m*/ if (PrivateEncryptionKey == null && ks.getPrivateEncryptionKey() != null) return false; if (PrivateEncryptionKey != null) { if (PrivateEncryptionKey instanceof RSAPrivateCrtKeyParameters && ks.getPrivateEncryptionKey() instanceof RSAPrivateCrtKeyParameters) { RSAPrivateCrtKeyParameters pek = (RSAPrivateCrtKeyParameters) ks.getPrivateEncryptionKey(); RSAPrivateCrtKeyParameters mek = (RSAPrivateCrtKeyParameters) PrivateEncryptionKey; if (!pek.getDP().equals(mek.getDP())) return false; if (!pek.getDQ().equals(mek.getDQ())) return false; if (!pek.getExponent().equals(mek.getExponent())) return false; if (!pek.getModulus().equals(mek.getModulus())) return false; if (!pek.getP().equals(mek.getP())) return false; if (!pek.getPublicExponent().equals(mek.getPublicExponent())) return false; if (!pek.getQ().equals(mek.getQ())) return false; if (!pek.getQInv().equals(mek.getQInv())) return false; } else { if (!PrivateEncryptionKey.equals(ks.getPrivateEncryptionKey())) return false; } } if (PrivateSigningKey == null && ks.getPrivateSigningKey() != null) return false; if (PrivateSigningKey != null) { if (PrivateSigningKey instanceof RSAPrivateCrtKeyParameters && ks.getPrivateSigningKey() instanceof RSAPrivateCrtKeyParameters) { RSAPrivateCrtKeyParameters pek = (RSAPrivateCrtKeyParameters) ks.getPrivateSigningKey(); RSAPrivateCrtKeyParameters mek = (RSAPrivateCrtKeyParameters) PrivateSigningKey; if (!pek.getDP().equals(mek.getDP())) return false; if (!pek.getDQ().equals(mek.getDQ())) return false; if (!pek.getExponent().equals(mek.getExponent())) return false; if (!pek.getModulus().equals(mek.getModulus())) return false; if (!pek.getP().equals(mek.getP())) return false; if (!pek.getPublicExponent().equals(mek.getPublicExponent())) return false; if (!pek.getQ().equals(mek.getQ())) return false; if (!pek.getQInv().equals(mek.getQInv())) return false; } else { if (!PrivateSigningKey.equals(ks.getPrivateSigningKey())) return false; } } if (PublicKeySet == null && ks.getPublicKeySet() != null) return false; if (PublicKeySet != null) { if (!PublicKeySet.equals(ks.getPublicKeySet())) return false; } return true; }
From source file:org.ourfilesystem.security.SecurityTools.java
License:Open Source License
public static void writeRSAPrivateKey(RSAPrivateCrtKeyParameters p, OutputStream fos, ByteCounter c) throws IOException { FileUtils.writeBytes(fos, p.getDP().toByteArray(), c); FileUtils.writeBytes(fos, p.getDQ().toByteArray(), c); FileUtils.writeBytes(fos, p.getExponent().toByteArray(), c); FileUtils.writeBytes(fos, p.getModulus().toByteArray(), c); FileUtils.writeBytes(fos, p.getP().toByteArray(), c); FileUtils.writeBytes(fos, p.getPublicExponent().toByteArray(), c); FileUtils.writeBytes(fos, p.getQ().toByteArray(), c); FileUtils.writeBytes(fos, p.getQInv().toByteArray(), c); }
From source file:org.soulwing.credo.service.crypto.bc.BcCertificateWrapper.java
License:Apache License
/** * Tests whether a given RSA private key matches this certificate's * public key.//from ww w . ja va 2 s.c om * @param privateKey the private key to match * @return {@code true} if the keys match */ public boolean matches(RSAPrivateCrtKeyParameters privateKey) { AsymmetricKeyParameter key = derivePublicKeyParameters(); if (!(key instanceof RSAKeyParameters)) return false; RSAKeyParameters publicKey = (RSAKeyParameters) key; return publicKey.getModulus().equals(privateKey.getModulus()) && publicKey.getExponent().equals(privateKey.getPublicExponent()); }