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

crypto-formats.com.unbound.common.crypto.PKCS1 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.common.crypto;

import com.unbound.common.Converter;

import java.math.BigInteger;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.*;

public class PKCS1
{
  public static ECPrivateKey importECPrivateKey(byte[] der)
  {
    DER.Parser parser = new DER.Parser(der);
    parser.beginSequence();
      parser.getBigInteger(); // version
      byte[] d = parser.getTagBytes(DER.TAG_OCTET_STRING);
      parser.begin((byte)0xa0);
        byte[] oid = parser.getFullTag();
      parser.end();
      parser.begin((byte)0xa1);
        byte[] oct = parser.getBitString(); // unused
      parser.end();
    parser.end();
    EC.Curve curve = EC.getCurveByOid(oid);
    ECPrivateKeySpec spec = new ECPrivateKeySpec(Converter.binToBigNum(d), curve.spec);
    try { return (ECPrivateKey) SystemProvider.KeyFactory.getInstance("EC").generatePrivate(spec); }
    catch (InvalidKeySpecException ex) { throw new IllegalArgumentException(ex); }
  }

  public static byte[] exportECPrivateKey(ECPrivateKey ec)
  {
    EC.Curve curve = EC.getCurve(ec.getParams());
    ECPoint point = EC.getPoint(ec);

    return new DER.Builder().beginSequence().
      addInteger(1).
      add(DER.TAG_OCTET_STRING, Converter.bigNumToBin(ec.getS())).
      begin((byte)0xa0).
        add(curve.oid).
      end().
      begin((byte)0xa1).
        addBitString(curve.toOct(point)).
      end().
    end().toByteArray();
  }

  public static byte[] exportRSAPrivateKey(RSAPrivateCrtKey rsa)
  {
    return new DER.Builder().beginSequence().
      addInteger(0).
      add(rsa.getModulus()).
      add(rsa.getPublicExponent()).
      add(rsa.getPrivateExponent()).
      add(rsa.getPrimeP()).
      add(rsa.getPrimeQ()).
      add(rsa.getPrimeExponentP()).
      add(rsa.getPrimeExponentQ()).
      add(rsa.getCrtCoefficient()).
    end().toByteArray();
  }

  public static byte[] exportRSAPublicKey(RSAPublicKey rsa)
  {
    return new DER.Builder().beginSequence().
      add(rsa.getModulus()).
      add(rsa.getPublicExponent()).
    end().toByteArray();
  }

  public static RSAPublicKey importRSAPublicKey(byte[] der)
  {
    DER.Parser parser = new DER.Parser(der);
      parser.beginSequence();
      BigInteger n    = parser.getBigInteger();
      BigInteger e    = parser.getBigInteger();
    parser.end();
    RSAPublicKeySpec spec = new RSAPublicKeySpec(n, e);
    try { return (RSAPublicKey) SystemProvider.KeyFactory.getInstance("RSA").generatePublic(spec); }
    catch (InvalidKeySpecException ex) { throw new IllegalArgumentException(ex); }
  }

  public static RSAPrivateKey importRSAPrivateKey(byte[] der)
  {
    DER.Parser parser = new DER.Parser(der);
      parser.beginSequence();
      parser.getBigInteger(); // version
      BigInteger n    = parser.getBigInteger();
      BigInteger e    = parser.getBigInteger();
      BigInteger d    = parser.getBigInteger();
      BigInteger p    = parser.getBigInteger();
      BigInteger q    = parser.getBigInteger();
      BigInteger dp   = parser.getBigInteger();
      BigInteger dq   = parser.getBigInteger();
      BigInteger qinv = parser.getBigInteger();
    parser.end();
    RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec(n, e, d, p, q, dp, dq, qinv);
    try { return (RSAPrivateKey) SystemProvider.KeyFactory.getInstance("RSA").generatePrivate(spec); }
    catch (InvalidKeySpecException ex) { throw new IllegalArgumentException(ex); }
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy