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