本文整理匯總了Java中java.security.spec.X509EncodedKeySpec類的典型用法代碼示例。如果您正苦於以下問題:Java X509EncodedKeySpec類的具體用法?Java X509EncodedKeySpec怎麽用?Java X509EncodedKeySpec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
X509EncodedKeySpec類屬於java.security.spec包,在下文中一共展示了X509EncodedKeySpec類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: save
import java.security.spec.X509EncodedKeySpec; //導入依賴的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: encodePublicKey
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
private static byte[] encodePublicKey(PublicKey publicKey)
throws InvalidKeyException, NoSuchAlgorithmException {
byte[] encodedPublicKey = null;
if ("X.509".equals(publicKey.getFormat())) {
encodedPublicKey = publicKey.getEncoded();
}
if (encodedPublicKey == null) {
try {
encodedPublicKey =
KeyFactory.getInstance(publicKey.getAlgorithm())
.getKeySpec(publicKey, X509EncodedKeySpec.class)
.getEncoded();
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(
"Failed to obtain X.509 encoded form of public key " + publicKey
+ " of class " + publicKey.getClass().getName(),
e);
}
}
if ((encodedPublicKey == null) || (encodedPublicKey.length == 0)) {
throw new InvalidKeyException(
"Failed to obtain X.509 encoded form of public key " + publicKey
+ " of class " + publicKey.getClass().getName());
}
return encodedPublicKey;
}
示例3: toPublicKey
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
PublicKey toPublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo)
throws CRMFException
{
try
{
X509EncodedKeySpec xspec = new X509EncodedKeySpec(subjectPublicKeyInfo.getEncoded());
AlgorithmIdentifier keyAlg = subjectPublicKeyInfo.getAlgorithm();
return createKeyFactory(keyAlg.getAlgorithm()).generatePublic(xspec);
}
catch (Exception e)
{
throw new CRMFException("invalid key: " + e.getMessage(), e);
}
}
示例4: verify
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
/**
* RSA驗簽名檢查
* @param content 待簽名數據
* @param sign 簽名值
* @param ali_public_key 支付寶公鑰
* @param input_charset 編碼格式
* @return 布爾值
*/
public static boolean verify(String content, String sign, String ali_public_key, String input_charset)
{
try
{
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = Base64.decode(ali_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;
}
示例5: verify
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
/**
* 校驗數字簽名
*
* @param data 加密數據
* @param publicKey 公鑰
* @param sign 數字簽名
* @return
* @throws Exception
*/
public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
//解密公鑰
byte[] keyBytes = Base64.decode(publicKey.getBytes(), Base64.DEFAULT);
//構造X509EncodedKeySpec對象
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
//指定加密算法
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//取公鑰匙對象
PublicKey publicKey2 = keyFactory.generatePublic(x509EncodedKeySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicKey2);
signature.update(data);
//驗證簽名是否正常
return signature.verify(Base64.decode(sign, Base64.DEFAULT));
}
示例6: encryptByPublicKey
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
/**
* 用公鑰加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data, String key)
throws Exception {
byte[] keyBytes = BASE64.decode(key); // 對公鑰解密
// 取得公鑰
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.getType());
Key publicKey = keyFactory.generatePublic(x509KeySpec);
// 對數據加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
示例7: onEnable
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
@Override
public void onEnable() {
instance = this;
try {
mojangPublicKey = KeyFactory.getInstance("EC").generatePublic(
new X509EncodedKeySpec(Base64
.getDecoder()
.decode("MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE8ELkixyLcwlZryUQcu1TvPOmI2B7vX83ndnWRUaXm74wFfa5f/lwQNTfrLVHa2PmenpGI6JhIMUJaWZrjmMj90NoKNFSNBuKdm8rYiXsfaz3K36x/1U26HpG0ZxK/V1V")
)
);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
}
networkManager = new NetworkManager();
}
示例8: getPublicKey
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
public static PublicKey getPublicKey(String publicKeyText) {
if (publicKeyText == null || publicKeyText.length() == 0) {
publicKeyText = DecryptUtil.DEFAULT_PUBLIC_KEY_STRING;
}
try {
byte[] publicKeyBytes = Base64.base64ToByteArray(publicKeyText);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(
publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(x509KeySpec);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to get public key", e);
}
}
示例9: verify
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
/**
* 校驗
*
* @param data 待校驗數據
* @param publicKey 公鑰
* @param sign 數字簽名
* @return boolean 校驗成功返回true 失敗返回false
* @throws Exception
*/
public static boolean verify(byte[] data, byte[] publicKey, byte[] sign) throws Exception {
// 轉換公鑰材料
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
// 實例化密鑰工廠
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
// 生成公鑰
PublicKey pubKey = keyFactory.generatePublic(keySpec);
// 實例化Signature
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
// 初始化Signature
signature.initVerify(pubKey);
// 更新
signature.update(data);
// 驗證
return signature.verify(sign);
}
示例10: verify
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
/**
* 校驗
*
* @param data
* 待校驗數據
* @param publicKey
* 公鑰
* @param sign
* 數字簽名
* @return boolean 校驗成功返回true 失敗返回false
* @throws Exception
*/
public static boolean verify(byte[] data, byte[] publicKey, byte[] sign) throws Exception {
// 轉換公鑰材料
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
// 實例化密鑰工廠
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
// 生成公鑰
PublicKey pubKey = keyFactory.generatePublic(keySpec);
// 實例化Signature
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
// 初始化Signature
signature.initVerify(pubKey);
// 更新
signature.update(data);
// 驗證
return signature.verify(sign);
}
示例11: verify
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
/**
* 校驗
*
* @param data 待校驗數據
* @param publicKey 公鑰
* @param sign 數字簽名
* @return boolean 校驗成功返回true 失敗返回false
* @throws Exception
*/
public static boolean verify(byte[] data, byte[] publicKey, byte[] sign) throws Exception {
// 轉換公鑰材料
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
// 實例化密鑰工廠
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
// 生成公鑰
PublicKey pubKey = keyFactory.generatePublic(keySpec);
// 實例化Signature
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
// 初始化Signature
signature.initVerify(pubKey);
// 更新
signature.update(data);
// 驗證
return signature.verify(sign);
}
示例12: engineGeneratePublic
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
protected PublicKey engineGeneratePublic(
KeySpec keySpec)
throws InvalidKeySpecException
{
if (keySpec instanceof X509EncodedKeySpec)
{
try
{
return generatePublic(SubjectPublicKeyInfo.getInstance(((X509EncodedKeySpec)keySpec).getEncoded()));
}
catch (Exception e)
{
throw new InvalidKeySpecException("encoded key spec not recognised");
}
}
else
{
throw new InvalidKeySpecException("key spec not recognised");
}
}
示例13: getKeyPair
import java.security.spec.X509EncodedKeySpec; //導入依賴的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: decryptByPublicKey
import java.security.spec.X509EncodedKeySpec; //導入依賴的package包/類
/**
* 用公鑰解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data, String key)
throws Exception {
byte[] keyBytes = BASE64.decode(key); // 對密鑰解密
// 取得公鑰
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.getType());
Key publicKey = keyFactory.generatePublic(x509KeySpec);
// 對數據解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
示例15: createKeyPairFromEncodedKeys
import java.security.spec.X509EncodedKeySpec; //導入依賴的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;
}