在文本文件中使用私钥写入已签名哈希的字节数组并将其作为相同格式取回的问题

Problems to write a byte array of a signed hash with a private key in a text file and get it back as the same format

我正在将字节数组写入带符号哈希的文本文件,我首先将其转换为 base64 字符串,然后在写入文本文件之前使用编码 UTF-8 再次转换为字节数组,我的当前的问题是,当我取回字节时,我不知道如何转换为原始格式以使用 public 密钥验证我的签名哈希,有人知道如何将其转换回来吗?

这就是我将原始字节转换为能够写入文本文件的方式:

        public static byte[] ConvertToBase64(byte[] bytes)
        {
            string base64 = Convert.ToBase64String(bytes);
            return Encoding.UTF8.GetBytes(base64);
        }

这是我对哈希签名的方式:

        private byte[] signData(byte[] dataValue)
        {
            X509Certificate2 privateKey = new X509Certificate2(privateKeyfileName, password);

            //Encryting/Signing a hash
            using (ECDsa ecdsa = privateKey.GetECDsaPrivateKey())
            {
                if (ecdsa == null) throw new Exception("Not an ECDSA cert, or has no private key");

                return ecdsa.SignData(dataValue, HashAlgorithmName.SHA256);
            }
        }

这是我创建哈希的方式:

        // The cryptographic service provider.
        private SHA256 Sha256 = SHA256.Create();

        // Compute the file's hash.
        private byte[] GetHashSha256FromFile(string filename)
        {
            using (FileStream stream = File.OpenRead(filename))
            {
                return Sha256.ComputeHash(stream);
            }
        }

如果我可以从我的原始签名哈希中获取值,这就是我应该如何验证签名哈希:

        private bool verifyData(byte[] dataValue, byte[] dataSigned)
        {
            byte[] mycertCer = Properties.Resources.mycertCer;
            X509Certificate2 publicKey = new X509Certificate2(mycertCer, password);

            //Checking the hash and signature
            using (ECDsa ecdsa = publicKey.GetECDsaPublicKey())
            {
                if (ecdsa == null) throw new Exception("Not an ECDSA cert, or has no private key");

                return ecdsa.VerifyData(dataValue, dataSigned, HashAlgorithmName.SHA256);
            }
        }

我将不胜感激任何帮助,如果您需要更多详细信息,请告诉我。

签名是二进制数据,因此您可以将其存储为原始字节(例如,在以任何扩展名 .bin 结尾的文件上)

public static byte[] readFileBytes(String filepath) throws Exception {
    return Files.readAllBytes(Paths.get(filepath));
}

public static void writeFileBytes(String filepath, byte[] input) throws Exception {
    try (FileOutputStream fos = new FileOutputStream(filepath)) {
        fos.write(input);
    }
}

如果你真的想把这个 binary/bytes 数组存储到文本文件中,那么你必须使用 base64 或 hex 进行安全编码:

public static String fromBytesToBase64(byte[] dataBytes) {
    return Base64.getEncoder().encodeToString(dataBytes);
}

public static byte[] fromBase64ToBytes(String base64String) {
    return Base64.getDecoder().decode(base64String);
}

流程如下:

byte[] signature = getTheSignatureAfterSignOperation();
String signatureInBase64 = fromBytesToBase64(signature);
saveToTextfile(signatureInBase64, "anyFile.txt");

// Other side
String signatureBase64 = readTextFile("anyFile.txt");
byte[] originalSignature = fromBase64ToBytes(signatureBase64);
doVerficiation(originalSignature);