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

The following examples show how to use java.security.interfaces.RSAPublicKey#getModulus() . 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: KeyPairGen.java    From heisenberg 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(2, publicKey);
  }
  catch (InvalidKeyException e)
  {
    RSAPublicKey rsaPublicKey = (RSAPublicKey)publicKey;
    RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
    Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
    cipher = Cipher.getInstance("RSA");
    cipher.init(2, fakePrivateKey);
  }

  if ((cipherText == null) || (cipherText.length() == 0)) {
    return cipherText;
  }

  byte[] cipherBytes = Base64.decodeBase64(cipherText);
  byte[] plainBytes = cipher.doFinal(cipherBytes);

  return new String(plainBytes);
}
 
Example 2
Source File: DNSSEC.java    From dnsjava with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static byte[] fromRSAPublicKey(RSAPublicKey key) {
  DNSOutput out = new DNSOutput();
  BigInteger exponent = key.getPublicExponent();
  BigInteger modulus = key.getModulus();
  int exponentLength = BigIntegerLength(exponent);

  if (exponentLength < 256) {
    out.writeU8(exponentLength);
  } else {
    out.writeU8(0);
    out.writeU16(exponentLength);
  }
  writeBigInteger(out, exponent);
  writeBigInteger(out, modulus);

  return out.toByteArray();
}
 
Example 3
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 4
Source File: HttpTestUtil.java    From gocd with Apache License 2.0 5 votes vote down vote up
private KeyPair generateKeyPair() {
    try {
        KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
        RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate();
        RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic();
        KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
        RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(), privateSeed.getPrivateExponent());
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(), publicSeed.getPublicExponent());
        return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: CaClientExample.java    From xipki with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: BCRSAPublicKey.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
BCRSAPublicKey(
    RSAPublicKey key)
{
    this.algorithmIdentifier = DEFAULT_ALGORITHM_IDENTIFIER;
    this.modulus = key.getModulus();
    this.publicExponent = key.getPublicExponent();
}
 
Example 7
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 8
Source File: JsseJce.java    From jdk8u-jdk 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: CaEmulator.java    From xipki with Apache License 2.0 5 votes vote down vote up
private static AsymmetricKeyParameter generatePublicKeyParameter(PublicKey key)
    throws InvalidKeyException {
  Args.notNull(key, "key");

  if (key instanceof RSAPublicKey) {
    RSAPublicKey rsaKey = (RSAPublicKey) key;
    return new RSAKeyParameters(false, rsaKey.getModulus(), rsaKey.getPublicExponent());
  } else if (key instanceof ECPublicKey) {
    return ECUtil.generatePublicKeyParameter(key);
  } else if (key instanceof DSAPublicKey) {
    return DSAUtil.generatePublicKeyParameter(key);
  } else {
    throw new InvalidKeyException("unknown key " + key.getClass().getName());
  }
}
 
Example 10
Source File: JsseJce.java    From dragonwell8_jdk 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 11
Source File: DOMKeyValue.java    From jdk8u60 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 12
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 13
Source File: RSAUtil.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
static RSAKeyParameters generatePublicKeyParameter(
    RSAPublicKey key)
{
    return new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());

}
 
Example 14
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 15
Source File: DOMKeyValue.java    From jdk8u-jdk 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: DOMKeyValue.java    From jdk8u_jdk 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 17
Source File: DOMKeyValue.java    From hottub 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: RSAUtil.java    From ripple-lib-java with ISC License 4 votes vote down vote up
static RSAKeyParameters generatePublicKeyParameter(
    RSAPublicKey key)
{
    return new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());

}
 
Example 19
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 20
Source File: DOMKeyValue.java    From openjdk-jdk9 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());
}