Search in sources :

Example 26 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey 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 27 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project CloudStack-archive by CloudStack-extras.

the class RSAHelper method readKey.

private static RSAPublicKey readKey(String key) throws Exception {
    byte[] encKey = Base64.decodeBase64(key.split(" ")[1]);
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(encKey));
    byte[] header = readElement(dis);
    String pubKeyFormat = new String(header);
    if (!pubKeyFormat.equals("ssh-rsa"))
        throw new RuntimeException("Unsupported format");
    byte[] publicExponent = readElement(dis);
    byte[] modulus = readElement(dis);
    KeySpec spec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
    KeyFactory keyFactory = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
    RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(spec);
    return pubKey;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DataInputStream(java.io.DataInputStream) KeyFactory(java.security.KeyFactory)

Example 28 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey 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)

Example 29 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project azure-sdk-for-java by Azure.

the class JsonWebKey method fromRSA.

/**
     * Converts RSA key pair to JSON web key.
     * @param keyPair RSA key pair
     * @return the JSON web key, converted from RSA key pair.
     */
public static JsonWebKey fromRSA(KeyPair keyPair) {
    RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) keyPair.getPrivate();
    JsonWebKey key = null;
    if (privateKey != null) {
        key = new JsonWebKey().withKty(JsonWebKeyType.RSA).withN(toByteArray(privateKey.getModulus())).withE(toByteArray(privateKey.getPublicExponent())).withD(toByteArray(privateKey.getPrivateExponent())).withP(toByteArray(privateKey.getPrimeP())).withQ(toByteArray(privateKey.getPrimeQ())).withDp(toByteArray(privateKey.getPrimeExponentP())).withDq(toByteArray(privateKey.getPrimeExponentQ())).withQi(toByteArray(privateKey.getCrtCoefficient()));
    } else {
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        key = new JsonWebKey().withKty(JsonWebKeyType.RSA).withN(toByteArray(publicKey.getModulus())).withE(toByteArray(publicKey.getPublicExponent())).withD(null).withP(null).withQ(null).withDp(null).withDq(null).withQi(null);
    }
    return key;
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPublicKey(java.security.interfaces.RSAPublicKey)

Example 30 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project cloudstack by apache.

the class RSAHelper method encryptWithSSHPublicKey.

public static String encryptWithSSHPublicKey(String sshPublicKey, String content) {
    String returnString = null;
    try {
        RSAPublicKey publicKey = readKey(sshPublicKey);
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", BouncyCastleProvider.PROVIDER_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey, new SecureRandom());
        byte[] encrypted = cipher.doFinal(content.getBytes());
        returnString = Base64.encodeBase64String(encrypted);
    } catch (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)83 PublicKey (java.security.PublicKey)29 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)24 BigInteger (java.math.BigInteger)17 KeyFactory (java.security.KeyFactory)17 X509Certificate (java.security.cert.X509Certificate)16 KeyPair (java.security.KeyPair)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 PrivateKey (java.security.PrivateKey)14 ECPublicKey (java.security.interfaces.ECPublicKey)14 IOException (java.io.IOException)13 InvalidKeyException (java.security.InvalidKeyException)13 KeyPairGenerator (java.security.KeyPairGenerator)13 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)13 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 CertificateFactory (java.security.cert.CertificateFactory)9 RSAKey (java.security.interfaces.RSAKey)8 DSAPublicKey (java.security.interfaces.DSAPublicKey)7