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


Java PKCS8EncodedKeySpec類代碼示例

本文整理匯總了Java中java.security.spec.PKCS8EncodedKeySpec的典型用法代碼示例。如果您正苦於以下問題:Java PKCS8EncodedKeySpec類的具體用法?Java PKCS8EncodedKeySpec怎麽用?Java PKCS8EncodedKeySpec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: save

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
/**
 * Saves a key pair.
 *
 * @param keyPair the key pair to save
 * @throws IOException if the files cannot be written
 * @since 1.0.0
 */
public void save(KeyPair keyPair) throws IOException {
    LOGGER.info("Saving key pair");
    final PrivateKey privateKey = keyPair.getPrivate();
    final PublicKey publicKey = keyPair.getPublic();

    // Store Public Key
    final File publicKeyFile = getKeyPath(publicKey);
    publicKeyFile.getParentFile().mkdirs(); // make directories if they do not exist
    final X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
    try (FileOutputStream fos = new FileOutputStream(publicKeyFile)) {
        fos.write(x509EncodedKeySpec.getEncoded());
    }

    // Store Private Key.
    final File privateKeyFile = getKeyPath(privateKey);
    privateKeyFile.getParentFile().mkdirs(); // make directories if they do not exist
    final PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    try (FileOutputStream fos = new FileOutputStream(privateKeyFile)) {
        fos.write(pkcs8EncodedKeySpec.getEncoded());
    }
}
 
開發者ID:stevespringett,項目名稱:Alpine,代碼行數:29,代碼來源:KeyManager.java

示例2: createPrivateKeyFromPemFile

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
private static PrivateKey createPrivateKeyFromPemFile(final String keyFileName) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException 
{
	// Loads a privte key from the specified key file name
    final PemReader pemReader = new PemReader(new FileReader(keyFileName));
    final PemObject pemObject = pemReader.readPemObject();
    final byte[] pemContent = pemObject.getContent();
    pemReader.close();
    final PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(pemContent);
    final KeyFactory keyFactory = getKeyFactoryInstance();
    final PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec);
    return privateKey;
}
 
開發者ID:PacktPublishing,項目名稱:MQTT-Essentials-A-Lightweight-IoT-Protocol,代碼行數:13,代碼來源:SecurityHelper.java

示例3: sign

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
/**
 * 開始簽名訂單
 *
 * @param content    待簽名內容
 * @param privateKey 私鑰
 * @return 簽名後的內容
 */
public static String sign(String content, String privateKey) {
    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(SIGN_ALGORITHMS);
        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:shenhuanet,項目名稱:AndroidOpen,代碼行數:24,代碼來源:SignUtils.java

示例4: getSecretKey

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
/**
 * 根據對方的公鑰和自己的私鑰生成本地密鑰
 */
public static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception{
	//實例化密鑰工廠
	KeyFactory keyFactory = KeyFactory.getInstance("DH");
	//將公鑰從字節數組轉換為publicKey
	X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey);
	PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
	//將私鑰從字節數組轉換為privateKey
	PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(privateKey);
	PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);
	//準備根據以上公鑰和私鑰生成本地密鑰SecretKey
	//先實例化KeyAgreement
	KeyAgreement keyAgreement = KeyAgreement.getInstance("DH");
	//用自己的私鑰初始化keyAgreement
	keyAgreement.init(priKey);
	//結合對方的公鑰進行運算
	keyAgreement.doPhase(pubKey, true);
	//開始生成本地密鑰secretKey   密鑰算法為對稱密碼算法
	SecretKey secretKey = keyAgreement.generateSecret("DES");  //DES、3DES、AES
	return secretKey.getEncoded();
}
 
開發者ID:laidu,項目名稱:java-learn,代碼行數:24,代碼來源:DHUtil.java

示例5: sign

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
/**
* RSA簽名
* @param content 待簽名數據
* @param privateKey 商戶私鑰
* @param input_charset 編碼格式
* @return 簽名值
*/
public static String sign(String content, String privateKey, String input_charset)
{
       try 
       {
       	PKCS8EncodedKeySpec priPKCS8 	= new PKCS8EncodedKeySpec( Base64.decode(privateKey) ); 
       	KeyFactory keyf 				= KeyFactory.getInstance("RSA");
       	PrivateKey priKey 				= keyf.generatePrivate(priPKCS8);

           java.security.Signature signature = java.security.Signature
               .getInstance(SIGN_ALGORITHMS);

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

           byte[] signed = signature.sign();
           
           return Base64.encode(signed);
       }
       catch (Exception e) 
       {
       	e.printStackTrace();
       }
       
       return null;
   }
 
開發者ID:dianbaer,項目名稱:epay,代碼行數:33,代碼來源:RSA.java

示例6: createKeyPairFromEncodedKeys

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
/**
 * Convert encoded private and public keys (bytes) to Private / PublicKey
 * interfaces and generate a KeyPair from them in order to construct a
 * Wallet object in the signIn method<br>
 * <b>Two different encoding</b>
 *
 * @param publicKeyBytes the public key with encoding X509
 * @param privateKeyBytes the private key with encoding PKCS8
 * @return the key pair
 */
public static KeyPair createKeyPairFromEncodedKeys(byte[] publicKeyBytes, byte[] privateKeyBytes) {
    // Generate specs
    final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
    final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
    
    try {
        // Create PublicKey and PrivateKey interfaces using the factory
        final PrivateKey privateKey = dsaKeyFactory.generatePrivate(privateKeySpec);
        final PublicKey publicKey = dsaKeyFactory.generatePublic(publicKeySpec);
        
        return new KeyPair(publicKey, privateKey);
    } catch (InvalidKeySpecException ex) {
        logAndAbort("Unable to create key pair. Abort!", ex);
    }
    return null;
}
 
開發者ID:StanIsAdmin,項目名稱:CrashCoin,代碼行數:27,代碼來源:Cryptography.java

示例7: sign

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
public static String sign(String content, String privateKey) {
	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(SIGN_ALGORITHMS);

		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:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:SignUtils.java

示例8: sign

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的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:egzosn,項目名稱:pay-java-android,代碼行數:23,代碼來源:SignUtils.java

示例9: encrypt

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
public static String encrypt(byte[] keyBytes, String plainText)
        throws Exception {
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = factory.generatePrivate(spec);
    Cipher cipher = Cipher.getInstance("RSA");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    } catch (InvalidKeyException e) {
        //For IBM JDK
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
        Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
    }

    byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
    String encryptedString = Base64.byteArrayToBase64(encryptedBytes);

    return encryptedString;
}
 
開發者ID:actiontech,項目名稱:dble,代碼行數:23,代碼來源:DecryptUtil.java

示例10: sign

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
/**
 * RSA簽名
 *
 * @param content       待簽名數據
 * @param privateKey    商戶私鑰
 * @param input_charset 編碼格式
 * @return 簽名值
 */
public static String sign(String content, String privateKey, String input_charset) {
	try {
		PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
		KeyFactory keyf = KeyFactory.getInstance("RSA");
		PrivateKey priKey = keyf.generatePrivate(priPKCS8);

		java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);

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

		byte[] signed = signature.sign();
		return new String(Base64.decodeBase64(signed));
	} catch (Exception e) {
		e.printStackTrace();
	}

	return null;
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:28,代碼來源:RSASign.java

示例11: sign

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的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:snowwolf10285,項目名稱:PicShow-zhaipin,代碼行數:23,代碼來源:SignUtils.java

示例12: engineGetKeySpec

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
protected KeySpec engineGetKeySpec(
    Key key,
    Class spec)
    throws InvalidKeySpecException
{
    if (spec.isAssignableFrom(PKCS8EncodedKeySpec.class) && key.getFormat().equals("PKCS#8"))
    {
        return new PKCS8EncodedKeySpec(key.getEncoded());
    }
    else if (spec.isAssignableFrom(X509EncodedKeySpec.class) && key.getFormat().equals("X.509"))
    {
        return new X509EncodedKeySpec(key.getEncoded());
    }

    throw new InvalidKeySpecException("not implemented yet " + key + " " + spec);
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:17,代碼來源:BaseKeyFactorySpi.java

示例13: getKeyPair

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
public KeyPair getKeyPair(PEMKeyPair keyPair)
    throws PEMException
{
    try
    {
        String algorithm =  keyPair.getPrivateKeyInfo().getPrivateKeyAlgorithm().getAlgorithm().getId();

        if (X9ObjectIdentifiers.id_ecPublicKey.getId().equals(algorithm))
        {
            algorithm = "ECDSA";
        }

        KeyFactory keyFactory = helper.createKeyFactory(algorithm);

        return new KeyPair(keyFactory.generatePublic(new X509EncodedKeySpec(keyPair.getPublicKeyInfo().getEncoded())),
                            keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded())));
    }
    catch (Exception e)
    {
        throw new PEMException("unable to convert key pair: " + e.getMessage(), e);
    }
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:23,代碼來源:JcaPEMKeyConverter.java

示例14: getPrivateKey

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
public PrivateKey getPrivateKey(PrivateKeyInfo privateKeyInfo)
    throws PEMException
{
    try
    {
        String algorithm =  privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm().getId();

        if (X9ObjectIdentifiers.id_ecPublicKey.getId().equals(algorithm))
        {
            algorithm = "ECDSA";
        }

        KeyFactory keyFactory = helper.createKeyFactory(algorithm);

        return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()));
    }
    catch (Exception e)
    {
        throw new PEMException("unable to convert key pair: " + e.getMessage(), e);
    }
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:22,代碼來源:JcaPEMKeyConverter.java

示例15: engineGeneratePrivate

import java.security.spec.PKCS8EncodedKeySpec; //導入依賴的package包/類
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
throws InvalidKeySpecException {
    try {
        if (keySpec instanceof DSAPrivateKeySpec) {
            DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec;
            return new DSAPrivateKey(dsaPrivKeySpec.getX(),
                                     dsaPrivKeySpec.getP(),
                                     dsaPrivKeySpec.getQ(),
                                     dsaPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DSAPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification: " + e.getMessage());
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:35,代碼來源:DSAKeyFactory.java


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