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

com.akeyless.crypto.rsa.RSAUtils Maven / Gradle / Ivy

There is a newer version: 0.0.10
Show newest version
package com.akeyless.crypto.rsa;


import com.akeyless.exceptions.AkeylessRuntimeException;

import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtils {

    public static KeyPair generateKeyPair(int keySize)
            throws NoSuchAlgorithmException {

        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(keySize, new SecureRandom());
        return generator.generateKeyPair();
    }

    public static RSAPublicKey extractPublicKeyFromBytes(byte[] pubKeyBytes) {
        X509EncodedKeySpec spec = new X509EncodedKeySpec(pubKeyBytes);
        RSAPublicKey rsaPubKey = null;
        try {
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PublicKey pubKey = kf.generatePublic(spec);
            if (pubKey instanceof RSAPublicKey) {
                rsaPubKey = (RSAPublicKey) pubKey;
            }
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw new AkeylessRuntimeException("Failed to extract RSA public key" + e.getMessage());
        }

        if(rsaPubKey == null) {
            throw new AkeylessRuntimeException("Failed to extract RSA public key");
        }

        return rsaPubKey;
    }

    static int getByteLength(BigInteger var0) {
        int var1 = var0.bitLength();
        return var1 + 7 >> 3;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy