List of usage examples for java.security KeyFactory generatePublic
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException
From source file:Main.java
public static RSAPublicKey buildPublicKeyFromBase64String(String key) throws InvalidKeySpecException, NoSuchAlgorithmException { byte[] byteKey = Base64.decode(key.getBytes(), Base64.NO_WRAP | Base64.URL_SAFE); X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); KeyFactory kf = KeyFactory.getInstance("RSA"); return (RSAPublicKey) kf.generatePublic(X509publicKey); }
From source file:Main.java
public static PublicKey getPublicKey(String key) { try {// ww w .j a va2 s. c o m Base64 base64_decoder = new Base64(); byte[] byteKey = base64_decoder.decode(key.getBytes()); // , // Base64.DEFAULT); X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(X509publicKey); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:cn.lynx.emi.license.ViewLicense.java
private static final String _decrypt(String data) { byte[] corekey = Base64.decodeBase64(LICENSE_CORE_KEY); byte[] rawData = Base64.decodeBase64(data); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(corekey); try {/* w ww.java2s . c o m*/ KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicKey); return new String(cipher.doFinal(rawData), "UTF-8"); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:aiai.apps.commons.utils.SecUtils.java
public static PublicKey getPublicKey(String keyBase64) throws NoSuchAlgorithmException, InvalidKeySpecException { byte[] keyBytes = Base64.decodeBase64(keyBase64); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(spec); }
From source file:com.java.demo.RsaDemo.java
public static boolean Verify(String str, byte[] enstr) { try {/*from www . j a va 2 s .co m*/ X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rSAPublicKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initVerify(publicKey); signature.update(str.getBytes()); boolean result = signature.verify(enstr); return result; } catch (Exception e) { System.out.println("com.Java.Demo.RsaDemo.Verify()" + e.getMessage()); return false; } }
From source file:se.curity.examples.oauth.jwt.RsaPublicKeyCreator.java
public static PublicKey createPublicKey(String modulus, String exponent) throws InvalidKeySpecException, NoSuchAlgorithmException { Base64 decoder = new Base64(true); BigInteger bigModulus = new BigInteger(1, decoder.decode(modulus)); BigInteger bigExponent = new BigInteger(1, decoder.decode(exponent)); RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(bigModulus, bigExponent); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); return publicKey; }
From source file:Main.java
public static PublicKey pubKeyFromJwk(String jwkp) { PublicKey pubKey = null;/*from w w w . j a va2 s . c o m*/ try { JSONObject jk = new JSONObject(jwkp).getJSONArray("keys").getJSONObject(0); BigInteger n = new BigInteger(1, decodeB64(jk.getString("n"))); BigInteger e = new BigInteger(1, decodeB64(jk.getString("e"))); RSAPublicKeySpec pubRsaSpec = new RSAPublicKeySpec(n, e); KeyFactory keyfact = KeyFactory.getInstance("RSA", "SC"); pubKey = keyfact.generatePublic(pubRsaSpec); } catch (Exception e) { e.printStackTrace(); } return pubKey; }
From source file:com.vimukti.accounter.developer.api.PublicKeyGenerator.java
private static void generate() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, KeyStoreException, CertificateException, IOException, URISyntaxException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed("VimTech".getBytes("UTF-8")); keyGen.initialize(1024, random);//from w ww . jav a 2s. c om KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); System.out.println(priv); System.out.println(pub); byte[] encoded = pub.getEncoded(); byte[] encodeBase64 = Base64.encodeBase64(encoded); System.out.println("Public Key:" + new String(encodeBase64)); byte[] encodedPrv = priv.getEncoded(); byte[] encodeBase64Prv = Base64.encodeBase64(encodedPrv); System.out.println("Private Key:" + new String(encodeBase64Prv)); byte[] decodeBase64 = Base64.decodeBase64(encodeBase64); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodeBase64); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); System.out.println(keyFactory.generatePublic(pubKeySpec).equals(pub)); }
From source file:Main.java
static PublicKey getRsaPublicKey(String n, String e) { BigInteger rsaN = null;//from w w w . java2 s .c o m BigInteger rsaE = null; try { rsaN = new BigInteger(n); rsaE = new BigInteger(e); } catch (Exception ex) { ex.printStackTrace(); } RSAPublicKeySpec pubRsaSpec = new RSAPublicKeySpec(rsaN, rsaE); try { KeyFactory keyfact = KeyFactory.getInstance("RSA", "SC"); PublicKey pk = keyfact.generatePublic(pubRsaSpec); Log.d("getRsaPublicKey", "pubRsaKey OK " + pk.getFormat()); return pk; } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:net.sf.zekr.common.util.CryptoUtils.java
public static boolean verify(byte[] text, byte[] sigBytes) throws GeneralSecurityException { X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(PUBLIC_KEY); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); PublicKey pubKey = keyFactory.generatePublic(pubSpec); return verify(text, pubKey, sigBytes); }