Search in sources :

Example 56 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project spring-cloud-digital-sign by SpringForAll.

the class RSA method main.

public static void main(String[] args) {
    try {
        /**
         * 密钥初始化
         */
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
        /**
         * 执行签名
         */
        // 使用私钥加密
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initSign(privateKey);
        signature.update(src.getBytes());
        byte[] result = signature.sign();
        System.out.println("jdk rsa sign : " + Hex.encodeHexString(result));
        /**
         * 验证签名
         */
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
        keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        signature = Signature.getInstance("MD5withRSA");
        signature.initVerify(publicKey);
        signature.update(src.getBytes());
        boolean bool = signature.verify(result);
        System.out.println("jdk rsa verify : " + bool);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    }
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) RSAPublicKey(java.security.interfaces.RSAPublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 57 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project blockchain-java-api-client by astarlabs.

the class KeyPairUtil method generateKeys.

public static KeyPairUtil.Keys generateKeys() throws IOException, GeneralSecurityException {
    Security.addProvider(new BouncyCastleProvider());
    KeyPair keyPair = generateRSAKeyPair();
    RSAPrivateKey priv = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey pub = (RSAPublicKey) keyPair.getPublic();
    String privateString = getStringEncodedKey("RSA PRIVATE KEY", priv.getEncoded());
    String publicString = getStringEncodedKey("RSA PUBLIC KEY", pub.getEncoded());
    KeyPairUtil.Keys myKeys = new KeyPairUtil().new Keys();
    myKeys.privateKey = privateString;
    myKeys.publicKey = publicString;
    return myKeys;
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 58 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project santuario-java by apache.

the class KeyResolverTest method testResolvePrivateKey.

/**
 * Encrypt some data, embedded the data encryption key
 * in the message using the key transport algorithm rsa-1_5.
 * Decrypt the data by resolving the Key Encryption Key.
 * This test verifies if a KeyResolver can return a PrivateKey.
 */
@org.junit.Test
public void testResolvePrivateKey() throws Exception {
    // See if AES-128 is available...
    String algorithmId = JCEMapper.translateURItoJCEID(org.apache.xml.security.utils.EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128);
    boolean haveAES = false;
    if (algorithmId != null) {
        try {
            if (Cipher.getInstance(algorithmId) != null) {
                haveAES = true;
            }
        } catch (NoSuchAlgorithmException nsae) {
        // 
        } catch (NoSuchPaddingException nspe) {
        // 
        }
    }
    if (!haveAES) {
        return;
    }
    // Create a sample XML document
    Document document = XMLUtils.createDocumentBuilder(false).newDocument();
    Element rootElement = document.createElement("root");
    document.appendChild(rootElement);
    Element elem = document.createElement("elem");
    Text text = document.createTextNode("text");
    elem.appendChild(text);
    rootElement.appendChild(elem);
    // Create a data encryption key
    byte[] keyBytes = { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 };
    SecretKeySpec dataEncryptKey = new SecretKeySpec(keyBytes, "AES");
    // Create public and private keys
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("8710a2bcb2f3fdac177f0ae0461c2dd0ebf72e0d88a5400583a7d8bdabd6" + "ae009d30cfdf6acb5b6a64cdc730bc630a39d946d08babffe62ea20a87e37c93b3b0e8a8e576045b" + "bddfbde83ca9bfa180fe6a5f5eee60661936d728314e809201ef52cd71d9fa3c8ce83f9d30ab5e08" + "1539219e7e45dd6a60be65ac95d2049b8f21", 16), new BigInteger("10001", 16));
    RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(new BigInteger("8710a2bcb2f3fdac177f0ae0461c2dd0ebf72e0d88a5400583a7d8bdabd" + "6ae009d30cfdf6acb5b6a64cdc730bc630a39d946d08babffe62ea20a87e37c93b3b0e8a8e576045" + "bbddfbde83ca9bfa180fe6a5f5eee60661936d728314e809201ef52cd71d9fa3c8ce83f9d30ab5e0" + "81539219e7e45dd6a60be65ac95d2049b8f21", 16), new BigInteger("20c39e569c2aa80cc91e5e6b0d56e49e5bbf78827bf56a546c1d996c597" + "5187cb9a50fa828e5efe51d52f5d112c20bc700b836facadca6e0051afcdfe866841e37d207c0295" + "36ff8674b301e2198b2c56abb0a0313f8ff84c1fcd6fa541aa6e5d9c018fab4784d2940def5dc709" + "ddc714d73b6c23b5d178eaa5933577b8e8ae9", 16));
    RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
    RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);
    // Encrypt the data encryption key with the key encryption key
    XMLCipher keyCipher = XMLCipher.getInstance(XMLCipher.RSA_v1dot5);
    keyCipher.init(XMLCipher.WRAP_MODE, pubKey);
    EncryptedKey encryptedKey = keyCipher.encryptKey(document, dataEncryptKey);
    String keyName = "testResolvePrivateKey";
    KeyInfo kekInfo = new KeyInfo(document);
    kekInfo.addKeyName(keyName);
    encryptedKey.setKeyInfo(kekInfo);
    // Encrypt the data
    XMLCipher xmlCipher = XMLCipher.getInstance(XMLCipher.AES_128);
    xmlCipher.init(XMLCipher.ENCRYPT_MODE, dataEncryptKey);
    EncryptedData encryptedData = xmlCipher.getEncryptedData();
    KeyInfo keyInfo = new KeyInfo(document);
    keyInfo.add(encryptedKey);
    encryptedData.setKeyInfo(keyInfo);
    xmlCipher.doFinal(document, rootElement, true);
    Element encryptedDataElement = (Element) rootElement.getFirstChild();
    assertEquals("EncryptedData", encryptedDataElement.getLocalName());
    // Decrypt the data by resolving the private key used as the KEK
    // First test with an internal KeyResolver
    MyPrivateKeyResolver.pk = privKey;
    MyPrivateKeyResolver.pkName = keyName;
    decryptDocument(document, new MyPrivateKeyResolver());
    // Now test with a static KeyResolver
    KeyResolver.registerAtStart(MyPrivateKeyResolver.class.getName(), false);
    KeyResolverSpi resolver = KeyResolver.iterator().next();
    assertEquals(MyPrivateKeyResolver.class.getName(), resolver.getClass().getName());
    decryptDocument(document, null);
}
Also used : EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Element(org.w3c.dom.Element) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) XMLCipher(org.apache.xml.security.encryption.XMLCipher) Text(org.w3c.dom.Text) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Document(org.w3c.dom.Document) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) RSAPublicKey(java.security.interfaces.RSAPublicKey) KeyInfo(org.apache.xml.security.keys.KeyInfo) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeyResolverSpi(org.apache.xml.security.keys.keyresolver.KeyResolverSpi) BigInteger(java.math.BigInteger) EncryptedData(org.apache.xml.security.encryption.EncryptedData) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 59 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project i2p.i2p by i2p.

the class SigUtil method toJavaRSAKey.

/**
 *  @deprecated unused
 */
public static RSAPublicKey toJavaRSAKey(SigningPublicKey pk) throws GeneralSecurityException {
    SigType type = pk.getType();
    KeyFactory kf = KeyFactory.getInstance("RSA");
    BigInteger n = new NativeBigInteger(1, pk.getData());
    BigInteger e = ((RSAKeyGenParameterSpec) type.getParams()).getPublicExponent();
    // modulus exponent
    KeySpec ks = new RSAPublicKeySpec(n, e);
    return (RSAPublicKey) kf.generatePublic(ks);
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 60 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project cosmic by MissionCriticalCloud.

the class RSAHelper method encryptWithSSHPublicKey.

public static String encryptWithSSHPublicKey(final String sshPublicKey, final String content) {
    String returnString = null;
    try {
        final RSAPublicKey publicKey = readKey(sshPublicKey);
        final Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", BouncyCastleProvider.PROVIDER_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey, new SecureRandom());
        final byte[] encrypted = cipher.doFinal(content.getBytes());
        returnString = Base64.encodeBase64String(encrypted);
    } catch (final Exception e) {
        s_logger.info("[ignored]" + "error during public key encryption: " + e.getLocalizedMessage());
    }
    return returnString;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher) IOException(java.io.IOException)

Aggregations

RSAPublicKey (java.security.interfaces.RSAPublicKey)240 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)65 PublicKey (java.security.PublicKey)50 KeyPair (java.security.KeyPair)48 BigInteger (java.math.BigInteger)44 IOException (java.io.IOException)39 KeyPairGenerator (java.security.KeyPairGenerator)39 KeyFactory (java.security.KeyFactory)37 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)31 ECPublicKey (java.security.interfaces.ECPublicKey)30 X509Certificate (java.security.cert.X509Certificate)29 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)28 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)27 Test (org.junit.Test)27 PrivateKey (java.security.PrivateKey)26 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)26 CertificateException (java.security.cert.CertificateException)24 DSAPublicKey (java.security.interfaces.DSAPublicKey)24 InvalidKeyException (java.security.InvalidKeyException)22 ByteArrayInputStream (java.io.ByteArrayInputStream)21