是否可以在HEX上签名?

时间:2019-06-18 12:40:21

标签: java android encryption hex private-key

我正在尝试创建数字签名。当我在控制台上登录此签名时,我得到如下奇怪的字符:

1��N�U]*������gڇ��Xр��ӶhR���1��q

我的朋友正在后端检查此数字签名,只有他的签名是十六进制的。他的签名看起来像:

3BFC368C044C932FB62746765A84F3C3ABE7913CC8FDDB32DDF94607D99BE754D8761FAC1E9314B80E1E5EB14D936E137CB44954F245E1D38A6F4FDC80DB00B5D953DDF0A9E533FADD574F116DDA36AF517217753AA98F284BE946739641EE26933DC8E72B10CAA0D75984D5B1561A3B84815D1BFC9910F2316E0A0773F2CFE9F139D7C9A5D0CE45256055A63A80BDE6B5DF7F433AFE803E251E7F19964F93F008A7B7AA4A488BD4FB4968AE82D1D6DF6EBB2DA000F6F798808888D040EC84FB285095387EB8AA9397CB2FAC0248D2D5277386432D99E55AAAD5C3E79048C64713A695B575FD89D3B4218FA784F2116A80B359D4FF62F0391F1EFAC7968DAAA

是否可以在Java中以十六进制形式签名?

Android:

            KeyFactory kf = KeyFactory.getInstance("RSA");
            PrivateKey privKey = kf.generatePrivate(keySpec);
            System.out.println(privKey);
            Signature signer = Signature.getInstance("SHA256withRSA");
            signer.initSign(privKey);
            signer.update(hash.getBytes());
            byte[] signature = signer.sign();
            String encryptHash = new String(signature);

1 个答案:

答案 0 :(得分:1)

您有一个字节数组,需要将每个字节转换为它们的十六进制表示形式,您可以执行以下操作:

byte[] signature = signer.sign();
StringBuilder hexBuilder = new StringBuilder();
for(b: signature){
    hexBuilder.append(String.format("%02x", b));
}
String hexSignature = hexBuilder.toString();