Examples of RSAPrivateKeySpec


Examples of java.security.spec.RSAPrivateKeySpec

    _signature.initVerify(pubKey);
  }

  @Override
  public void setPrvKey(byte[] d, byte[] n) throws Exception {
    RSAPrivateKeySpec rsaPrivKeySpec = new RSAPrivateKeySpec(new BigInteger(n), new BigInteger(d));
    PrivateKey prvKey = _keyFactory.generatePrivate(rsaPrivKeySpec);
    _signature.initSign(prvKey);
  }
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

      cipher.init(Cipher.DECRYPT_MODE, publicKey);
    } catch (InvalidKeyException e) {
            // 因为 IBM JDK 不支持私钥加密, 公钥解密, 所以要反转公私钥
            // 也就是说对于解密, 可以通过公钥的参数伪造一个私钥对象欺骗 IBM JDK
            RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
            RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
            Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
            cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one.
            cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
    }
   
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

       }
       else if (spec.isAssignableFrom(RSAPrivateKeySpec.class) && key instanceof RSAPrivateKey)
       {
            RSAPrivateKey    k = (RSAPrivateKey)key;

            return new RSAPrivateKeySpec(k.getModulus(), k.getPrivateExponent());
       }
       else if (spec.isAssignableFrom(RSAPrivateCrtKeySpec.class) && key instanceof RSAPrivateCrtKey)
       {
            RSAPrivateCrtKey    k = (RSAPrivateCrtKey)key;
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

            kspec = new DSAPrivateKeySpec
                (new BigInteger(DSA_X), new BigInteger(DSA_P),
                 new BigInteger(DSA_Q), new BigInteger(DSA_G));
        } else if (algo.equalsIgnoreCase("RSA")) {
            if (keysize == 512) {
                kspec = new RSAPrivateKeySpec
                    (new BigInteger(RSA_MOD), new BigInteger(RSA_PRIV));
            } else {
                kspec = new RSAPrivateKeySpec(new BigInteger(RSA_1024_MOD),
                                              new BigInteger(RSA_1024_PRIV));
            }
        } else throw new RuntimeException("Unsupported key algorithm " + algo);
        return kf.generatePrivate(kspec);
    }
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

        BigInteger d = new BigInteger(1, delement)

        KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM)
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM)

        RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d)
        PrivateKey privKey = factory.generatePrivate(privSpec)
        cipher.init(Cipher.DECRYPT_MODE, privKey);
        return cipher.doFinal(data);
    }
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

       }
       else if (spec.isAssignableFrom(RSAPrivateKeySpec.class) && key instanceof RSAPrivateKey)
       {
            RSAPrivateKey    k = (RSAPrivateKey)key;

            return new RSAPrivateKeySpec(k.getModulus(), k.getPrivateExponent());
       }
       else if (spec.isAssignableFrom(RSAPrivateCrtKeySpec.class) && key instanceof RSAPrivateCrtKey)
       {
            RSAPrivateCrtKey    k = (RSAPrivateCrtKey)key;
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

        keyPairGenerator.initialize(KEY_SIZE);
        KeyPair kp = keyPairGenerator.genKeyPair();

        try {
            RSAPublicKeySpec pub = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
            RSAPrivateKeySpec priv = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
            String uuid = UUID.randomUUID().toString();
            signer.initSign(kp.getPrivate(), new SecureRandom());
            signer.update(uuid.getBytes(Charsets.UTF_8));
            signer.update(pub.getModulus().toByteArray());
            signer.update(pub.getPublicExponent().toByteArray());
            byte[] rawSig = signer.sign();
            BigInteger signature = new BigInteger(rawSig);

            PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature);
            PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent());
            return new CertificatePair(publicCert, privateCert);
        } catch (InvalidKeySpecException e) {
            throw new RuntimeException("Unexpected exception generating certificate", e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException("Unexpected exception generating certificate", e);
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

     */
    public CertificatePair generate(PrivateIdentityCertificate signingCertificate) {
        keyPairGenerator.initialize(KEY_SIZE);
        KeyPair kp = keyPairGenerator.genKeyPair();

        RSAPrivateKeySpec signingRSAKey = new RSAPrivateKeySpec(signingCertificate.getModulus(), signingCertificate.getExponent());

        try {
            PrivateKey signingKey = keyFactory.generatePrivate(signingRSAKey);

            RSAPublicKeySpec pub = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
            RSAPrivateKeySpec priv = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);

            String uuid = UUID.randomUUID().toString();
            signer.initSign(signingKey, new SecureRandom());
            signer.update(uuid.getBytes(Charsets.UTF_8));
            signer.update(pub.getModulus().toByteArray());
            signer.update(pub.getPublicExponent().toByteArray());
            byte[] rawSig = signer.sign();
            BigInteger signature = new BigInteger(rawSig);

            PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature);
            PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent());
            return new CertificatePair(publicCert, privateCert);
        } catch (InvalidKeySpecException e) {
            throw new RuntimeException("Unexpected exception generating certificate", e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException("Unexpected exception generating certificate", e);
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

     *
     * @param dataToSign
     * @return The signature
     */
    public byte[] sign(byte[] dataToSign) {
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent);

        Signature signer = null;
        try {
            signer = Signature.getInstance(IdentityConstants.SIGNATURE_ALGORITHM);
            KeyFactory keyFactory = KeyFactory.getInstance(IdentityConstants.CERTIFICATE_ALGORITHM);
View Full Code Here

Examples of java.security.spec.RSAPrivateKeySpec

       }
       else if (spec.isAssignableFrom(RSAPrivateKeySpec.class) && key instanceof RSAPrivateKey)
       {
            RSAPrivateKey    k = (RSAPrivateKey)key;

            return new RSAPrivateKeySpec(k.getModulus(), k.getPrivateExponent());
       }
       else if (spec.isAssignableFrom(RSAPrivateCrtKeySpec.class) && key instanceof RSAPrivateCrtKey)
       {
            RSAPrivateCrtKey    k = (RSAPrivateCrtKey)key;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.