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


Java X509EncodedKeySpec类代码示例

本文整理汇总了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());
    }
}
 
开发者ID:stevespringett,项目名称:Alpine,代码行数:29,代码来源:KeyManager.java

示例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;
}
 
开发者ID:F8LEFT,项目名称:FApkSigner,代码行数:27,代码来源:V2SchemeSigner.java

示例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);
    }
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:16,代码来源:CRMFHelper.java

示例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;
}
 
开发者ID:noseparte,项目名称:Spring-Boot-Server,代码行数:33,代码来源:AlipayRSA.java

示例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));

}
 
开发者ID:abook23,项目名称:godlibrary,代码行数:27,代码来源:RSAUtlis.java

示例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);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:24,代码来源:Codec.java

示例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();
}
 
开发者ID:KernelFreeze,项目名称:BedrockProxy,代码行数:18,代码来源:BedrockProxy.java

示例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);
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:17,代码来源:DecryptUtil.java

示例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);
}
 
开发者ID:liuxx001,项目名称:bird-java,代码行数:26,代码来源:RSACoder.java

示例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);
}
 
开发者ID:mumucommon,项目名称:mumu-core,代码行数:29,代码来源:RSACoder.java

示例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);
}
 
开发者ID:youngMen1,项目名称:JAVA-,代码行数:26,代码来源:RSACoder.java

示例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");
    }
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:21,代码来源:BaseKeyFactorySpi.java

示例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);
    }
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:23,代码来源:JcaPEMKeyConverter.java

示例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);
}
 
开发者ID:lzmlsfe,项目名称:19porn,代码行数:24,代码来源:Codec.java

示例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;
}
 
开发者ID:StanIsAdmin,项目名称:CrashCoin,代码行数:27,代码来源:Cryptography.java


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