use of java.security.spec.RSAPrivateKeySpec in project j2objc by google.
the class RSAKeyTest method test_getModulus.
/**
* java.security.interfaces.RSAKey
* #getModulus()
* test covers following use cases
* Case 1: check private key
* Case 2: check public key
*/
public void test_getModulus() throws Exception {
KeyFactory gen = KeyFactory.getInstance("RSA");
final BigInteger n = BigInteger.valueOf(3233);
final BigInteger d = BigInteger.valueOf(2753);
final BigInteger e = BigInteger.valueOf(17);
RSAKey key = null;
// Case 1: check private key
key = (RSAKey) gen.generatePrivate(new RSAPrivateKeySpec(n, d));
assertEquals("invalid modulus", n, key.getModulus());
// Case 2: check public key
key = (RSAKey) gen.generatePublic(new RSAPublicKeySpec(n, e));
assertEquals("invalid modulus", n, key.getModulus());
}
use of java.security.spec.RSAPrivateKeySpec in project gocd by gocd.
the class HttpTestUtil method generateKeyPair.
private KeyPair generateKeyPair() {
try {
KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate();
RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic();
KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(), privateSeed.getPrivateExponent());
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(), publicSeed.getPublicExponent());
return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.security.spec.RSAPrivateKeySpec in project dble by actiontech.
the class DecryptUtil method decrypt.
public static String decrypt(PublicKey publicKey, String cipherText) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
try {
cipher.init(Cipher.DECRYPT_MODE, publicKey);
} catch (InvalidKeyException e) {
// IBM JDK not support Private key encryption, public key decryption
// so fake an PrivateKey for it
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
// It is a stateful object. so we need to get new one.
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
}
if (cipherText == null || cipherText.length() == 0) {
return cipherText;
}
byte[] cipherBytes = Base64.base64ToByteArray(cipherText);
byte[] plainBytes = cipher.doFinal(cipherBytes);
return new String(plainBytes);
}
use of java.security.spec.RSAPrivateKeySpec in project jmulticard by ctt-gob-es.
the class EncodedPrinter method printPrivate.
/**
* Imprime la codificación de una clave privada RSA.
* @throws Exception en cualquier error.
*/
@SuppressWarnings("static-method")
@Test
public void printPrivate() throws Exception {
final RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(new BigInteger(N), new BigInteger(D));
// $NON-NLS-1$
final KeyFactory factory = KeyFactory.getInstance("RSA");
final PrivateKey priv = factory.generatePrivate(privateSpec);
final byte[] enc = priv.getEncoded();
System.out.println(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
"(byte) 0x" + HexUtils.hexify(enc, true).replace("\n", "-").replace("-", ", (byte) 0x"));
}
use of java.security.spec.RSAPrivateKeySpec in project android2 by aqi00.
the class RSAUtil method getPrivateKey.
// 使用N、d值还原私钥
private static PrivateKey getPrivateKey(String modulus, String privateExponent, int radix) throws NoSuchAlgorithmException, InvalidKeySpecException {
BigInteger bigIntModulus = new BigInteger(modulus, radix);
BigInteger bigIntPrivateExponent = new BigInteger(privateExponent, radix);
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(bigIntModulus, bigIntPrivateExponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
Aggregations