下面列出了java.security.interfaces.RSAPublicKey#getPublicExponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String encodePublicKey(RSAPublicKey key, String keyname) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
/* encode the "ssh-rsa" string */
byte[] sshrsa = new byte[] {0, 0, 0, 7, 's', 's', 'h', '-', 'r', 's', 'a'};
out.write(sshrsa);
/* Encode the public exponent */
BigInteger e = key.getPublicExponent();
byte[] data = e.toByteArray();
encodeUInt32(data.length, out);
out.write(data);
/* Encode the modulus */
BigInteger m = key.getModulus();
data = m.toByteArray();
encodeUInt32(data.length, out);
out.write(data);
return "ssh-rsa "
+ Base64.getEncoder().encodeToString(out.toByteArray())
+ " "
+ keyname
+ "@openbaton";
}
/**
* Encodes the public key according to some spec somewhere
* @param key public key to use
* @return the ssh-rsa bytes
*/
public static byte[] encodePublicKey(RSAPublicKey key) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
/* encode the "ssh-rsa" string */
byte[] sshrsa = new byte[] { 0, 0, 0, 7, 's', 's', 'h', '-', 'r', 's', 'a' };
out.write(sshrsa);
/* Encode the public exponent */
BigInteger e = key.getPublicExponent();
byte[] data = e.toByteArray();
encodeUInt32(data.length, out);
out.write(data);
/* Encode the modulus */
BigInteger m = key.getModulus();
data = m.toByteArray();
encodeUInt32(data.length, out);
out.write(data);
return out.toByteArray();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public static PaillierKeyPair convertToPaillier(final KeyPair rsaKeyPair)
throws InvalidKeySpecException, NoSuchAlgorithmException {
// Get keys
final RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) rsaKeyPair.getPrivate();
final RSAPublicKey rsaPublicKey = (RSAPublicKey) rsaKeyPair.getPublic();
// Get fields
final BigInteger n = rsaPublicKey.getModulus(); // treat as 'n'
final BigInteger g = rsaPublicKey.getPublicExponent(); // treat as 'g'
final BigInteger lambda = rsaPrivateKey.getPrivateExponent(); // treat as 'lambda'
// Convert them back to Paillier keys
final PaillierPrivateKey privKey = new PaillierPrivateKey(lambda, n);
final PaillierPublicKey pubKey = new PaillierPublicKey(n, g);
// Convert to key pair
return new PaillierKeyPair(pubKey, privKey);
}
public static PaillierKeyPair convertToPaillier(final KeyPair rsaKeyPair)
throws InvalidKeySpecException, NoSuchAlgorithmException {
// Get keys
final RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) rsaKeyPair.getPrivate();
final RSAPublicKey rsaPublicKey = (RSAPublicKey) rsaKeyPair.getPublic();
// Get fields
final BigInteger n = rsaPublicKey.getModulus(); // treat as 'n'
final BigInteger g = rsaPublicKey.getPublicExponent(); // treat as 'g'
final BigInteger lambda = rsaPrivateKey.getPrivateExponent(); // treat as 'lambda'
// Convert them back to Paillier keys
final PaillierPrivateKey privKey = new PaillierPrivateKey(lambda, n);
final PaillierPublicKey pubKey = new PaillierPublicKey(n, g);
// Convert to key pair
return new PaillierKeyPair(pubKey, privKey);
}
private void checkPublicKey(RSAPublicKey pub) {
BigInteger e = pub.getPublicExponent();
BigInteger n = pub.getModulus();
// Checks that e > 1. [CVE-1999-1444]
assertEquals(1, e.compareTo(BigInteger.ONE));
// TODO(bleichen): Try to generalize and test private keys once the paper is available.
// Test for CVE-2017-15361. Public keys generated by the broken generator can be identified
// heuristically by testing if n is equivalent to a power of 65537 modulo the following primes:
int[] primes = {11, 13, 17, 19, 37, 53, 61, 71, 73, 79, 97, 103, 107, 109, 127, 151, 157};
boolean hasPattern = true;
for (int prime : primes) {
int residue = n.mod(BigInteger.valueOf(prime)).intValue();
int exp = 1;
do {
exp = exp * 65537 % prime;
} while (exp != 1 && exp != residue);
if (exp != residue) {
hasPattern = false;
break;
}
}
assertFalse("Public key has pattern from CVE-2017-15361. n = " + n.toString(), hasPattern);
}
protected BigInteger RSAVP1(RSAPublicKey K, BigInteger s) {
if ( K == null ) {
throw new IllegalArgumentException("K");
}
if ( s == null ) {
throw new IllegalArgumentException("s");
}
BigInteger n = K.getModulus();
BigInteger e = K.getPublicExponent();
if ( s.compareTo(BigInteger.ONE) == -1 || s.compareTo(n) != -1 ) {
throw new IllegalArgumentException("message representative out of range");
}
return s.modPow(e, n);
}
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);
cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one.
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);
}
public static void main(String[] args) throws Exception {
Gmp.checkLoaded();
KeyPair pair = generateKeyPair(RSA_KEY_BITS);
RSAPrivateCrtKey priv = (RSAPrivateCrtKey) pair.getPrivate();
RSAPublicKey pub = (RSAPublicKey) pair.getPublic();
byte[] random = new byte[2048 / 8];
SECURE_RANDOM.nextBytes(random);
// Clear the top bit to ensure it fits.
random[0] &= 0x7F;
final BigInteger message = new BigInteger(1, random);
BigInteger signed =
Gmp.modPowSecure(message, priv.getPrivateExponent(), priv.getModulus());
BigInteger recovered =
Gmp.modPowSecure(signed, pub.getPublicExponent(), pub.getModulus());
assertEquals(message, recovered);
ExecutorService service = Executors.newFixedThreadPool(4);
final GmpInteger exponent = new GmpInteger(pub.getPublicExponent());
final GmpInteger modulus = new GmpInteger(pub.getModulus());
for (int i = 0; i < CORES; ++i) {
service.execute(new Runnable() {
@Override public void run() {
while (true) {
Gmp.modPowSecure(message, exponent, modulus);
}
}
});
}
service.shutdown();
while (true) {
Thread.sleep(1000);
System.gc();
}
}
static RSAPublicKeySpec getRSAPublicKeySpec(PublicKey key) {
if (key instanceof RSAPublicKey) {
RSAPublicKey rsaKey = (RSAPublicKey)key;
return new RSAPublicKeySpec(rsaKey.getModulus(),
rsaKey.getPublicExponent());
}
try {
KeyFactory factory = JsseJce.getKeyFactory("RSA");
return factory.getKeySpec(key, RSAPublicKeySpec.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public RSAKeyEntry(int keysize) throws Exception {
if (keysize % 1024 != 0) {
throw new IllegalArgumentException("invalid RSA keysize " + keysize);
}
AlgorithmIdentifier keyAlgId = new AlgorithmIdentifier(
PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE);
String modulusStr;
if (keysize == 1024 || keysize == 2048 || keysize == 3072 || keysize == 4096) {
if (keysize == 1024) {
modulusStr = N_1024;
} else if (keysize == 2048) {
modulusStr = N_2048;
} else if (keysize == 3072) {
modulusStr = N_3072;
} else { // if (keysize == 4096) {
modulusStr = N_4096;
}
BigInteger modulus = base64ToInt(modulusStr);
this.spki = new SubjectPublicKeyInfo(keyAlgId,
new org.bouncycastle.asn1.pkcs.RSAPublicKey(modulus, PUBLIC_EXPONENT));
} else {
KeyPairGenerator kp = KeyPairGenerator.getInstance("RSA");
kp.initialize(keysize);
RSAPublicKey publicKey = (RSAPublicKey) kp.generateKeyPair().getPublic();
this.spki = new SubjectPublicKeyInfo(keyAlgId,
new org.bouncycastle.asn1.pkcs.RSAPublicKey(
publicKey.getModulus(), publicKey.getPublicExponent()));
}
}
public static PaillierPublicKey convertToPaillierPublicKey(final RSAPublicKey rsaPublicKey)
throws InvalidKeySpecException, NoSuchAlgorithmException {
// Get fields
final BigInteger n = rsaPublicKey.getModulus(); // treat as 'n'
final BigInteger g = rsaPublicKey.getPublicExponent(); // treat as 'g'
// Convert them back to Paillier public key
return new PaillierPublicKey(n, g);
}
public static PaillierPublicKey convertToPaillierPublicKey(final RSAPublicKey rsaPublicKey)
throws InvalidKeySpecException, NoSuchAlgorithmException {
// Get fields
final BigInteger n = rsaPublicKey.getModulus(); // treat as 'n'
final BigInteger g = rsaPublicKey.getPublicExponent(); // treat as 'g'
// Convert them back to Paillier public key
return new PaillierPublicKey(n, g);
}
static RSAPublicKeySpec getRSAPublicKeySpec(PublicKey key) {
if (key instanceof RSAPublicKey) {
RSAPublicKey rsaKey = (RSAPublicKey)key;
return new RSAPublicKeySpec(rsaKey.getModulus(),
rsaKey.getPublicExponent());
}
try {
KeyFactory factory = KeyFactory.getInstance("RSA");
return factory.getKeySpec(key, RSAPublicKeySpec.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static RsaPublicKeyDef from(RSAPublicKey key) {
return new RsaPublicKeyDef(key.getModulus(), key.getPublicExponent(), key);
}
RSA(PublicKey key) throws KeyException {
super(key);
RSAPublicKey rkey = (RSAPublicKey)key;
exponent = new DOMCryptoBinary(rkey.getPublicExponent());
modulus = new DOMCryptoBinary(rkey.getModulus());
}
RSA(PublicKey key) throws KeyException {
super(key);
RSAPublicKey rkey = (RSAPublicKey)key;
exponent = new DOMCryptoBinary(rkey.getPublicExponent());
modulus = new DOMCryptoBinary(rkey.getModulus());
}
public static RSACOSEKey create(RSAPublicKey publicKey, COSEAlgorithmIdentifier alg) {
publicKey.getPublicExponent();
byte[] n = publicKey.getModulus().toByteArray();
byte[] e = publicKey.getPublicExponent().toByteArray();
return new RSACOSEKey(null, alg, null, n, e);
}
static public RSAKeyParameters generatePublicKeyParameter(
RSAPublicKey key)
{
return new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());
}
RSA(PublicKey key) throws KeyException {
super(key);
RSAPublicKey rkey = (RSAPublicKey)key;
exponent = new DOMCryptoBinary(rkey.getPublicExponent());
modulus = new DOMCryptoBinary(rkey.getModulus());
}
JCERSAPublicKey(
RSAPublicKey key)
{
this.modulus = key.getModulus();
this.publicExponent = key.getPublicExponent();
}