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