java.security.interfaces.RSAPublicKey#getPublicExponent ( )源码实例Demo

下面列出了java.security.interfaces.RSAPublicKey#getPublicExponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: NFVO   文件: KeyHelper.java
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";
}
 
源代码2 项目: blueocean-plugin   文件: SSHKeyUtils.java
/**
 * 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);
    }
}
 
源代码3 项目: protect   文件: KeyGeneratorCli.java
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);
}
 
源代码4 项目: protect   文件: MessageStatusCli.java
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);
}
 
源代码5 项目: wycheproof   文件: RsaKeyTest.java
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);
}
 
源代码6 项目: azure-keyvault-java   文件: RsaSignature.java
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);
}
 
源代码7 项目: MultimediaDesktop   文件: ConfigTools.java
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);
}
 
源代码8 项目: jna-gmp   文件: ModPowLeak.java
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();
  }
}
 
源代码9 项目: jdk8u60   文件: JsseJce.java
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);
    }
}
 
源代码10 项目: xipki   文件: CaEnrollBenchKeyEntry.java
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()));
  }
}
 
源代码11 项目: protect   文件: KeyGeneratorCli.java
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);
}
 
源代码12 项目: protect   文件: Pem.java
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);
}
 
源代码13 项目: Bytecoder   文件: JsseJce.java
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);
    }
}
 
源代码14 项目: swim   文件: RsaPublicKeyDef.java
public static RsaPublicKeyDef from(RSAPublicKey key) {
  return new RsaPublicKeyDef(key.getModulus(), key.getPublicExponent(), key);
}
 
源代码15 项目: openjdk-8   文件: DOMKeyValue.java
RSA(PublicKey key) throws KeyException {
    super(key);
    RSAPublicKey rkey = (RSAPublicKey)key;
    exponent = new DOMCryptoBinary(rkey.getPublicExponent());
    modulus = new DOMCryptoBinary(rkey.getModulus());
}
 
源代码16 项目: openjdk-8-source   文件: DOMKeyValue.java
RSA(PublicKey key) throws KeyException {
    super(key);
    RSAPublicKey rkey = (RSAPublicKey)key;
    exponent = new DOMCryptoBinary(rkey.getPublicExponent());
    modulus = new DOMCryptoBinary(rkey.getModulus());
}
 
源代码17 项目: webauthn4j   文件: RSACOSEKey.java
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);
}
 
源代码18 项目: TorrentEngine   文件: RSAUtil.java
static public RSAKeyParameters generatePublicKeyParameter(
    RSAPublicKey    key)
{
    return new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());

}
 
源代码19 项目: openjdk-jdk8u   文件: DOMKeyValue.java
RSA(PublicKey key) throws KeyException {
    super(key);
    RSAPublicKey rkey = (RSAPublicKey)key;
    exponent = new DOMCryptoBinary(rkey.getPublicExponent());
    modulus = new DOMCryptoBinary(rkey.getModulus());
}
 
源代码20 项目: BiglyBT   文件: JCERSAPublicKey.java
JCERSAPublicKey(
    RSAPublicKey key)
{
    this.modulus = key.getModulus();
    this.publicExponent = key.getPublicExponent();
}