Java Code Examples for java.security.interfaces.RSAPublicKey#getPublicExponent()

The following examples show how to use java.security.interfaces.RSAPublicKey#getPublicExponent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyHelper.java    From NFVO with Apache License 2.0 6 votes vote down vote up
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";
}
 
Example 2
Source File: ConfigTools.java    From MultimediaDesktop with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: RsaSignature.java    From azure-keyvault-java with MIT License 6 votes vote down vote up
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);
}
 
Example 4
Source File: RsaKeyTest.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: MessageStatusCli.java    From protect with MIT License 6 votes vote down vote up
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);
}
 
Example 6
Source File: KeyGeneratorCli.java    From protect with MIT License 6 votes vote down vote up
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);
}
 
Example 7
Source File: SSHKeyUtils.java    From blueocean-plugin with MIT License 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 8
Source File: JsseJce.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 9
Source File: CaEnrollBenchKeyEntry.java    From xipki with Apache License 2.0 5 votes vote down vote up
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()));
  }
}
 
Example 10
Source File: ModPowLeak.java    From jna-gmp with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 11
Source File: KeyGeneratorCli.java    From protect with MIT License 5 votes vote down vote up
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);
}
 
Example 12
Source File: Pem.java    From protect with MIT License 5 votes vote down vote up
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);
}
 
Example 13
Source File: JsseJce.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 14
Source File: RSACOSEKey.java    From webauthn4j with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 15
Source File: DOMKeyValue.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
RSA(PublicKey key) throws KeyException {
    super(key);
    RSAPublicKey rkey = (RSAPublicKey)key;
    exponent = new DOMCryptoBinary(rkey.getPublicExponent());
    modulus = new DOMCryptoBinary(rkey.getModulus());
}
 
Example 16
Source File: RSAUtil.java    From TorrentEngine with GNU General Public License v3.0 4 votes vote down vote up
static public RSAKeyParameters generatePublicKeyParameter(
    RSAPublicKey    key)
{
    return new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());

}
 
Example 17
Source File: DOMKeyValue.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
RSA(PublicKey key) throws KeyException {
    super(key);
    RSAPublicKey rkey = (RSAPublicKey)key;
    exponent = new DOMCryptoBinary(rkey.getPublicExponent());
    modulus = new DOMCryptoBinary(rkey.getModulus());
}
 
Example 18
Source File: DOMKeyValue.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
RSA(PublicKey key) throws KeyException {
    super(key);
    RSAPublicKey rkey = (RSAPublicKey)key;
    exponent = new DOMCryptoBinary(rkey.getPublicExponent());
    modulus = new DOMCryptoBinary(rkey.getModulus());
}
 
Example 19
Source File: JCERSAPublicKey.java    From BiglyBT with GNU General Public License v2.0 4 votes vote down vote up
JCERSAPublicKey(
    RSAPublicKey key)
{
    this.modulus = key.getModulus();
    this.publicExponent = key.getPublicExponent();
}
 
Example 20
Source File: RsaPublicKeyDef.java    From swim with Apache License 2.0 4 votes vote down vote up
public static RsaPublicKeyDef from(RSAPublicKey key) {
  return new RsaPublicKeyDef(key.getModulus(), key.getPublicExponent(), key);
}