use of java.security.spec.ECGenParameterSpec in project benchmark by seelunzi.
the class Wallet method generateKeyPair.
/**
* 使用ECC(椭圆曲线算法)生成公私钥
*/
public void generateKeyPair() {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDSA", "BC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("prime192v1");
keyPairGenerator.initialize(ecSpec, random);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
privateKey = keyPair.getPrivate();
publicKey = keyPair.getPublic();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.security.spec.ECGenParameterSpec in project karaf by apache.
the class PublickeyLoginModule method equals.
public static boolean equals(PublicKey key, String storedKey) throws FailedLoginException {
try {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(storedKey)));
String identifier = readString(dis);
if (key instanceof DSAPublicKey) {
if (!"ssh-dss".equals(identifier)) {
return false;
}
BigInteger p = readBigInteger(dis);
BigInteger q = readBigInteger(dis);
BigInteger g = readBigInteger(dis);
BigInteger y = readBigInteger(dis);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
return key.equals(generatedPublicKey);
} else if (key instanceof RSAKey) {
if (!"ssh-rsa".equals(identifier)) {
return false;
}
BigInteger exponent = readBigInteger(dis);
BigInteger modulus = readBigInteger(dis);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
return key.equals(generatedPublicKey);
} else if (key instanceof ECPublicKey) {
String ecIdentifier = readString(dis);
if (!identifier.equals("ecdsa-sha2-" + ecIdentifier) || !nistSecMap.containsKey(ecIdentifier)) {
return false;
}
// Overall size of the x + y coordinates. We only support uncompressed points here, so
// to read x + y we ignore the "04" byte using (size - 1) / 2
int size = dis.readInt();
byte[] bytes = new byte[(size - 1) / 2];
dis.skipBytes(1);
dis.read(bytes, 0, bytes.length);
BigInteger x = new BigInteger(bytes);
dis.read(bytes, 0, bytes.length);
BigInteger y = new BigInteger(bytes);
KeyFactory keyFactory = KeyFactory.getInstance("EC");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec(nistSecMap.get(ecIdentifier)));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPoint pubPoint = new ECPoint(x, y);
KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
PublicKey generatedPublicKey = keyFactory.generatePublic(keySpec);
return key.equals(generatedPublicKey);
} else {
throw new FailedLoginException("Unsupported key type " + key.getClass().toString());
}
} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException e) {
throw new FailedLoginException("Unable to check public key");
}
}
use of java.security.spec.ECGenParameterSpec in project karaf by apache.
the class PublicKeyLoginModuleTest method testECLogin.
@Test
public void testECLogin() throws Exception {
Properties options = getLoginModuleOptions();
PublickeyLoginModule module = new PublickeyLoginModule();
Subject subject = new Subject();
String x = "-29742501866672735446035294501787338870744851402037490785638836399245997090445";
String y = "-47637824304079393691947094099226900728731860400898598466261954347951527449659";
KeyFactory keyFactory = KeyFactory.getInstance("EC");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPoint pubPoint = new ECPoint(new BigInteger(x), new BigInteger(y));
KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
module.initialize(subject, new NamePubkeyCallbackHandler("ec", publicKey), null, options);
assertEquals("Precondition", 0, subject.getPrincipals().size());
assertTrue(module.login());
assertTrue(module.commit());
assertFalse(subject.getPrincipals().isEmpty());
assertThat("ec", isIn(names(subject.getPrincipals(UserPrincipal.class))));
assertThat("ssh", isIn(names(subject.getPrincipals(RolePrincipal.class))));
assertTrue(module.logout());
assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
use of java.security.spec.ECGenParameterSpec in project wycheproof by google.
the class EcdsaTest method testBasic.
/**
* This test checks the basic functionality of ECDSA. It can also be used to generate simple test
* vectors.
*/
public void testBasic() throws Exception {
String algorithm = "SHA256WithECDSA";
String hashAlgorithm = "SHA-256";
String message = "Hello";
String curve = "secp256r1";
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
keyGen.initialize(ecSpec);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
byte[] messageBytes = message.getBytes("UTF-8");
Signature signer = Signature.getInstance(algorithm);
Signature verifier = Signature.getInstance(algorithm);
signer.initSign(priv);
signer.update(messageBytes);
byte[] signature = signer.sign();
verifier.initVerify(pub);
verifier.update(messageBytes);
assertTrue(verifier.verify(signature));
// Extract some parameters.
byte[] rawHash = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
ECParameterSpec params = priv.getParams();
// Print keys and signature, so that it can be used to generate new test vectors.
System.out.println("Message:" + message);
System.out.println("Hash:" + TestUtil.bytesToHex(rawHash));
System.out.println("Curve:" + curve);
System.out.println("Order:" + params.getOrder().toString());
System.out.println("Private key:");
System.out.println("S:" + priv.getS().toString());
System.out.println("encoded:" + TestUtil.bytesToHex(priv.getEncoded()));
System.out.println("Public key:");
ECPoint w = pub.getW();
System.out.println("X:" + w.getAffineX().toString());
System.out.println("Y:" + w.getAffineY().toString());
System.out.println("encoded:" + TestUtil.bytesToHex(pub.getEncoded()));
System.out.println("Signature:" + TestUtil.bytesToHex(signature));
System.out.println("r:" + extractR(signature).toString());
System.out.println("s:" + extractS(signature).toString());
}
use of java.security.spec.ECGenParameterSpec in project wycheproof by google.
the class EciesTest method testIsAlias.
/**
* Tests whether algorithmA is an alias of algorithmB by encrypting with algorithmA and decrypting
* with algorithmB.
*/
@SuppressWarnings("InsecureCryptoUsage")
public void testIsAlias(String algorithmA, String algorithmB) throws Exception {
Cipher eciesA;
Cipher eciesB;
// Allowing tests to be skipped, because we don't want to encourage abbreviations.
try {
eciesA = Cipher.getInstance(algorithmA);
} catch (NoSuchAlgorithmException ex) {
System.out.println("Skipping because of:" + ex.toString());
return;
}
try {
eciesB = Cipher.getInstance(algorithmB);
} catch (NoSuchAlgorithmException ex) {
System.out.println("Skipping because of:" + ex.toString());
return;
}
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
KeyPairGenerator kf = KeyPairGenerator.getInstance("EC");
kf.initialize(ecSpec);
KeyPair keyPair = kf.generateKeyPair();
byte[] message = "Hello".getBytes("UTF-8");
eciesA.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
byte[] ciphertext = eciesA.doFinal(message);
eciesB.init(Cipher.DECRYPT_MODE, keyPair.getPrivate(), eciesB.getParameters());
byte[] decrypted = eciesB.doFinal(ciphertext);
assertEquals(TestUtil.bytesToHex(message), TestUtil.bytesToHex(decrypted));
}
Aggregations