當前位置: 首頁>>代碼示例>>Java>>正文


Java KeyFactory.getInstance方法代碼示例

本文整理匯總了Java中java.security.KeyFactory.getInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java KeyFactory.getInstance方法的具體用法?Java KeyFactory.getInstance怎麽用?Java KeyFactory.getInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.security.KeyFactory的用法示例。


在下文中一共展示了KeyFactory.getInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createPrivateKey

import java.security.KeyFactory; //導入方法依賴的package包/類
public static SigningPrivateKey createPrivateKey(BigInteger x, BigInteger p, BigInteger q, BigInteger g) throws NoSuchAlgorithmException, InvalidKeySpecException {
  if (x == null) {
    throw new IllegalArgumentException("x must not be null");
  }
  if (p == null) {
    throw new IllegalArgumentException("p must not be null");
  }
  if (q == null) {
    throw new IllegalArgumentException("q must not be null");
  }
  if (g == null) {
    throw new IllegalArgumentException("g must not be null");
  }
  KeySpec keySpec = new DSAPrivateKeySpec(x, p, q, g);
  KeyFactory keyFactory = KeyFactory.getInstance("DSA");
  DSAPrivateKey privateKey = (DSAPrivateKey) keyFactory.generatePrivate(keySpec);
  return new DSASigningPrivateKey(privateKey);
}
 
開發者ID:mozilla-mobile,項目名稱:FirefoxData-android,代碼行數:19,代碼來源:DSACryptoImplementation.java

示例2: sign

import java.security.KeyFactory; //導入方法依賴的package包/類
public static String sign(String content, String privateKey, boolean rsa2) {
	try {
		PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
				Base64.decode(privateKey));
		KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
		PrivateKey priKey = keyf.generatePrivate(priPKCS8);

		java.security.Signature signature = java.security.Signature
				.getInstance(getAlgorithms(rsa2));

		signature.initSign(priKey);
		signature.update(content.getBytes(DEFAULT_CHARSET));

		byte[] signed = signature.sign();

		return Base64.encode(signed);
	} catch (Exception e) {
		e.printStackTrace();
	}

	return null;
}
 
開發者ID:Leavessilent,項目名稱:QuanMinTV,代碼行數:23,代碼來源:SignUtils.java

示例3: encryptByPrivateKey

import java.security.KeyFactory; //導入方法依賴的package包/類
/**
 * 用私鑰加密
 *
 * @param data 加密數據
 * @param key  密鑰
 * @return
 * @throws Exception
 */
public static String encryptByPrivateKey(String data, String key)
        throws Exception {
    // 解密密鑰
    byte[] keyBytes = Base64.decode(key.getBytes(), Base64.DEFAULT);
    // 取私鑰
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
            keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

    // 對數據加密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    byte[] resultBytes = cipher.doFinal(data.getBytes("UTF-8"));
    return Base64.encodeToString(resultBytes, Base64.DEFAULT);
}
 
開發者ID:abook23,項目名稱:godlibrary,代碼行數:25,代碼來源:RSAUtlis.java

示例4: encryptByPublicKey

import java.security.KeyFactory; //導入方法依賴的package包/類
/**
 * 加密<br>
 * 用公鑰加密
 * 
 * @param data
 * @param key
 * @return
 * @throws Exception
 */
protected static byte[] encryptByPublicKey(byte[] data, String key) {
	try {
		// 對公鑰解密
		byte[] keyBytes = decryptBASE64(key);

		// 取得公鑰
		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key publicKey = keyFactory.generatePublic(x509KeySpec);

		// 對數據加密
		Cipher cipher = Cipher.getInstance(RSA_java);// Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);

		return cipher.doFinal(data);
	} catch (Exception e) {
		throw new RuntimeException("用公鑰加密出錯" + e.getMessage());
	}
}
 
開發者ID:wooui,項目名稱:springboot-training,代碼行數:29,代碼來源:RSACoder.java

示例5: getInstance

import java.security.KeyFactory; //導入方法依賴的package包/類
public static KeyFactory getInstance(final Provider provider) {
  try {
    return KeyFactory.getInstance(ALGORITHM, provider);
  } catch (NoSuchAlgorithmException ex) {
    throw new AssertionError(algorithmAssertionMsg, ex);
  }
}
 
開發者ID:Aptoide,項目名稱:AppCoins-ethereumj,代碼行數:8,代碼來源:ECKeyFactory.java

示例6: verify

import java.security.KeyFactory; //導入方法依賴的package包/類
/**
* RSA驗簽名檢查
* @param content 待簽名數據
* @param sign 簽名值
* @param alipay_public_key 支付寶公鑰
* @param input_charset 編碼格式
* @return 布爾值
*/
public static boolean verify(String content, String sign, String alipay_public_key, String input_charset)
{
	try 
	{
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] encodedKey = Base64.decode(alipay_public_key);
        PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));

	
		java.security.Signature signature = java.security.Signature
		.getInstance(SIGN_ALGORITHMS);
	
		signature.initVerify(pubKey);
		signature.update( content.getBytes(input_charset) );
	
		boolean bverify = signature.verify( Base64.decode(sign) );
		return bverify;
		
	} 
	catch (Exception e) 
	{
		e.printStackTrace();
	}
	
	return false;
}
 
開發者ID:Herbertyy,項目名稱:HerbertyyRepository,代碼行數:35,代碼來源:RSA.java

示例7: loadPrivateKey

import java.security.KeyFactory; //導入方法依賴的package包/類
/**
 * 還原私鑰,PKCS8EncodedKeySpec 用於構建私鑰的規範
 *
 * @param keyBytes
 * @return
 */
public static PrivateKey loadPrivateKey(byte[] keyBytes) {
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
            keyBytes);
    try {
        KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey privateKey = factory
                .generatePrivate(pkcs8EncodedKeySpec);
        return privateKey;
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
 
開發者ID:warlock-china,項目名稱:azeroth,代碼行數:21,代碼來源:RSA.java

示例8: getPublicKey

import java.security.KeyFactory; //導入方法依賴的package包/類
public static PublicKey getPublicKey(String algorithm, String publicKey) throws Exception {
	if (StringUtils.isBlank(publicKey) || StringUtils.isEmpty(algorithm)) {
		return null;
	}
	KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
	byte[] encodedKey = Base64Utils.decode(publicKey.getBytes());
	return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
}
 
開發者ID:zhaoqilong3031,項目名稱:spring-cloud-samples,代碼行數:9,代碼來源:RSAUtils.java

示例9: loadPrivateKey2

import java.security.KeyFactory; //導入方法依賴的package包/類
private static PrivateKey loadPrivateKey2() throws Exception {

	    String privateKeyPEM = getKeyFromFile("./keys/privatekey-pkcs8.pem");
	    // strip of header, footer, newlines, whitespaces
	    privateKeyPEM = privateKeyPEM
	            .replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "")
	            .replace("-----END ENCRYPTED PRIVATE KEY-----", "")
	            .replaceAll("\\s", "");

	    // decode to get the binary DER representation
	    byte[] privateKeyDER = Base64.getDecoder().decode(privateKeyPEM);


	    EncryptedPrivateKeyInfo epkInfo = new EncryptedPrivateKeyInfo(privateKeyDER);
	    System.out.println("Algo: ");
	    System.out.println(epkInfo.getAlgName());
	    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName());
	    PBEKeySpec pbeKeySpec = new PBEKeySpec("123".toCharArray());
	    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);

	    Cipher cipher = Cipher.getInstance(epkInfo.getAlgName());
	    cipher.init(Cipher.DECRYPT_MODE, pbeKey, epkInfo.getAlgParameters());

	    // return epkInfo.getKeySpec(cipher);


	    KeyFactory keyFactory2 = KeyFactory.getInstance("RSA");
	    PrivateKey privateKey = keyFactory2.generatePrivate(epkInfo.getKeySpec(cipher));
	    return privateKey;
	}
 
開發者ID:gahana,項目名稱:edge-jwt-sample,代碼行數:31,代碼來源:JWTUtil.java

示例10: generateRSAPublicKey

import java.security.KeyFactory; //導入方法依賴的package包/類
/**
 * 生成公鑰
 *
 * @param modulus
 * @param publicExponent
 * @return
 */
private static RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) {
    try {
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
                1, modulus), new BigInteger(1, publicExponent));
        KeyFactory keyFac = KeyFactory.getInstance(KEY_ALGORITHM);
        return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
    } catch (Exception e) {
        throw new RuntimeException("Error when generate rsaPubblicKey, errmsg: " + e.getMessage(), e);
    }

}
 
開發者ID:TIIEHenry,項目名稱:TIIEHenry-Android-SDK,代碼行數:19,代碼來源:RSAUtils.java

示例11: decryptByPrivateKey

import java.security.KeyFactory; //導入方法依賴的package包/類
/**
 * 用私鑰解密
 *
 * @param data
 * @param key
 * @return
 * @throws Exception
 */
public static byte[] decryptByPrivateKey(byte[] data, String key)
        throws Exception {
    byte[] keyBytes = BASE64.decode(key);   // 對密鑰解密

    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);   // 取得私鑰
    KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.getType());
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

    // 對數據解密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    return cipher.doFinal(data);
}
 
開發者ID:lzmlsfe,項目名稱:19porn,代碼行數:23,代碼來源:Codec.java

示例12: sign

import java.security.KeyFactory; //導入方法依賴的package包/類
/**
 * 用私鑰對信息生成數字簽名
 *
 * @param data       加密數據
 * @param privateKey 私鑰
 * @return
 * @throws Exception
 */
public static String sign(byte[] data, String privateKey) throws Exception {
    byte[] keyBytes = BASE64.decode(privateKey);        // 解密由base64編碼的私鑰
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);   // 構造PKCS8EncodedKeySpec對象
    KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.getType());    // KEY_ALGORITHM 指定的加密算法
    PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);        // 取私鑰匙對象
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);   // 用私鑰對信息生成數字簽名
    signature.initSign(priKey);
    signature.update(data);

    return BASE64.encodeToString(signature.sign());
}
 
開發者ID:ruiqiao2017,項目名稱:Renrentou,代碼行數:20,代碼來源:Codec.java

示例13: getSSLContext

import java.security.KeyFactory; //導入方法依賴的package包/類
private SSLContext getSSLContext() throws Exception {

        // generate certificate from cert string
        CertificateFactory cf = CertificateFactory.getInstance("X.509");

        // create a key store
        KeyStore ts = KeyStore.getInstance("JKS");
        KeyStore ks = KeyStore.getInstance("JKS");
        ts.load(null, null);
        ks.load(null, null);

        // import the trused cert
        ByteArrayInputStream is =
                    new ByteArrayInputStream(trustedCertStr.getBytes());
        Certificate trusedCert = cf.generateCertificate(is);
        is.close();
        ts.setCertificateEntry("rsa-trusted-2048", trusedCert);

        // generate the private key.
        String keySpecStr = targetPrivateKey;
        PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
                            Base64.getMimeDecoder().decode(keySpecStr));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPrivateKey priKey = (RSAPrivateKey)kf.generatePrivate(priKeySpec);

        Certificate[] chain = new Certificate[1];
        chain[0] = trusedCert;

        // import the key entry.
        ks.setKeyEntry("rsa-key-2048", priKey, passphrase, chain);

        // create SSL context
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, passphrase);

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(ts);

        SSLContext sslCtx = SSLContext.getInstance("TLSv1");
        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

        return sslCtx;
    }
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:44,代碼來源:DHEKeySizing.java

示例14: unmarshalKeyValue

import java.security.KeyFactory; //導入方法依賴的package包/類
PublicKey unmarshalKeyValue(Element kvtElem)
    throws MarshalException
{
    if (dsakf == null) {
        try {
            dsakf = KeyFactory.getInstance("DSA");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException
                ("unable to create DSA KeyFactory: " + e.getMessage());
        }
    }
    Element curElem = DOMUtils.getFirstChildElement(kvtElem);
    // check for P and Q
    if (curElem.getLocalName().equals("P")) {
        p = new DOMCryptoBinary(curElem.getFirstChild());
        curElem = DOMUtils.getNextSiblingElement(curElem, "Q");
        q = new DOMCryptoBinary(curElem.getFirstChild());
        curElem = DOMUtils.getNextSiblingElement(curElem);
    }
    if (curElem.getLocalName().equals("G")) {
        g = new DOMCryptoBinary(curElem.getFirstChild());
        curElem = DOMUtils.getNextSiblingElement(curElem, "Y");
    }
    y = new DOMCryptoBinary(curElem.getFirstChild());
    curElem = DOMUtils.getNextSiblingElement(curElem);
    if (curElem != null && curElem.getLocalName().equals("J")) {
        j = new DOMCryptoBinary(curElem.getFirstChild());
        // curElem = DOMUtils.getNextSiblingElement(curElem);
    }
    /*
    if (curElem != null) {
        seed = new DOMCryptoBinary(curElem.getFirstChild());
        curElem = DOMUtils.getNextSiblingElement(curElem);
        pgen = new DOMCryptoBinary(curElem.getFirstChild());
    }
    */
    //@@@ do we care about j, pgenCounter or seed?
    DSAPublicKeySpec spec = new DSAPublicKeySpec(y.getBigNum(),
                                                 p.getBigNum(),
                                                 q.getBigNum(),
                                                 g.getBigNum());
    return generatePublicKey(dsakf, spec);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:44,代碼來源:DOMKeyValue.java

示例15: getSSLContext

import java.security.KeyFactory; //導入方法依賴的package包/類
private static SSLContext getSSLContext(String trusedCertStr,
        String keyCertStr, String keySpecStr) throws Exception {

    // generate certificate from cert string
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    // create a key store
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, null);

    // import the trused cert
    Certificate trusedCert = null;
    ByteArrayInputStream is = null;
    if (trusedCertStr != null) {
        is = new ByteArrayInputStream(trusedCertStr.getBytes());
        trusedCert = cf.generateCertificate(is);
        is.close();

        ks.setCertificateEntry("RSA Export Signer", trusedCert);
    }

    if (keyCertStr != null) {
        // generate the private key.
        PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
                            Base64.getMimeDecoder().decode(keySpecStr));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPrivateKey priKey =
                (RSAPrivateKey)kf.generatePrivate(priKeySpec);

        // generate certificate chain
        is = new ByteArrayInputStream(keyCertStr.getBytes());
        Certificate keyCert = cf.generateCertificate(is);
        is.close();

        Certificate[] chain = null;
        if (trusedCert != null) {
            chain = new Certificate[2];
            chain[0] = keyCert;
            chain[1] = trusedCert;
        } else {
            chain = new Certificate[1];
            chain[0] = keyCert;
        }

        // import the key entry.
        ks.setKeyEntry("Whatever", priKey, passphrase, chain);
    }

    // create SSL context
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlgorithm);
    tmf.init(ks);

    SSLContext ctx = SSLContext.getInstance("TLS");
    if (keyCertStr != null && !keyCertStr.isEmpty()) {
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
        kmf.init(ks, passphrase);

        ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        ks = null;
    } else {
        ctx.init(null, tmf.getTrustManagers(), null);
    }

    return ctx;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:66,代碼來源:SelfIssuedCert.java


注:本文中的java.security.KeyFactory.getInstance方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。