javadigital-signaturederber

Is there a way to set signature format for ECDSA to BER encoding in java?


I'm trying to sign a message with a private key. It works well, but the signature is encoded in DER encoding. What I want is to get the signature in BER encoding. Here is the method used to sign :

public static byte[] sign(String plainText, String privateKeyPath) throws 
Exception {
    PrivateKey privateKey = getPrivate(privateKeyPath);
    System.out.println(privateKey.getAlgorithm());
    Signature ecdsaSign = Signature.getInstance("SHA256withECDSA", "BC");
    ecdsaSign.initSign(privateKey);
    ecdsaSign.update(plainText.getBytes("UTF-8"));
    byte[] signature = ecdsaSign.sign();
    return signature;
}

I'm using BouncyCastle library


Solution

  • DER encoding is valid BER encoding

    From X.690 summary in wikipedia

    DER (Distinguished Encoding Rules) is a restricted variant of BER for producing unequivocal transfer syntax for data structures described by ASN.1. Like CER, DER encodings are valid BER encodings. DER is the same thing as BER with all but one sender's options removed.