All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unbound.provider.UBRSAKeyPairGenerator Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.provider;

import com.unbound.client.Partition;
import com.unbound.client.RSAPrivateKeyObject;
import com.unbound.common.crypto.SystemProvider;

import java.math.BigInteger;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.RSAPublicKeySpec;

public final class UBRSAKeyPairGenerator extends KeyPairGeneratorSpi
{
  private final Partition partition;
  private int bitSize;
  private KeyParameters keyParameter = null;

  UBRSAKeyPairGenerator(Partition partition)
  {
    this.partition = partition;
  }

// --------------------- interface -----------------

  @Override
  public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException
  {
    if (!(params instanceof KeyGenSpec)) throw new InvalidAlgorithmParameterException("Not supported algorithm parameter spec");
    keyParameter = ((KeyGenSpec)params).getKeyParams();
    initialize(((KeyGenSpec)params).getBitSize(), random);
  }

  @Override
  public void initialize(int bitSize, SecureRandom random)
  {
    this.bitSize = bitSize;
    if (bitSize!=2048 && bitSize!=3072 && bitSize!=4096) throw new InvalidParameterException("Unsupported RSA key size " + bitSize);
  }

  @Override
  public KeyPair generateKeyPair()
  {
    try
    {
      RSAPrivateKeyObject object = partition.generateRsaKey(null, bitSize, keyParameter);
      BigInteger modulus = object.getModulus();
      BigInteger publicExponent = object.getPublicExponent();
      PublicKey publicKey = SystemProvider.KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
      return new KeyPair(publicKey, new UBRSAPrivateKey(object));
    }
    catch (Exception e) { throw new ProviderException(e); }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy