下面列出了java.security.spec.RSAKeyGenParameterSpec#F4() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
int size = 0;
if (args.length >= 1) {
size = Integer.parseInt(args[0]);
} else {
throw new RuntimeException("Missing keysize to test with");
}
BigInteger publicExponent
= (args.length >= 2) ? new BigInteger(args[1]) : RSAKeyGenParameterSpec.F4;
System.out.println("Running test with key size: " + size
+ " and public exponent: " + publicExponent);
KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
kpg1.initialize(new RSAKeyGenParameterSpec(size, publicExponent));
if (!specTest(kpg1.generateKeyPair(), publicExponent)) {
throw new RuntimeException("Test failed.");
}
}
public static void main(String[] args) throws Exception {
int size = 0;
if (args.length >= 1) {
size = Integer.parseInt(args[0]);
} else {
throw new RuntimeException("Missing keysize to test with");
}
BigInteger publicExponent
= (args.length >= 2) ? new BigInteger(args[1]) : RSAKeyGenParameterSpec.F4;
System.out.println("Running test with key size: " + size
+ " and public exponent: " + publicExponent);
KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
kpg1.initialize(new RSAKeyGenParameterSpec(size, publicExponent));
if (!specTest(kpg1.generateKeyPair(), publicExponent)) {
throw new RuntimeException("Test failed.");
}
}
/**
* Initialize Key Generator (Blowfish keygen and RSA keygen)
*
* @throws GeneralSecurityException
*/
public static void init() throws GeneralSecurityException {
log.info("Initializing Key Generator...");
blowfishKeyGen = KeyGenerator.getInstance("Blowfish");
KeyPairGenerator rsaKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
rsaKeyPairGenerator.initialize(spec);
encryptedRSAKeyPairs = new EncryptedRSAKeyPair[10];
for (int i = 0; i < 10; i++) {
encryptedRSAKeyPairs[i] = new EncryptedRSAKeyPair(
rsaKeyPairGenerator.generateKeyPair());
}
// Pre-init RSA cipher.. saving about 300ms
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
rsaCipher.init(Cipher.DECRYPT_MODE, encryptedRSAKeyPairs[0].getRSAKeyPair().getPrivate());
}
public static void main(String[] args) throws Exception {
int size = 0;
if (args.length >= 1) {
size = Integer.parseInt(args[0]);
} else {
throw new RuntimeException("Missing keysize to test with");
}
BigInteger publicExponent
= (args.length >= 2) ? new BigInteger(args[1]) : RSAKeyGenParameterSpec.F4;
System.out.println("Running test with key size: " + size
+ " and public exponent: " + publicExponent);
KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
kpg1.initialize(new RSAKeyGenParameterSpec(size, publicExponent));
if (!specTest(kpg1.generateKeyPair(), publicExponent)) {
throw new RuntimeException("Test failed.");
}
}
private LoginController() throws GeneralSecurityException
{
_log.info("Loading LoginContoller...");
_hackProtection = new HashMap<>();
_keyPairs = new ScrambledKeyPair[10];
KeyPairGenerator keygen = null;
keygen = KeyPairGenerator.getInstance("RSA");
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
keygen.initialize(spec);
//generate the initial set of keys
for (int i = 0; i < 10; i++)
{
_keyPairs[i] = new ScrambledKeyPair(keygen.generateKeyPair());
}
_log.info("Cached 10 KeyPairs for RSA communication");
testCipher((RSAPrivateKey) _keyPairs[0]._pair.getPrivate());
// Store keys for blowfish communication
generateBlowFishKeys();
}
public void initialize(int keySize, SecureRandom random) {
// do not allow unreasonably small or large key sizes,
// probably user error
try {
RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
512, 64 * 1024);
} catch (InvalidKeyException e) {
throw new InvalidParameterException(e.getMessage());
}
this.keySize = keySize;
this.random = random;
this.publicExponent = RSAKeyGenParameterSpec.F4;
}
public static void main(String[] args) throws Exception {
RSAKeyGenParameterSpec rsaSpec =
new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
kpg.initialize(rsaSpec);
// test generateKeyPair
KeyPair kpair = kpg.generateKeyPair();
if (kpair == null) {
throw new Exception("no keypair generated");
}
}
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
throws InvalidAlgorithmParameterException {
if (params instanceof RSAKeyGenParameterSpec == false) {
throw new InvalidAlgorithmParameterException
("Params must be instance of RSAKeyGenParameterSpec");
}
RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
int tmpKeySize = rsaSpec.getKeysize();
BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();
if (tmpPublicExponent == null) {
tmpPublicExponent = RSAKeyGenParameterSpec.F4;
} else {
if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
throw new InvalidAlgorithmParameterException
("Public exponent must be 3 or larger");
}
if (tmpPublicExponent.bitLength() > tmpKeySize) {
throw new InvalidAlgorithmParameterException
("Public exponent must be smaller than key size");
}
}
// do not allow unreasonably large key sizes, probably user error
try {
RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent,
512, 64 * 1024);
} catch (InvalidKeyException e) {
throw new InvalidAlgorithmParameterException(
"Invalid key sizes", e);
}
this.keySize = tmpKeySize;
this.publicExponent = tmpPublicExponent;
this.random = random;
}
private KeyPair generateRSAKeyPair() throws Exception {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
KeyPairGenerator generator;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
generator = KeyPairGenerator.getInstance("RSA");
} else {
generator = KeyPairGenerator.getInstance("RSA", "BC");
}
generator.initialize(spec, secureRandom);
return generator.generateKeyPair();
}
public void initialize(int keySize, SecureRandom random) {
// do not allow unreasonably small or large key sizes,
// probably user error
try {
RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
512, 64 * 1024);
} catch (InvalidKeyException e) {
throw new InvalidParameterException(e.getMessage());
}
this.keySize = keySize;
this.random = random;
this.publicExponent = RSAKeyGenParameterSpec.F4;
}
public void initialize(int keySize, SecureRandom random) {
// do not allow unreasonably small or large key sizes,
// probably user error
try {
RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
512, 64 * 1024);
} catch (InvalidKeyException e) {
throw new InvalidParameterException(e.getMessage());
}
this.keySize = keySize;
this.random = random;
this.publicExponent = RSAKeyGenParameterSpec.F4;
}
public static void main(String[] args) throws Exception {
RSAKeyGenParameterSpec rsaSpec =
new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
kpg.initialize(rsaSpec);
// test generateKeyPair
KeyPair kpair = kpg.generateKeyPair();
if (kpair == null) {
throw new Exception("no keypair generated");
}
}
private void initializeScrambledKeys() throws GeneralSecurityException {
var keygen = KeyPairGenerator.getInstance("RSA");
var spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
keygen.initialize(spec);
_keyPairs = new ScrambledKeyPair[10];
for (int i = 0; i < 10; i++) {
_keyPairs[i] = new ScrambledKeyPair(keygen.generateKeyPair());
}
LOGGER.info("Cached 10 KeyPairs for RSA communication");
testCipher((RSAPrivateKey) _keyPairs[0].getPair().getPrivate());
}
public void initialize(int keySize, SecureRandom random) {
// do not allow unreasonably small or large key sizes,
// probably user error
try {
RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
512, 64 * 1024);
} catch (InvalidKeyException e) {
throw new InvalidParameterException(e.getMessage());
}
this.keySize = keySize;
this.random = random;
this.publicExponent = RSAKeyGenParameterSpec.F4;
}
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
throws InvalidAlgorithmParameterException {
if (params instanceof RSAKeyGenParameterSpec == false) {
throw new InvalidAlgorithmParameterException
("Params must be instance of RSAKeyGenParameterSpec");
}
RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
int tmpKeySize = rsaSpec.getKeysize();
BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();
if (tmpPublicExponent == null) {
tmpPublicExponent = RSAKeyGenParameterSpec.F4;
} else {
if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
throw new InvalidAlgorithmParameterException
("Public exponent must be 3 or larger");
}
if (tmpPublicExponent.bitLength() > tmpKeySize) {
throw new InvalidAlgorithmParameterException
("Public exponent must be smaller than key size");
}
}
// do not allow unreasonably large key sizes, probably user error
try {
RSAKeyFactory.checkKeyLengths(tmpKeySize, tmpPublicExponent,
512, 64 * 1024);
} catch (InvalidKeyException e) {
throw new InvalidAlgorithmParameterException(
"Invalid key sizes", e);
}
this.keySize = tmpKeySize;
this.publicExponent = tmpPublicExponent;
this.random = random;
}
public static void main(String[] args) throws Exception {
RSAKeyGenParameterSpec rsaSpec =
new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
kpg.initialize(rsaSpec);
// test generateKeyPair
KeyPair kpair = kpg.generateKeyPair();
if (kpair == null) {
throw new Exception("no keypair generated");
}
}
public static void main(String[] args) throws Exception {
RSAKeyGenParameterSpec rsaSpec =
new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
kpg.initialize(rsaSpec);
// test generateKeyPair
KeyPair kpair = kpg.generateKeyPair();
if (kpair == null) {
throw new Exception("no keypair generated");
}
}
public static void main(String[] args) throws Exception {
RSAKeyGenParameterSpec rsaSpec =
new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
kpg.initialize(rsaSpec);
// test generateKeyPair
KeyPair kpair = kpg.generateKeyPair();
if (kpair == null) {
throw new Exception("no keypair generated");
}
}
public static void main(String[] args) throws Exception {
RSAKeyGenParameterSpec rsaSpec =
new RSAKeyGenParameterSpec (1024, RSAKeyGenParameterSpec.F4);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
kpg.initialize(rsaSpec);
// test generateKeyPair
KeyPair kpair = kpg.generateKeyPair();
if (kpair == null) {
throw new Exception("no keypair generated");
}
}
@Override
public AlgorithmParameterSpec getSpec() {
return new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4);
}