How to sign string with private key

后端 未结 5 2062
别那么骄傲
别那么骄傲 2020-12-04 18:02

How can I get the signature of a string using SHA1withRSA if I already have the Private Key as byte[] or String?

5条回答
  •  眼角桃花
    2020-12-04 18:52

    public static String sign(String samlResponseString, String keystoreFile, String keyStorePassword, String privateKeyPassword, String alias)
                throws NoSuchAlgorithmException, UnsupportedEncodingException,
                InvalidKeyException, SignatureException {
            PrivateKey pkey=getPrivateKey(  keystoreFile,   keyStorePassword, privateKeyPassword,   alias);
            String signedString = null;
            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initSign(pkey);
            signature.update(samlResponseString.getBytes());
            byte[] signatureBytes = signature.sign();
            byte[] encryptedByteValue = Base64.encodeBase64(signatureBytes);
            signedString = new String(encryptedByteValue, "UTF-8");
            System.out.println(signedString);
            return signedString;
        }
    

提交回复
热议问题