use of java.security.spec.KeySpec in project otertool by wuntee.
the class JarSigner15 method sign.
public static File sign(File jar, String newName, String keystoreLocation, String alias, char[] passwd, char[] keypasswd) throws Exception {
FileInputStream fileIn = new FileInputStream(keystoreLocation);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(fileIn, passwd);
java.security.cert.Certificate[] chain = keyStore.getCertificateChain(alias);
X509Certificate[] certChain = new X509Certificate[chain.length];
CertificateFactory cf = CertificateFactory.getInstance("X.509");
for (int count = 0; count < chain.length; count++) {
ByteArrayInputStream certIn = new ByteArrayInputStream(chain[0].getEncoded());
X509Certificate cert = (X509Certificate) cf.generateCertificate(certIn);
certChain[count] = cert;
}
Key key = keyStore.getKey(alias, keypasswd);
logger.debug("Algo: " + key.getAlgorithm());
KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
KeySpec keySpec = keyFactory.getKeySpec(key, DSAPrivateKeySpec.class);
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
/*Key key = keyStore.getKey(alias, keypasswd);
X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);*/
JarSigner15 jarSigner = new JarSigner15(alias, privateKey, certChain);
JarFile jarFile = new JarFile(jar.getCanonicalPath());
File newJar = new File(jar.getParentFile(), newName);
OutputStream outStream = new FileOutputStream(newJar);
jarSigner.signJarFile(jarFile, outStream);
fileIn.close();
return newJar;
}
use of java.security.spec.KeySpec in project azure-sdk-for-java by Azure.
the class KeyOperationsTest method getWellKnownKey.
private static KeyPair getWellKnownKey() throws Exception {
BigInteger modulus = new BigInteger("27266783713040163753473734334021230592631652450892850648620119914958066181400432364213298181846462385257448168605902438305568194683691563208578540343969522651422088760509452879461613852042845039552547834002168737350264189810815735922734447830725099163869215360401162450008673869707774119785881115044406101346450911054819448375712432746968301739007624952483347278954755460152795801894283389540036131881712321193750961817346255102052653789197325341350920441746054233522546543768770643593655942246891652634114922277138937273034902434321431672058220631825053788262810480543541597284376261438324665363067125951152574540779");
BigInteger publicExponent = new BigInteger("65537");
BigInteger privateExponent = new BigInteger("10466613941269075477152428927796086150095892102279802916937552172064636326433780566497000814207416485739683286961848843255766652023400959086290344987308562817062506476465756840999981989957456897020361717197805192876094362315496459535960304928171129585813477132331538577519084006595335055487028872410579127692209642938724850603554885478763205394868103298473476811627231543504190652483290944218004086457805431824328448422034887148115990501701345535825110962804471270499590234116100216841170344686381902328362376624405803648588830575558058257742073963036264273582756620469659464278207233345784355220317478103481872995809");
BigInteger primeP = new BigInteger("175002941104568842715096339107566771592009112128184231961529953978142750732317724951747797764638217287618769007295505214923187971350518217670604044004381362495186864051394404165602744235299100790551775147322153206730562450301874236875459336154569893255570576967036237661594595803204808064127845257496057219227");
BigInteger primeQ = new BigInteger("155807574095269324897144428622185380283967159190626345335083690114147315509962698765044950001909553861571493035240542031420213144237033208612132704562174772894369053916729901982420535940939821673277140180113593951522522222348910536202664252481405241042414183668723338300649954708432681241621374644926879028977");
BigInteger primeExponentP = new BigInteger("79745606804504995938838168837578376593737280079895233277372027184693457251170125851946171360348440134236338520742068873132216695552312068793428432338173016914968041076503997528137698610601222912385953171485249299873377130717231063522112968474603281996190849604705284061306758152904594168593526874435238915345");
BigInteger primeExponentQ = new BigInteger("80619964983821018303966686284189517841976445905569830731617605558094658227540855971763115484608005874540349730961777634427740786642996065386667564038755340092176159839025706183161615488856833433976243963682074011475658804676349317075370362785860401437192843468423594688700132964854367053490737073471709030801");
BigInteger crtCoefficient = new BigInteger("2157818511040667226980891229484210846757728661751992467240662009652654684725325675037512595031058612950802328971801913498711880111052682274056041470625863586779333188842602381844572406517251106159327934511268610438516820278066686225397795046020275055545005189953702783748235257613991379770525910232674719428");
KeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
KeySpec privateKeySpec = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return new KeyPair(keyFactory.generatePublic(publicKeySpec), keyFactory.generatePrivate(privateKeySpec));
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class SecretKeyFactoryTest method test_PBKDF2_8BIT.
private void test_PBKDF2_8BIT(char[] password, byte[] salt, int iterations, int keyLength, byte[] expected) throws Exception {
if (!StandardNames.IS_RI) {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1And8bit");
KeySpec ks = new PBEKeySpec(password, salt, iterations, keyLength);
SecretKey key = factory.generateSecret(ks);
assertTrue(Arrays.equals(expected, key.getEncoded()));
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class KeyFactory2Test method test_generatePublicLjava_security_spec_KeySpec.
public void test_generatePublicLjava_security_spec_KeySpec() throws Exception {
// java.security.KeyFactory.generatePublic(java.security.spec.KeySpec)
for (int i = 0; i < keyfactAlgs.length; i++) {
KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
// We don't use getInstance
SecureRandom random = new SecureRandom();
keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
KeyPair keys = keyGen.generateKeyPair();
if (keepalive != null) {
keepalive.interrupt();
}
KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), StandardNames.getPublicKeySpecClass(keyfactAlgs[i]));
PublicKey publicKey = fact.generatePublic(publicKeySpec);
assertEquals("generatePublic generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPublic().getEncoded()), Arrays.toString(publicKey.getEncoded()));
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class KeyFactoryTest method testGeneratePrivate.
@SuppressWarnings("unchecked")
public void testGeneratePrivate() {
KeyFactory factory = null;
try {
factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
assertNotNull(factory);
try {
TestPrivateKey key = new TestPrivateKey();
TestPrivateKeySpec keySpec = new TestPrivateKeySpec(key);
PrivateKey privateKey = factory.generatePrivate(keySpec);
assertNotNull(privateKey);
assertTrue(Arrays.equals(key.getEncoded(), privateKey.getEncoded()));
} catch (InvalidKeySpecException e) {
fail("unexpected exception: " + e);
}
KeySpec[] keySpecs = { new TestPublicKeySpec(new TestPublicKey()), null, new DSAPublicKeySpec(null, null, null, null) };
Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class };
for (int i = 0; i < keySpecs.length; i++) {
KeySpec keySpec = keySpecs[i];
exceptionThrown = false;
String message = "generatePrivate(" + (keySpec == null ? "null" : keySpec.toString()) + ")";
try {
factory.generatePrivate(keySpec);
} catch (Exception e) {
checkException(message, e, exceptions[i]);
} finally {
checkException(message, null, exceptions[i]);
}
}
}
Aggregations