This question already has answers here:
How to sign string with private key
                                
                                    (4个答案)
                                
                        
                                3年前关闭。
            
                    
有人可以解释一下我如何使用私钥对字符串或字节数组进行签名吗?请。两个星期,我正在努力实现这一目标。
我有代码:

private static byte[] createSignature(byte[] file) {
    byte[] signature = null;

    try {
        java.security.KeyStore keyStoreFile = java.security.KeyStore
                .getInstance("PKCS12");
        keyStoreFile.load(new FileInputStream("keyStore.pfx"),
                "password".toCharArray());

        // nuskaitomas privatus raktas iš failo
        PrivateKey privateKey = (PrivateKey) keyStoreFile.getKey(
                "alais", "password".toCharArray());

        Signature dsa = Signature.getInstance(SHA1withRSA);
        dsa.initSign(privateKey);
        dsa.update(file, 0, file.length);
        signature = dsa.sign();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return signature;
}


但这只是签名。

最佳答案

更改退货签名;为此:

byte[] encryptedByteValue = Base64.encodeBase64(signature);
return new String(encryptedByteValue, "UTF-8");

10-05 18:07
查看更多