List of usage examples for java.security KeyFactory getAlgorithm
public final String getAlgorithm()
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 {/* ww w . j a v a 2 s .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: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 {//w w w. j a v a 2 s .c om 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:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?//from ww w . j a v a 2s . c om * * @param data * ? * @param key * * @return byte[] ? */ public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?//w w w . j a v a 2s .c o m * * @param data? * @param key * * @return byte[] ? */ public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:com.zf.decipher.DataEn.java
private static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey); PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey); PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm()); keyAgree.init(priKey);/*from w w w . ja v a 2 s . c om*/ keyAgree.doPhase(pubKey, true); return keyAgree.generateSecret(AES).getEncoded(); }
From source file:com.security.ch08_rsa.RSACoderTextKey.java
/** * //from www. java 2s. c o m * * @param data * ? * @param key * * @return byte[] ? * @throws Exception */ private static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception { // ? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); }
From source file:com.security.ch08_rsa.RSACoderTextKey.java
/** * //from ww w .j a v a 2s .c o m * * @param data * ? * @param key * * @return byte[] ? * @throws Exception */ private static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception { // ? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ? PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicKey); return cipher.doFinal(data); }
From source file:com.security.ch08_rsa.RSACoderTextKey.java
/** * ?//from w w w .j a v a2 s . co m * * @param data * ? * @param key * ? * @return byte[] ? * @throws Exception */ private static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:com.security.ch08_rsa.RSACoderTextKey.java
/** * ?/*w w w .j a va 2s.c o m*/ * * @param data * ? * @param key * ? * @return byte[] ? * @throws Exception */ private static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:com.zxy.commons.codec.rsa.RSAUtils.java
/** * <p>/*from w w w.java 2 s . c o m*/ * * </p> * * @param encryptedData ? * @param publicKey (BASE64?) * @return byte * @throws Exception Exception */ public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception { byte[] keyBytes = Base64.decodeBase64(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicK); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int index = 0; // ? while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); index++; offSet = index * MAX_DECRYPT_BLOCK; } byte[] decryptedData = out.toByteArray(); out.close(); return decryptedData; }