SHA256WithRSA签名算法⽀付⽹关回调签名⽅案

public static boolean validateSign(String data, String sign, String publicKey){
        try{
            Signature signature = Signature.getInstance("SHA256WithRSA"); 
            PublicKey localPublicKey = getPublicKeyFromX509("RSA", publicKey);
            signature.initVerify(localPublicKey);
            signature.update(data.getBytes());
            byte[] bytesSign = Base64.decode(sign);
            return signature.verify(bytesSign);
        }catch(Exception e){
            e.printStackTrace(); 
            return false;
        }
    }
    
    public static PublicKey getPublicKeyFromX509(String algorithm, String publicKey) throws Exception { 
        KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
        return keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(publicKey)));
    }

String token="dijoewjfiorenmginrtntigha4575";

String result="";

System.out.println("签名验证结果:"+validateSign(result,token,SQBConstant.publicKey));

你可能感兴趣的:(java开发,servlet,java,jvm,SHA256WithRSA)