use of java.security.spec.RSAPublicKeySpec in project j2objc by google.
the class RSAPublicKeySpecTest method testRSAPublicKeySpec02.
/**
* Test #2 for <code>RSAPublicKeySpec</code> constructor
* Assertion: Constructs <code>RSAPublicKeySpec</code>
* object using valid parameters
*/
public final void testRSAPublicKeySpec02() {
KeySpec ks = new RSAPublicKeySpec(null, null);
assertTrue(ks instanceof RSAPublicKeySpec);
}
use of java.security.spec.RSAPublicKeySpec in project j2objc by google.
the class RSAPublicKeyTest method test_getPublicExponent.
/**
* java.security.interfaces.RSAPublicKey
* #getPublicExponent()
*/
public void test_getPublicExponent() throws Exception {
KeyFactory gen = KeyFactory.getInstance("RSA");
final BigInteger n = BigInteger.valueOf(3233);
final BigInteger e = BigInteger.valueOf(17);
RSAPublicKey key = (RSAPublicKey) gen.generatePublic(new RSAPublicKeySpec(n, e));
assertEquals("invalid public exponent", e, key.getPublicExponent());
}
use of java.security.spec.RSAPublicKeySpec in project druid by alibaba.
the class ConfigTools method encrypt.
public static String encrypt(byte[] keyBytes, String plainText) throws Exception {
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory factory = KeyFactory.getInstance("RSA", "SunRsaSign");
PrivateKey privateKey = factory.generatePrivate(spec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
try {
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
} catch (InvalidKeyException e) {
//For IBM JDK, 原因请看解密方法中的说明
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
}
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedString = Base64.byteArrayToBase64(encryptedBytes);
return encryptedString;
}
use of java.security.spec.RSAPublicKeySpec in project remusic by aa112901.
the class RsaCal method main1.
public static void main1(String[] args) throws Exception {
// TODO Auto-generated method stub
HashMap<String, Object> map = RSAUtils.getKeys();
//生成公钥和私钥
// RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
// RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
String big1 = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7";
String big2 = "010001";
// System.out.println(b1.toString());
// System.out.println(b2.toString());
RSAPublicKeySpec rsaPubKS = new RSAPublicKeySpec(new BigInteger(big1, 16), new BigInteger(big2, 16));
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey publicKey = (RSAPublicKey) kf.generatePublic(rsaPubKS);
//模
String modulus = publicKey.getModulus().toString();
//公钥指数
String public_exponent = publicKey.getPublicExponent().toString();
System.err.println(modulus);
System.err.println(public_exponent);
//私钥指数
// String private_exponent = privateKey.getPrivateExponent().toString();
//明文
String ming = "628211bf359401b7";
//使用模和指数生成公钥和私钥
RSAPublicKey pubKey = RSAUtils.getPublicKey(new BigInteger(big1, 16) + "", new BigInteger(big2, 16) + "");
// RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent);
//加密后的密文
String mi = RSAUtils.encryptByPublicKey(ming, pubKey);
System.err.println(mi);
//解密后的明文
// ming = RSAUtils.decryptByPrivateKey(mi, priKey);
// System.err.println(ming);
}
use of java.security.spec.RSAPublicKeySpec in project Mycat-Server by MyCATApache.
the class DecryptUtil method encrypt.
public static String encrypt(byte[] keyBytes, String plainText) throws Exception {
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(spec);
Cipher cipher = Cipher.getInstance("RSA");
try {
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
} catch (InvalidKeyException e) {
//For IBM JDK, 原因请看解密方法中的说明
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
}
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedString = Base64.byteArrayToBase64(encryptedBytes);
return encryptedString;
}
Aggregations