0

我正在编写一个针对 Android 25 的应用程序,并且我正在尝试String使用我的公钥对我的 HTTP 查询参数进行签名。java.security.SignatureException: object not initialized for signing但是,调用此方法时出现以下异常。

你认为我在这里缺少什么?我的代码中引发异常的相关行用注释标记//EXCEPTION IS THROWN HERE IN THE FOLLOWING LINE

 private static String signSHA256RSA(String httpQueryParameterString, String strPublicKey) throws Exception {

    String input = httpQueryParameterString;

    if(input == null || input.isEmpty() || strPublicKey == null || strPublicKey.isEmpty()){
        return "";
    }

    String realPublicKey = strPublicKey.
            replaceAll("-----END PUBLIC KEY-----", "").
            replaceAll("-----BEGIN PUBLIC KEY-----", "").
            replaceAll("\n", "");

    byte[] b1 = Base64.decode(realPublicKey, Base64.DEFAULT);
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(b1);
    KeyFactory kf = KeyFactory.getInstance("RSA"); 
    RSAPublicKey rsaPubKey = (RSAPublicKey) kf.generatePublic(keySpec);

    Signature publicSignature = Signature.getInstance("SHA256withRSA");
    publicSignature.initVerify(rsaPubKey);

    //EXCEPTION IS THROWN HERE IN THE FOLLOWING LINE
    byte[] s = publicSignature.sign();

    return Base64.encodeToString(s, Base64.DEFAULT);
}
4

0 回答 0