public byte[] getEncoded() {
    SubjectPublicKeyInfo info;
    byte[] keyEnc = this.getY().toByteArray();
    byte[] keyBytes;

    if (keyEnc[0] == 0) {
      keyBytes = new byte[keyEnc.length - 1];
    } else {
      keyBytes = new byte[keyEnc.length];
    }

    for (int i = 0; i != keyBytes.length; i++) {
      keyBytes[i] = keyEnc[keyEnc.length - 1 - i]; // must be little endian
    }

    try {
      if (gost3410Spec instanceof GOST3410ParameterSpec) {
        if (gost3410Spec.getEncryptionParamSetOID() != null) {
          info =
              new SubjectPublicKeyInfo(
                  new AlgorithmIdentifier(
                      CryptoProObjectIdentifiers.gostR3410_94,
                      new GOST3410PublicKeyAlgParameters(
                          new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()),
                          new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()),
                          new ASN1ObjectIdentifier(gost3410Spec.getEncryptionParamSetOID()))),
                  new DEROctetString(keyBytes));
        } else {
          info =
              new SubjectPublicKeyInfo(
                  new AlgorithmIdentifier(
                      CryptoProObjectIdentifiers.gostR3410_94,
                      new GOST3410PublicKeyAlgParameters(
                          new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()),
                          new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()))),
                  new DEROctetString(keyBytes));
        }
      } else {
        info =
            new SubjectPublicKeyInfo(
                new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94),
                new DEROctetString(keyBytes));
      }

      return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
    } catch (IOException e) {
      return null;
    }
  }
示例#2
0
  public byte[] encodeKey(CipherParameters key) {
    if (!(key instanceof RSAKeyParameters)) {
      return null;
    }

    if (((RSAKeyParameters) key).isPrivate()) {
      return null;
    }

    RSAKeyParameters rsaKey = (RSAKeyParameters) key;

    ASN1EncodableVector encodable = new ASN1EncodableVector();
    encodable.add(new ASN1Integer(rsaKey.getModulus()));
    encodable.add(new ASN1Integer(rsaKey.getExponent()));

    return KeyUtil.getEncodedSubjectPublicKeyInfo(
        new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()),
        new DERSequence(encodable));
  }