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


Java KeyFactory.getKeySpec方法代码示例

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


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

示例1: main

import java.security.KeyFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws GeneralSecurityException, UnsupportedEncodingException {

        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        KeyPair kp = kpg.genKeyPair();

        KeyFactory fact = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(),
                RSAPublicKeySpec.class);
        RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(),
                RSAPrivateKeySpec.class);

        publicKey = fact.generatePublic(pub);
        privateKey = fact.generatePrivate(priv);

        String foo = rsaEncrypt("foo");

        byte[] decode = Base64.getDecoder().decode("foo");
        System.out.println(Base64.getEncoder().encodeToString(decode));

        System.out.println(rsaDecrypt(foo));

    }
 
开发者ID:daishicheng,项目名称:outcomes,代码行数:24,代码来源:Main.java

示例2: addDEREncodedPublicKey

import java.security.KeyFactory; //导入方法依赖的package包/类
/**
 * Converts a Java public key into a {@link DEREncodedKeyValue} element and adds it to
 * a {@link KeyInfo}.
 * 
 * @param keyInfo the {@link KeyInfo} element to which to add the key
 * @param pk the native Java {@link PublicKey} to add
 * @throws NoSuchAlgorithmException if the key type is unsupported
 * @throws InvalidKeySpecException if the key type does not support X.509 SPKI encoding
 */
public static void addDEREncodedPublicKey(KeyInfo keyInfo, PublicKey pk)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    DEREncodedKeyValue keyValue = (DEREncodedKeyValue) Configuration.getBuilderFactory()
        .getBuilder(DEREncodedKeyValue.DEFAULT_ELEMENT_NAME)
        .buildObject(DEREncodedKeyValue.DEFAULT_ELEMENT_NAME);
    
    KeyFactory keyFactory = KeyFactory.getInstance(pk.getAlgorithm());
    X509EncodedKeySpec keySpec = keyFactory.getKeySpec(pk, X509EncodedKeySpec.class);
    keyValue.setValue(Base64.encodeBytes(keySpec.getEncoded()));
    keyInfo.getXMLObjects().add(keyValue);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:21,代码来源:KeyInfoHelper.java

示例3: testPublic

import java.security.KeyFactory; //导入方法依赖的package包/类
private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
    System.out.println("Testing public key...");
    PublicKey key2 = (PublicKey)kf.translateKey(key);
    KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);
    PublicKey key3 = kf.generatePublic(keySpec);
    KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
    PublicKey key4 = kf.generatePublic(x509Spec);
    KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
    PublicKey key5 = kf.generatePublic(x509Spec2);
    testKey(key, key);
    testKey(key, key2);
    testKey(key, key3);
    testKey(key, key4);
    testKey(key, key5);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:TestKeyFactory.java

示例4: testPrivate

import java.security.KeyFactory; //导入方法依赖的package包/类
private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
    System.out.println("Testing private key...");
    PrivateKey key2 = (PrivateKey)kf.translateKey(key);
    KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);
    PrivateKey key3 = kf.generatePrivate(keySpec);
    KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
    PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
    KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
    PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
    testKey(key, key);
    testKey(key, key2);
    testKey(key, key3);
    testKey(key, key4);
    testKey(key, key5);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:TestKeyFactory.java

示例5: main

import java.security.KeyFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    int iKeyPairSize = Integer.parseInt(args[0]);
    int maxLoopCnt = Integer.parseInt(args[1]);

    int failCount = 0;
    KeyPairGenerator keyPairGen
            = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    keyPairGen.initialize(iKeyPairSize);
    // Generate RSA keypair
    KeyPair keyPair = keyPairGen.generateKeyPair();

    // Get priavte and public keys
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();
    try {
        if (!sizeTest(keyPair)) {
            failCount++;
        }
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        failCount++;
    }

    for (int iCnt = 0; iCnt < maxLoopCnt; iCnt++) {

        // Get keysize (modulus) of keys
        KeyFactory keyFact = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);

        // Comparing binary length.
        RSAPrivateKeySpec privateKeySpec
                = (RSAPrivateKeySpec) keyFact.getKeySpec(privateKey,
                        RSAPrivateKeySpec.class);
        int iPrivateKeySize = privateKeySpec.getModulus().bitLength();

        RSAPublicKeySpec publicKeySpec
                = (RSAPublicKeySpec) keyFact.getKeySpec(publicKey,
                        RSAPublicKeySpec.class);
        int iPublicKeySize = publicKeySpec.getModulus().bitLength();

        if ((iKeyPairSize != iPublicKeySize) || (iKeyPairSize != iPrivateKeySize)) {
            System.err.println("iKeyPairSize : " + iKeyPairSize);
            System.err.println("Generated a " + iPrivateKeySize
                    + " bit RSA private key");
            System.err.println("Generated a " + iPublicKeySize
                    + " bit RSA public key");
            failCount++;
        }
    }

    if (failCount > 0) {
        throw new RuntimeException("There are " + failCount + " tests failed.");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:54,代码来源:KeySizeTest.java


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