use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.
the class RSAPrivateCrtKeySpecTest method testGetPublicExponent.
/**
* Test for <code>getPublicExponent()</code> method<br>
* Assertion: returns public exponent
*/
public final void testGetPublicExponent() {
RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
assertTrue(BigInteger.valueOf(5L).equals(ks.getPublicExponent()));
}
use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.
the class RSAPrivateCrtKeySpecTest method testRSAPrivateCrtKeySpec02.
/**
* Test #2 for <code>RSAPrivateCrtKeySpec</code> constructor
* Assertion: Constructs <code>RSAPrivateCrtKeySpec</code>
* object using valid parameters
*/
public final void testRSAPrivateCrtKeySpec02() {
KeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
assertTrue(ks instanceof RSAPrivateKeySpec);
}
use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.
the class RSAPrivateCrtKeySpecTest method testRSAPrivateCrtKeySpec01.
/**
* Test #1 for <code>RSAPrivateCrtKeySpec</code> constructor
* Assertion: Constructs <code>RSAPrivateCrtKeySpec</code>
* object using valid parameters
*/
public final void testRSAPrivateCrtKeySpec01() {
KeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
assertTrue(ks instanceof RSAPrivateCrtKeySpec);
}
use of java.security.spec.RSAPrivateCrtKeySpec in project qpid-broker-j by apache.
the class SSLUtil method getRSAKeySpec.
private static RSAPrivateCrtKeySpec getRSAKeySpec(byte[] keyBytes) throws InvalidKeySpecException {
ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
try {
// PKCS#1 is encoded as a DER sequence of:
// (version, modulus, publicExponent, privateExponent, primeP, primeQ,
// primeExponentP, primeExponentQ, crtCoefficient)
int tag = ((int) buffer.get()) & 0xff;
// check tag is that of a sequence
if (((tag & 0x20) != 0x20) || ((tag & 0x1F) != 0x10)) {
throw new InvalidKeySpecException("Unable to parse key as PKCS#1 format");
}
int length = getLength(buffer);
buffer = buffer.slice();
buffer.limit(length);
// first tlv is version - which we'll ignore
byte versionTag = buffer.get();
int versionLength = getLength(buffer);
buffer.position(buffer.position() + versionLength);
RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer), getInteger(buffer));
return keySpec;
} catch (BufferUnderflowException e) {
throw new InvalidKeySpecException("Unable to parse key as PKCS#1 format");
}
}
use of java.security.spec.RSAPrivateCrtKeySpec in project java by kubernetes-client.
the class SSLUtils method loadKey.
public static PrivateKey loadKey(InputStream keyInputStream, String clientKeyAlgo) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
// Try PKCS7 / EC
if (clientKeyAlgo.equals("EC")) {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
PEMParser pemParser = new PEMParser(new InputStreamReader(keyInputStream));
Object pemObject;
while ((pemObject = pemParser.readObject()) != null) {
if (pemObject instanceof PEMKeyPair) {
return new JcaPEMKeyConverter().getKeyPair(((PEMKeyPair) pemObject)).getPrivate();
}
}
}
byte[] keyBytes = decodePem(keyInputStream);
// Try PKCS1 / RSA
if (clientKeyAlgo.equals("RSA")) {
RSAPrivateCrtKeySpec keySpec = decodePKCS1(keyBytes);
return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
}
// Try PKCS8
// TODO: There _has_ to be a better way to do this, but I spent >
// 2 hours trying to find it and failed...
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
try {
return KeyFactory.getInstance("RSA").generatePrivate(spec);
} catch (InvalidKeySpecException ex) {
// ignore if it's not RSA
}
try {
return KeyFactory.getInstance("ECDSA").generatePrivate(spec);
} catch (InvalidKeySpecException ex) {
// ignore if it's not DSA
}
throw new InvalidKeySpecException("Unknown type of PKCS8 Private Key, tried RSA and ECDSA");
}
Aggregations