当前位置: 首页>>代码示例>>Java>>正文


Java KeyFactory.generatePrivate方法代码示例

本文整理汇总了Java中java.security.KeyFactory.generatePrivate方法的典型用法代码示例。如果您正苦于以下问题:Java KeyFactory.generatePrivate方法的具体用法?Java KeyFactory.generatePrivate怎么用?Java KeyFactory.generatePrivate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.security.KeyFactory的用法示例。


在下文中一共展示了KeyFactory.generatePrivate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getKeyPair

import java.security.KeyFactory; //导入方法依赖的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

示例2: decryptByPrivateKey

import java.security.KeyFactory; //导入方法依赖的package包/类
/**
 * 用私钥解密
 *
 * @param data 加密数据
 * @param key  密钥
 * @return
 * @throws Exception
 */
public static String decryptByPrivateKey(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.DECRYPT_MODE, privateKey);
    byte[] dataBytes = Base64.decode(data.getBytes(), Base64.DEFAULT);
    byte[] resultBytes = cipher.doFinal(dataBytes);
    return new String(resultBytes, "UTF-8");
}
 
开发者ID:abook23,项目名称:godlibrary,代码行数:25,代码来源:RSAUtlis.java

示例3: sign

import java.security.KeyFactory; //导入方法依赖的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

示例4: testKeyPairGeneratorEccNewKeyFromExisting

import java.security.KeyFactory; //导入方法依赖的package包/类
@Test
public void testKeyPairGeneratorEccNewKeyFromExisting()
    throws NoSuchProviderException, NoSuchAlgorithmException,
           InvalidAlgorithmParameterException, InvalidKeySpecException {

    if (enabledCurves.size() > 0) {

        KeyPairGenerator kpg =
            KeyPairGenerator.getInstance("EC", "wolfJCE");

        ECGenParameterSpec ecSpec =
            new ECGenParameterSpec(enabledCurves.get(0));
        kpg.initialize(ecSpec);

        KeyPair kp = kpg.generateKeyPair();

        KeyFactory kf = KeyFactory.getInstance("EC");
        PublicKey pub = kf.generatePublic(new X509EncodedKeySpec(
                    kp.getPublic().getEncoded()));
        PrivateKey priv = kf.generatePrivate(new PKCS8EncodedKeySpec(
                    kp.getPrivate().getEncoded()));
    }
}
 
开发者ID:wolfSSL,项目名称:wolfcrypt-jni,代码行数:24,代码来源:WolfCryptKeyPairGeneratorTest.java

示例5: encryptByPrivate

import java.security.KeyFactory; //导入方法依赖的package包/类
/**
 * 私钥加密
 *
 * @param data       待加密数据
 * @param privateKey 密钥
 * @return byte[] 加密数据
 */
private static byte[] encryptByPrivate(byte[] data, byte[] privateKey) {
    byte[] result = null;
    // 得到私钥
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
    try {
        KeyFactory kf = KeyFactory.getInstance(RSA);
        PrivateKey keyPrivate = kf.generatePrivate(keySpec);
        // 数据加密
        Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);
        result = cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("RSA加密", "私钥加密失败");
    }
    return result;
}
 
开发者ID:JJS-CN,项目名称:JBase,代码行数:25,代码来源:RsaUtils.java

示例6: decryptByPrivate

import java.security.KeyFactory; //导入方法依赖的package包/类
/**
 * 使用私钥进行解密
 */
private static byte[] decryptByPrivate(byte[] encrypted, byte[] privateKey) {
    byte[] result = null;
    // 得到私钥
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
    try {
        KeyFactory kf = KeyFactory.getInstance(RSA);
        PrivateKey keyPrivate = kf.generatePrivate(keySpec);

        // 解密数据
        Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
        cp.init(Cipher.DECRYPT_MODE, keyPrivate);
        result = cp.doFinal(encrypted);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("RSA加密", "私钥解密失败");
    }
    return result;
}
 
开发者ID:JJS-CN,项目名称:JBase,代码行数:22,代码来源:RsaUtils.java

示例7: encryptByPrivateKey

import java.security.KeyFactory; //导入方法依赖的package包/类
/**
 * 私钥加密
 * 
 * @param data 待加密数据
 * @param key 私钥
 * @return byte[] 加密数据
 * @throws Exception
 */
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);
}
 
开发者ID:youngMen1,项目名称:JAVA-,代码行数:20,代码来源:RSACoder.java

示例8: loadPrivateKey

import java.security.KeyFactory; //导入方法依赖的package包/类
public static PrivateKey loadPrivateKey(Buffer keyValue) {
    if (keyValue == null) {
        throw new NullPointerException("Missing private key");
    }
    try {
        byte[] value = loadPem(keyValue, "PRIVATE KEY").get(0);
        KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA");
        return rsaKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(value));
    } catch (Exception e) {
        throw new RuntimeException("Problem loading private key", e);
    }
}
 
开发者ID:xkr47,项目名称:vertx-acme4j,代码行数:13,代码来源:PemLoader.java

示例9: getPrivateKey

import java.security.KeyFactory; //导入方法依赖的package包/类
/**
     * 得到私钥
     *
     * @param key
     *            密钥字符串(经过base64编码)
     * @throws Exception
     */
    public static PrivateKey getPrivateKey(String key) throws Exception {
        byte[] keyBytes;
//        keyBytes = (new BASE64Decoder()).decodeBuffer(key);
        keyBytes=Base64.decode(key);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }
 
开发者ID:Herbertyy,项目名称:HerbertyyRepository,代码行数:17,代码来源:RSAUtils.java

示例10: getKeyPair

import java.security.KeyFactory; //导入方法依赖的package包/类
public KeyPair getKeyPair(String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
	X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
			Base64.getDecoder().decode(getString(key + "_PUBLIC")));
	PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
			Base64.getDecoder().decode(getString(key + "_PRIVATE")));
	KeyFactory kf = KeyFactory.getInstance("RSA");
	return new KeyPair(kf.generatePublic(publicKeySpec), kf.generatePrivate(privateKeySpec));
}
 
开发者ID:biancso,项目名称:Mevius-IO,代码行数:9,代码来源:Configuration.java

示例11: createPrivateKey

import java.security.KeyFactory; //导入方法依赖的package包/类
public static SigningPrivateKey createPrivateKey(BigInteger n, BigInteger d) throws NoSuchAlgorithmException, InvalidKeySpecException {
  if (n == null) {
    throw new IllegalArgumentException("n must not be null");
  }
  if (d == null) {
    throw new IllegalArgumentException("d must not be null");
  }
  KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  KeySpec keySpec = new RSAPrivateKeySpec(n, d);
  RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
  return new RSASigningPrivateKey(privateKey);
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:13,代码来源:RSACryptoImplementation.java

示例12: decodePrivateKey

import java.security.KeyFactory; //导入方法依赖的package包/类
/**
 * Decode a PEM encoded private key string to an RSA PrivateKey
 * @param pemEncoded - PEM string for private key
 * @return PrivateKey
 * @throws Exception on decode failure
 */
public static PrivateKey decodePrivateKey(String pemEncoded) throws Exception {
    pemEncoded = removeBeginEnd(pemEncoded);
    byte[] pkcs8EncodedBytes = Base64.getDecoder().decode(pemEncoded);

    // extract the private key

    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey privKey = kf.generatePrivate(keySpec);
    return privKey;
}
 
开发者ID:eclipse,项目名称:microprofile-jwt-auth,代码行数:18,代码来源:TokenUtils.java

示例13: createInstance

import java.security.KeyFactory; //导入方法依赖的package包/类
@Override
protected PrivateKey createInstance() throws Exception {
    try (final InputStream privKey = this.location.getInputStream()) {
        final byte[] bytes = new byte[privKey.available()];
        privKey.read(bytes);
        final PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(bytes);
        final KeyFactory factory = KeyFactory.getInstance(this.algorithm);
        return factory.generatePrivate(privSpec);
    }
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:11,代码来源:PrivateKeyFactoryBean.java

示例14: encryptByPrivateKey

import java.security.KeyFactory; //导入方法依赖的package包/类
/**
     * <p>
     * 私钥加密
     * </p>
     *
     * @param data       源数据
     * @param privateKey 私钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPrivateKey(byte[] data, String privateKey)
            throws Exception {
        byte[] keyBytes = Base64Utils.decode(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
//        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, privateK);
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密  
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:40,代码来源:RSAUtils.java

示例15: main

import java.security.KeyFactory; //导入方法依赖的package包/类
@Override
    public void main(Provider provider) throws Exception {
        if (provider.getService("KeyAgreement", "DH") == null) {
            System.out.println("DH not supported, skipping");
            return;
        }
        try {
            DHPublicKeySpec publicSpec;
            DHPrivateKeySpec privateSpec;
            KeyFactory kf = KeyFactory.getInstance("DH", provider);
            KeyAgreement ka = KeyAgreement.getInstance("DH", provider);

            PrivateKey pr1 = kf.generatePrivate(new DHPrivateKeySpec(x1, p, g));
            PublicKey pu2 = kf.generatePublic(new DHPublicKeySpec(y2, p, g));
            PublicKey pu3 = kf.generatePublic(new DHPublicKeySpec(y3, p, g));

            ka.init(pr1);
            ka.doPhase(pu2, true);
            byte[] n2 = ka.generateSecret();
            if (Arrays.equals(s2, n2) == false) {
                throw new Exception("mismatch 2");
            }
            System.out.println("short ok");

            ka.init(pr1);
            ka.doPhase(pu3, true);
            byte[] n3 = ka.generateSecret();
            if (Arrays.equals(s3, n3) == false) {
                throw new Exception("mismatch 3");
            }
            System.out.println("normal ok");
        } catch (Exception ex) {
            System.out.println("Unexpected Exception: " + ex);
            ex.printStackTrace();
            throw ex;
        }

/*
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", provider);
        kpg.initialize(512);
//        KeyPair kp1 = kpg.generateKeyPair();
//      System.out.println(kp1.getPublic());
//      System.out.println(kp1.getPrivate());
        while (true) {
            KeyAgreement ka = KeyAgreement.getInstance("DH", provider);
            ka.init(pr1);
            KeyPair kp2 = kpg.generateKeyPair();
            ka.doPhase(kp2.getPublic(), true);
            byte[] sec = ka.generateSecret();
            if (sec.length == 64) {
                System.out.println(kp2.getPrivate());
                System.out.println(kp2.getPublic());
                System.out.println(toString(sec));
                break;
            }
        }
/**/
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:59,代码来源:TestShort.java


注:本文中的java.security.KeyFactory.generatePrivate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。