Search in sources :

Example 46 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project j2objc by google.

the class RSAPublicKeySpecTest method testRSAPublicKeySpec02.

/**
     * Test #2 for <code>RSAPublicKeySpec</code> constructor
     * Assertion: Constructs <code>RSAPublicKeySpec</code>
     * object using valid parameters
     */
public final void testRSAPublicKeySpec02() {
    KeySpec ks = new RSAPublicKeySpec(null, null);
    assertTrue(ks instanceof RSAPublicKeySpec);
}
Also used : KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 47 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project j2objc by google.

the class RSAPublicKeyTest method test_getPublicExponent.

/**
     * java.security.interfaces.RSAPublicKey
     * #getPublicExponent()
     */
public void test_getPublicExponent() throws Exception {
    KeyFactory gen = KeyFactory.getInstance("RSA");
    final BigInteger n = BigInteger.valueOf(3233);
    final BigInteger e = BigInteger.valueOf(17);
    RSAPublicKey key = (RSAPublicKey) gen.generatePublic(new RSAPublicKeySpec(n, e));
    assertEquals("invalid public exponent", e, key.getPublicExponent());
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 48 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project druid by alibaba.

the class ConfigTools method encrypt.

public static String encrypt(byte[] keyBytes, String plainText) throws Exception {
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory factory = KeyFactory.getInstance("RSA", "SunRsaSign");
    PrivateKey privateKey = factory.generatePrivate(spec);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    } catch (InvalidKeyException e) {
        //For IBM JDK, 原因请看解密方法中的说明
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
        Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
    }
    byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
    String encryptedString = Base64.byteArrayToBase64(encryptedBytes);
    return encryptedString;
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Cipher(javax.crypto.Cipher) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) PrivateKey(java.security.PrivateKey)

Example 49 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project remusic by aa112901.

the class RsaCal method main1.

public static void main1(String[] args) throws Exception {
    // TODO Auto-generated method stub
    HashMap<String, Object> map = RSAUtils.getKeys();
    //生成公钥和私钥
    //     RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
    //     RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
    String big1 = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7";
    String big2 = "010001";
    //  System.out.println(b1.toString());
    // System.out.println(b2.toString());
    RSAPublicKeySpec rsaPubKS = new RSAPublicKeySpec(new BigInteger(big1, 16), new BigInteger(big2, 16));
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKey publicKey = (RSAPublicKey) kf.generatePublic(rsaPubKS);
    //模
    String modulus = publicKey.getModulus().toString();
    //公钥指数
    String public_exponent = publicKey.getPublicExponent().toString();
    System.err.println(modulus);
    System.err.println(public_exponent);
    //私钥指数
    //  String private_exponent = privateKey.getPrivateExponent().toString();
    //明文
    String ming = "628211bf359401b7";
    //使用模和指数生成公钥和私钥
    RSAPublicKey pubKey = RSAUtils.getPublicKey(new BigInteger(big1, 16) + "", new BigInteger(big2, 16) + "");
    // RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent);
    //加密后的密文
    String mi = RSAUtils.encryptByPublicKey(ming, pubKey);
    System.err.println(mi);
//解密后的明文
//  ming = RSAUtils.decryptByPrivateKey(mi, priKey);
//  System.err.println(ming);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 50 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project Mycat-Server by MyCATApache.

the class DecryptUtil method encrypt.

public static String encrypt(byte[] keyBytes, String plainText) throws Exception {
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = factory.generatePrivate(spec);
    Cipher cipher = Cipher.getInstance("RSA");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    } catch (InvalidKeyException e) {
        //For IBM JDK, 原因请看解密方法中的说明
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
        Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
    }
    byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
    String encryptedString = Base64.byteArrayToBase64(encryptedBytes);
    return encryptedString;
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Cipher(javax.crypto.Cipher) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) PrivateKey(java.security.PrivateKey)

Aggregations

RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)85 KeyFactory (java.security.KeyFactory)61 PublicKey (java.security.PublicKey)48 RSAPublicKey (java.security.interfaces.RSAPublicKey)30 BigInteger (java.math.BigInteger)24 Signature (java.security.Signature)22 PrivateKey (java.security.PrivateKey)20 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)17 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)14 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 Cipher (javax.crypto.Cipher)12 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)10 SecretKeyFactory (javax.crypto.SecretKeyFactory)10 IOException (java.io.IOException)9 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)9 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)8 KeySpec (java.security.spec.KeySpec)8 KeyPair (java.security.KeyPair)7 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)6 InvalidKeyException (java.security.InvalidKeyException)5