ecc加密,p128算法不生成64字符十六进制

sqxo8psd  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(229)

当我将签名值转换为时,它会返回一个长度超过64的十六进制字符串,我想要一个长度为64的十六进制字符串
下面是我正在使用的代码

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` 椭圆曲线128位加密算法 `SHA-256withPLAIN-ECDSA` 但是bouncy castle在java中不支持这一点,因为java中的c#包中也存在同样的情况
任何关于如何使用上述算法实现恒定64字符长度加密的输入。
注意:由于我使用的是遗留系统,因此不建议在2010年之后使用secp128r1

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题