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);
}
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;
}
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;
}
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"));
}
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"));
}
Aggregations