本文整理汇总了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);
}
}
示例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");
}
示例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;
}
示例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()));
}
}
示例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;
}
示例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;
}
示例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);
}
示例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);
}
}
示例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;
}
示例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));
}
示例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);
}
示例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;
}
示例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);
}
}
示例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;
}
示例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;
}
}
/**/
}