Search in sources :

Example 16 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project zaproxy by zaproxy.

the class SslCertificateUtils method generatePrivateKeyFromDER.

private static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    return (RSAPrivateKey) factory.generatePrivate(spec);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 17 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project XobotOS by xamarin.

the class MiscPEMGenerator method createPemObject.

private PemObject createPemObject(Object o) throws IOException {
    String type;
    byte[] encoding;
    if (o instanceof PemObject) {
        return (PemObject) o;
    }
    if (o instanceof PemObjectGenerator) {
        return ((PemObjectGenerator) o).generate();
    }
    if (o instanceof X509Certificate) {
        type = "CERTIFICATE";
        try {
            encoding = ((X509Certificate) o).getEncoded();
        } catch (CertificateEncodingException e) {
            throw new PemGenerationException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof X509CRL) {
        type = "X509 CRL";
        try {
            encoding = ((X509CRL) o).getEncoded();
        } catch (CRLException e) {
            throw new PemGenerationException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof KeyPair) {
        return createPemObject(((KeyPair) o).getPrivate());
    } else if (o instanceof PrivateKey) {
        PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) ASN1Object.fromByteArray(((Key) o).getEncoded()));
        if (o instanceof RSAPrivateKey) {
            type = "RSA PRIVATE KEY";
            encoding = info.getPrivateKey().getEncoded();
        } else if (o instanceof DSAPrivateKey) {
            type = "DSA PRIVATE KEY";
            DSAParameter p = DSAParameter.getInstance(info.getAlgorithmId().getParameters());
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(new DERInteger(0));
            v.add(new DERInteger(p.getP()));
            v.add(new DERInteger(p.getQ()));
            v.add(new DERInteger(p.getG()));
            BigInteger x = ((DSAPrivateKey) o).getX();
            BigInteger y = p.getG().modPow(x, p.getP());
            v.add(new DERInteger(y));
            v.add(new DERInteger(x));
            encoding = new DERSequence(v).getEncoded();
        } else if (((PrivateKey) o).getAlgorithm().equals("ECDSA")) {
            type = "EC PRIVATE KEY";
            encoding = info.getPrivateKey().getEncoded();
        } else {
            throw new IOException("Cannot identify private key");
        }
    } else if (o instanceof PublicKey) {
        type = "PUBLIC KEY";
        encoding = ((PublicKey) o).getEncoded();
    } else if (o instanceof X509AttributeCertificate) {
        type = "ATTRIBUTE CERTIFICATE";
        encoding = ((X509V2AttributeCertificate) o).getEncoded();
    } else if (o instanceof PKCS10CertificationRequest) {
        type = "CERTIFICATE REQUEST";
        encoding = ((PKCS10CertificationRequest) o).getEncoded();
    } else if (o instanceof ContentInfo) {
        type = "PKCS7";
        encoding = ((ContentInfo) o).getEncoded();
    } else {
        throw new PemGenerationException("unknown object passed - can't encode.");
    }
    return new PemObject(type, encoding);
}
Also used : X509CRL(java.security.cert.X509CRL) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) X509AttributeCertificate(org.bouncycastle.x509.X509AttributeCertificate) DERInteger(org.bouncycastle.asn1.DERInteger) PemObjectGenerator(org.bouncycastle.util.io.pem.PemObjectGenerator) DERSequence(org.bouncycastle.asn1.DERSequence) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DSAParameter(org.bouncycastle.asn1.x509.DSAParameter) CRLException(java.security.cert.CRLException) PKCS10CertificationRequest(org.bouncycastle.jce.PKCS10CertificationRequest) KeyPair(java.security.KeyPair) PemGenerationException(org.bouncycastle.util.io.pem.PemGenerationException) PublicKey(java.security.PublicKey) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509V2AttributeCertificate(org.bouncycastle.x509.X509V2AttributeCertificate) X509Certificate(java.security.cert.X509Certificate) PemObject(org.bouncycastle.util.io.pem.PemObject) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey)

Example 18 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project jdk8u_jdk by JetBrains.

the class SpecTest method specTest.

/**
     *
     * @param kpair test key pair
     * @param pubExponent expected public exponent.
     * @return true if test passed. false if test failed.
     */
private static boolean specTest(KeyPair kpair, BigInteger pubExponent) {
    boolean passed = true;
    RSAPrivateKey priv = (RSAPrivateKey) kpair.getPrivate();
    RSAPublicKey pub = (RSAPublicKey) kpair.getPublic();
    // test the getModulus method
    if ((priv instanceof RSAKey) && (pub instanceof RSAKey)) {
        if (!priv.getModulus().equals(pub.getModulus())) {
            System.err.println("priv.getModulus() = " + priv.getModulus());
            System.err.println("pub.getModulus() = " + pub.getModulus());
            passed = false;
        }
        if (!pubExponent.equals(pub.getPublicExponent())) {
            System.err.println("pubExponent = " + pubExponent);
            System.err.println("pub.getPublicExponent() = " + pub.getPublicExponent());
            passed = false;
        }
    }
    return passed;
}
Also used : RSAKey(java.security.interfaces.RSAKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 19 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project yyl_example by Relucent.

the class Rsa method main.

public static void main(String[] args) throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
    // 密钥位数
    keyPairGen.initialize(1024);
    // 密钥对
    KeyPair keyPair = keyPairGen.generateKeyPair();
    // 公钥
    PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    // 私钥
    PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    String publicKeyString = getKeyString(publicKey);
    System.out.println("public:\n" + publicKeyString);
    String privateKeyString = getKeyString(privateKey);
    System.out.println("private:\n" + privateKeyString);
    // 加解密类
    // Cipher.getInstance("RSA/ECB/PKCS1Padding");
    Cipher cipher = Cipher.getInstance("RSA");
    // 明文
    byte[] plainText = "我们都很好!邮件:@sina.com".getBytes();
    // 加密
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] enBytes = cipher.doFinal(plainText);
    // 通过密钥字符串得到密钥
    publicKey = getPublicKey(publicKeyString);
    privateKey = getPrivateKey(privateKeyString);
    // 解密
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] deBytes = cipher.doFinal(enBytes);
    publicKeyString = getKeyString(publicKey);
    System.out.println("public:\n" + publicKeyString);
    privateKeyString = getKeyString(privateKey);
    System.out.println("private:\n" + privateKeyString);
    String s = new String(deBytes);
    System.out.println(s);
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) KeyPairGenerator(java.security.KeyPairGenerator) Cipher(javax.crypto.Cipher) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 20 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project Gradle-demo by Arisono.

the class RSAUtils method generateKeyBytes.

/**
	 * 生成密钥对。注意这里是生成密钥对KeyPair,再由密钥对获取公私钥
	 * 生成RSA的公钥和私钥
	 * @return
	 */
public static Map<String, byte[]> generateKeyBytes() {
    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGenerator.initialize(KEY_SIZE);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        Map<String, byte[]> keyMap = new HashMap<String, byte[]>();
        keyMap.put(PUBLIC_KEY, publicKey.getEncoded());
        keyMap.put(PRIVATE_KEY, privateKey.getEncoded());
        return keyMap;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) HashMap(java.util.HashMap) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Aggregations

RSAPrivateKey (java.security.interfaces.RSAPrivateKey)46 RSAPublicKey (java.security.interfaces.RSAPublicKey)24 KeyFactory (java.security.KeyFactory)13 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)13 InvalidKeyException (java.security.InvalidKeyException)12 PrivateKey (java.security.PrivateKey)12 KeyPair (java.security.KeyPair)11 PublicKey (java.security.PublicKey)11 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)11 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)9 KeyPairGenerator (java.security.KeyPairGenerator)8 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)7 BigInteger (java.math.BigInteger)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 IOException (java.io.IOException)5 Key (java.security.Key)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 Signature (java.security.Signature)4