Search in sources :

Example 46 with RSAPublicKey

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

the class CaClientExample method generateRsaKeypair.

protected static MyKeypair generateRsaKeypair() throws Exception {
    KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
    kpGen.initialize(2048);
    KeyPair kp = kpGen.generateKeyPair();
    RSAPublicKey pubKey = (RSAPublicKey) kp.getPublic();
    SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new org.bouncycastle.asn1.pkcs.RSAPublicKey(pubKey.getModulus(), pubKey.getPublicExponent()));
    return new MyKeypair(kp.getPrivate(), subjectPublicKeyInfo);
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) KeyPairGenerator(java.security.KeyPairGenerator) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 47 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project jeesuite-libs by vakinge.

the class RSA method generateKeyPair.

/**
 * 随机生成密钥对
 */
public static void generateKeyPair(String filePath) {
    // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
    KeyPairGenerator keyPairGen = null;
    try {
        keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // 初始化密钥对生成器,密钥大小为96-1024位
    keyPairGen.initialize(KEY_SIZE, new SecureRandom());
    // 生成一个密钥对,保存在keyPair中
    KeyPair keyPair = keyPairGen.generateKeyPair();
    // 得到私钥
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    // 得到公钥
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    try {
        // 得到公钥字符串
        String publicKeyString = Base64.encodeToString(publicKey.getEncoded(), true);
        // 得到私钥字符串
        String privateKeyString = Base64.encodeToString(privateKey.getEncoded(), true);
        // 将密钥对写入到文件
        FileWriter pubfw = new FileWriter(filePath + "/public.key");
        FileWriter prifw = new FileWriter(filePath + "/private.key");
        BufferedWriter pubbw = new BufferedWriter(pubfw);
        BufferedWriter pribw = new BufferedWriter(prifw);
        pubbw.write(publicKeyString);
        pribw.write(privateKeyString);
        pubbw.flush();
        pubbw.close();
        pubfw.close();
        pribw.flush();
        pribw.close();
        prifw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) FileWriter(java.io.FileWriter) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) BufferedWriter(java.io.BufferedWriter)

Example 48 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project Mycat_plus by coderczp.

the class DecryptUtil method decrypt.

public static String decrypt(PublicKey publicKey, String cipherText) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    try {
        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);
        // It is a stateful object. so we need to get new one.
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
    }
    if (cipherText == null || cipherText.length() == 0) {
        return cipherText;
    }
    byte[] cipherBytes = Base64.base64ToByteArray(cipherText);
    byte[] plainBytes = cipher.doFinal(cipherBytes);
    return new String(plainBytes);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Cipher(javax.crypto.Cipher) InvalidKeyException(java.security.InvalidKeyException) 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 RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project lzc_app_lib by httplzc.

the class RSAUtils method loadPublicKey.

/**
 * 从字符串中加载公钥
 *
 * @param
 * @return
 * @throws Exception
 */
public static RSAPublicKey loadPublicKey(byte[] buffer) {
    try {
        KeyFactory keyFactory = null;
        keyFactory = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec x509 = new X509EncodedKeySpec(buffer);
        return (RSAPublicKey) keyFactory.generatePublic(x509);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
        return null;
    } catch (NullPointerException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 50 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project QFGame by porschan.

the class RSA method generateKeyPair.

/**
 * 生成密钥对
 */
public static Map<String, String> generateKeyPair() throws Exception {
    /**
     * RSA算法要求有一个可信任的随机数源
     */
    SecureRandom sr = new SecureRandom();
    /**
     * 为RSA算法创建一个KeyPairGenerator对象
     */
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    /**
     * 利用上面的随机数据源初始化这个KeyPairGenerator对象
     */
    kpg.initialize(KEYSIZE, sr);
    /**
     * 生成密匙对
     */
    KeyPair kp = kpg.generateKeyPair();
    /**
     * 得到公钥
     */
    Key publicKey = kp.getPublic();
    byte[] publicKeyBytes = publicKey.getEncoded();
    String pub = new String(Base64.encodeBase64(publicKeyBytes), CHAR_ENCODING);
    /**
     * 得到私钥
     */
    Key privateKey = kp.getPrivate();
    byte[] privateKeyBytes = privateKey.getEncoded();
    String pri = new String(Base64.encodeBase64(privateKeyBytes), CHAR_ENCODING);
    Map<String, String> map = new HashMap<String, String>();
    map.put("publicKey", pub);
    map.put("privateKey", pri);
    RSAPublicKey rsp = (RSAPublicKey) kp.getPublic();
    BigInteger bint = rsp.getModulus();
    byte[] b = bint.toByteArray();
    byte[] deBase64Value = Base64.encodeBase64(b);
    String retValue = new String(deBase64Value);
    map.put("modulus", retValue);
    return map;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) HashMap(java.util.HashMap) BigInteger(java.math.BigInteger) RSAPublicKey(java.security.interfaces.RSAPublicKey)

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