List of usage examples for java.security KeyFactory generatePrivate
public final PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException
From source file:Main.java
public static PrivateKey getPrivateKey(String key) { try {//from w ww .j a v a2 s.co m Base64 base64_decoder = new Base64(); byte[] byteKey = base64_decoder.decode(key.getBytes()); // , // Base64.DEFAULT); PKCS8EncodedKeySpec X509privateKey = new PKCS8EncodedKeySpec(byteKey); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(X509privateKey); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static PrivateKey getPrivateKey(byte[] keyBytes) throws NoSuchAlgorithmException, InvalidKeySpecException { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; }
From source file:Main.java
public static PrivateKey getPrivateKey(String modulus, String privateExponent) throws NoSuchAlgorithmException, InvalidKeySpecException { BigInteger bigIntModulus = new BigInteger(modulus); BigInteger bigIntPrivateExponent = new BigInteger(privateExponent); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent); KeyFactory keyFactory = KeyFactory.getInstance(RSA); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; }
From source file:aiai.apps.commons.utils.SecUtils.java
public static PrivateKey getPrivateKey(String keyBase64) throws NoSuchAlgorithmException, InvalidKeySpecException { byte[] keyBytes = Base64.decodeBase64(keyBase64); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(spec); }
From source file:mx.bigdata.cfdi.security.KeyLoader.java
public static PrivateKey loadPKCS8PrivateKey(InputStream in, String passwd) throws Exception { byte[] decrypted = (passwd != null) ? getBytes(in, passwd.toCharArray()) : getBytes(in); PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(decrypted); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(keysp); }
From source file:com.java.demo.RsaDemo.java
public static byte[] Encrypt(String str) { try {/*w w w .j av a 2 s. c om*/ PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(rSAPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(pKCS8EncodedKeySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initSign(privateKey); signature.update(str.getBytes()); byte[] result = signature.sign(); return result; } catch (Exception e) { System.out.println(e.getMessage()); return null; } }
From source file:cn.lynx.emi.license.GenerateLicense.java
private static final String encrypt(String key, String data) { byte[] corekey = Base64.decodeBase64(key); PKCS8EncodedKeySpec pkspec = new PKCS8EncodedKeySpec(corekey); try {/*from ww w.j a v a2 s . c o m*/ KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key privateKey = keyFactory.generatePrivate(pkspec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] encData = cipher.doFinal(data.getBytes("UTF-8")); System.out.println("after encrypt, len=" + encData.length); return Base64.encodeBase64String(encData); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:be.solidx.hot.utils.SSLUtils.java
protected static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException { PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory factory = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) factory.generatePrivate(spec); }
From source file:com.floreantpos.license.FiveStarPOSLicenseGenerator.java
private static PrivateKey readPrivateKey(InputStream privateKey) throws FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(privateKey)); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); PrivateKey key = keyFactory.generatePrivate(keySpec); return key;//w w w .jav a 2 s. c o m }
From source file:com.github.ibole.infrastructure.security.ECDSA.java
public static void jdkECDSA() { try {// w w w . ja v a2s . c o m // 1.? KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC"); keyPairGenerator.initialize(256); KeyPair keyPair = keyPairGenerator.generateKeyPair(); ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic(); ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate(); // 2.?? PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("EC"); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Signature signature = Signature.getInstance("SHA256withECDSA"); signature.initSign(privateKey); signature.update(src.getBytes()); byte[] res = signature.sign(); System.out.println("??" + Base64.encodeBase64String(res)); // 3.??? X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded()); keyFactory = KeyFactory.getInstance("EC"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); signature = Signature.getInstance("SHA256withECDSA"); signature.initVerify(publicKey); signature.update(src.getBytes()); boolean bool = signature.verify(res); System.out.println("?" + bool); } catch (Exception e) { e.printStackTrace(); } }