Search in sources :

Example 36 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project XobotOS by xamarin.

the class JDKDigestSignature method engineInitSign.

protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    if (!(privateKey instanceof RSAPrivateKey)) {
        throw new InvalidKeyException("Supplied key (" + getType(privateKey) + ") is not a RSAPrivateKey instance");
    }
    CipherParameters param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) privateKey);
    digest.reset();
    cipher.init(true, param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 37 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project Gradle-demo by Arisono.

the class RSAUtils method generateKey.

/**
	 * 生成RSA的公钥和私钥
	 */
public static Map<String, Object> generateKey() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    //512-65536 & 64的倍数
    keyPairGenerator.initialize(KEY_SIZE);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    Map<String, Object> keyMap = new HashMap<String, Object>();
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) HashMap(java.util.HashMap) KeyPairGenerator(java.security.KeyPairGenerator) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 38 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project Gradle-demo by Arisono.

the class RSAUtil method initKey.

/**
	 * 生成RSA的公钥和私钥
	 */
public static Map<String, Object> initKey() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    //512-65536 & 64的倍数
    keyPairGenerator.initialize(1024);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    Map<String, Object> keyMap = new HashMap<String, Object>();
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) HashMap(java.util.HashMap) KeyPairGenerator(java.security.KeyPairGenerator) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 39 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project Gradle-demo by Arisono.

the class testRSA method main.

public static void main(String[] args) throws Exception {
    Map<String, Object> keyMap = RSAUtil.initKey();
    RSAPublicKey rsaPublicKey = RSAUtil.getpublicKey(keyMap);
    RSAPrivateKey rsaPrivateKey = RSAUtil.getPrivateKey(keyMap);
    System.out.println("RSA PublicKey: " + rsaPublicKey);
    System.out.println("RSA PrivateKey: " + rsaPrivateKey);
    //公钥加密
    byte[] rsaResult = RSAUtil.encrypt(DATA.getBytes(), rsaPublicKey);
    System.out.println(DATA + "====>>>> RSA 加密>>>>====" + BytesToHex.fromBytesToHex(rsaResult));
    //System.out.println(DATA + "====>>>> RSA 加密>>>>====" + new String(rsaResult, "utf-8"));
    //私钥解密
    byte[] plainResult = RSAUtil.decrypt(rsaResult, rsaPrivateKey);
    System.out.println(DATA + "====>>>> RSA 解密>>>>====" + BytesToHex.fromBytesToHex(plainResult));
    System.out.println(DATA + "====>>>> RSA 解密>>>>====" + new String(plainResult, "utf-8"));
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 40 with RSAPrivateKey

use of java.security.interfaces.RSAPrivateKey in project Gradle-demo by Arisono.

the class testRSAUtils method main.

public static void main(String[] args) throws Exception {
    Map<String, Object> keyMap = RSAUtil.initKey();
    RSAPublicKey rsaPublicKey = RSAUtil.getpublicKey(keyMap);
    RSAPrivateKey rsaPrivateKey = RSAUtil.getPrivateKey(keyMap);
    System.out.println("RSA PublicKey: " + rsaPublicKey);
    System.out.println("RSA PrivateKey: " + rsaPrivateKey);
    //公钥加密
    byte[] rsaResult = RSAUtil.encrypt(DATA.getBytes(), rsaPublicKey);
    System.out.println(DATA + "====>>>> RSA 加密>>>>====" + BytesToHex.fromBytesToHex(rsaResult));
    //System.out.println(DATA + "====>>>> RSA 加密>>>>====" + new String(rsaResult, "utf-8"));
    //私钥解密
    byte[] plainResult = RSAUtil.decrypt(rsaResult, rsaPrivateKey);
    System.out.println(DATA + "====>>>> RSA 解密>>>>====" + BytesToHex.fromBytesToHex(plainResult));
    System.out.println(DATA + "====>>>> RSA 解密>>>>====" + new String(plainResult, "utf-8"));
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Aggregations

RSAPrivateKey (java.security.interfaces.RSAPrivateKey)55 RSAPublicKey (java.security.interfaces.RSAPublicKey)27 KeyFactory (java.security.KeyFactory)15 InvalidKeyException (java.security.InvalidKeyException)14 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)14 KeyPair (java.security.KeyPair)12 PrivateKey (java.security.PrivateKey)12 PublicKey (java.security.PublicKey)11 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)11 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)10 KeyPairGenerator (java.security.KeyPairGenerator)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 BigInteger (java.math.BigInteger)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)7 IOException (java.io.IOException)5 Key (java.security.Key)5 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 HashMap (java.util.HashMap)5