use of java.security.spec.RSAPrivateKeySpec in project Wurst-MC-1.12 by Wurst-Imperium.
the class Encryption method loadRsaKeys.
private KeyPair loadRsaKeys(Path publicFile, Path privateFile) throws GeneralSecurityException, ReflectiveOperationException, IOException {
KeyFactory factory = KeyFactory.getInstance("RSA");
// load public key
PublicKey publicKey;
try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(publicFile))) {
publicKey = factory.generatePublic(new RSAPublicKeySpec((BigInteger) in.readObject(), (BigInteger) in.readObject()));
}
// load private key
PrivateKey privateKey;
try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(privateFile))) {
privateKey = factory.generatePrivate(new RSAPrivateKeySpec((BigInteger) in.readObject(), (BigInteger) in.readObject()));
}
return new KeyPair(publicKey, privateKey);
}
use of java.security.spec.RSAPrivateKeySpec in project remusic by aa112901.
the class RSAUtils method getPrivateKey.
/**
* 使用模和指数生成RSA私钥
* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
* /None/NoPadding】
*
* @param modulus 模
* @param exponent 指数
* @return
*/
public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {
try {
BigInteger b1 = new BigInteger(modulus);
BigInteger b2 = new BigInteger(exponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of java.security.spec.RSAPrivateKeySpec in project Terasology by MovingBlocks.
the class CertificateGenerator method generateSelfSigned.
/**
* Generates a self-signed certificate. These are used to identify servers.
*
* @return A matched pair of public and private certificates.
*/
public CertificatePair generateSelfSigned() {
keyPairGenerator.initialize(KEY_SIZE);
KeyPair kp = keyPairGenerator.genKeyPair();
try {
RSAPublicKeySpec pub = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
String uuid = UUID.randomUUID().toString();
signer.initSign(kp.getPrivate(), new SecureRandom());
signer.update(uuid.getBytes(Charsets.UTF_8));
signer.update(pub.getModulus().toByteArray());
signer.update(pub.getPublicExponent().toByteArray());
byte[] rawSig = signer.sign();
BigInteger signature = new BigInteger(rawSig);
PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature);
PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent());
return new CertificatePair(publicCert, privateCert);
} catch (InvalidKeySpecException | SignatureException | InvalidKeyException e) {
throw new RuntimeException("Unexpected exception generating certificate", e);
}
}
use of java.security.spec.RSAPrivateKeySpec in project Terasology by MovingBlocks.
the class CertificateGenerator method generate.
/**
* Generates a certificate signed by the given signer - a server will typically generate client identity certificates
* signed by its certificate.
*
* @param signingCertificate
* @return A matched pair of public and private certificates.
*/
public CertificatePair generate(PrivateIdentityCertificate signingCertificate) {
keyPairGenerator.initialize(KEY_SIZE);
KeyPair kp = keyPairGenerator.genKeyPair();
RSAPrivateKeySpec signingRSAKey = new RSAPrivateKeySpec(signingCertificate.getModulus(), signingCertificate.getExponent());
try {
PrivateKey signingKey = keyFactory.generatePrivate(signingRSAKey);
RSAPublicKeySpec pub = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
String uuid = UUID.randomUUID().toString();
signer.initSign(signingKey, new SecureRandom());
signer.update(uuid.getBytes(Charsets.UTF_8));
signer.update(pub.getModulus().toByteArray());
signer.update(pub.getPublicExponent().toByteArray());
byte[] rawSig = signer.sign();
BigInteger signature = new BigInteger(rawSig);
PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature);
PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent());
return new CertificatePair(publicCert, privateCert);
} catch (InvalidKeySpecException | SignatureException | InvalidKeyException e) {
throw new RuntimeException("Unexpected exception generating certificate", e);
}
}
use of java.security.spec.RSAPrivateKeySpec in project Terasology by MovingBlocks.
the class PrivateIdentityCertificate method decrypt.
/**
* Decrypts data encrypted by the paired public certificate
*
* @param data
* @return The decrypted data
* @throws BadEncryptedDataException If the data could not be decrypted due to an error with the data.
*/
public byte[] decrypt(byte[] data) throws BadEncryptedDataException {
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent);
try {
KeyFactory keyFactory = KeyFactory.getInstance(IdentityConstants.CERTIFICATE_ALGORITHM);
PrivateKey key = keyFactory.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance(IdentityConstants.CERTIFICATE_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException("Insufficient support for '" + IdentityConstants.CERTIFICATE_ALGORITHM + "', required for identity management", e);
} catch (InvalidKeySpecException | InvalidKeyException e) {
throw new RuntimeException("Unexpected error during encryption", e);
} catch (BadPaddingException | IllegalBlockSizeException e) {
throw new BadEncryptedDataException("Invalid encrypted data", e);
}
}
Aggregations