0

When I convert the signature value to it returns a more than 64 length hex string i want an exact 64 length hex string

Below is the code that I'm using

    public static void GenerateSingature() {
        try {
            String plaintext = "<PlainTextToBeEncrypted>";
            ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp128r1");
            KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
            g.initialize(ecSpec, new SecureRandom());
            KeyPair keypair = g.generateKeyPair();
            PublicKey publicKey = keypair.getPublic();
            PrivateKey privateKey = keypair.getPrivate();

            //SHA-256withPLAIN-ECDSA
            Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
            ecdsaSign.initSign(privateKey);
            ecdsaSign.update(plaintext.getBytes("UTF-8"));
            byte[] signature = ecdsaSign.sign();
            String pub = Base64.getEncoder().encodeToString(publicKey.getEncoded());
            //String sig = Base64.getEncoder().encodeToString(signature);
            String sig = Hex.toHexString(signature);

            //System.out.println(sighex);
            //System.out.println(Base64.getEncoder().encodeToString(Hex.decode(sighex)));
            System.out.println("----------- Signature --------------------");
            System.out.println(sig);

            // ------------------- Verify -------------------------------------------
            Signature ecdsaVerify = Signature.getInstance("SHA256withECDSA");
            KeyFactory kf = KeyFactory.getInstance("EC");

            //EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(obj.getString("publicKey")));
            //KeyFactory keyFactory = KeyFactory.getInstance("EC");
            //PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

            ecdsaVerify.initVerify(publicKey);
            ecdsaVerify.update("<PlainTextToBeEncrypted>".getBytes("UTF-8"));
            boolean result = ecdsaVerify.verify(Hex.decode(sig));
            System.out.printf("Result: %b", result);

        } catch (Exception e) {
            System.out.println(e);
            System.out.println("Error......");
        }
    }

secp128r1 Algorithm Used for elliptic curve 128 Bit encryption Came across SHA-256withPLAIN-ECDSA but the bouncy castle is not supporting this in JAVA where the same exists in the C# package

Any input on how we can achieve a constant 64 Char length encryption Using the above-mentioned algorithm.

Note: secp128r1 is not recommended after 2010 since I'm working with a legacy system

  • The PLAIN-ECDSA schemes are also available in Java for newer BouncyCastle versions, which would be the easiest solution. Alternatively you can do the conversion yourself. The ASN.1 format for signatures is easy to understand, see [here](https://crypto.stackexchange.com/a/1797). – Topaco Dec 08 '20 at 09:53
  • @Topaco Thanks for the reply. need some clarification. ECDSA returns two values r|s which are to type Big Integer, Both r|s values are passed to asn1 Marshal function convert it into bytes which are then encoded to Hex. As for as, I understand we need to convert r|s to big-endian format to get the minimal length? – AbhisheK6006 Dec 08 '20 at 10:58
  • I'm sorry, I can' t follow you: _ECDSA returns two values r|s which are to type Big Integer_: Where in your code should this happen? When I apply `SHA256withPLAIN-ECDSA` I directly get a 32 bytes signature from `ecdsaSign.sign()` (hex encoded 64 chars). Why don't you do the same, since this is what you originally wanted? And the rest of your comment is also unclear to me. You should describe what you are referring to. Or do you ask for an explanation of the phrase _Signed big-endian encoding of minimal length_ from the linked answer? – Topaco Dec 08 '20 at 13:56

0 Answers0