为了账号安全,请及时绑定邮箱和手机立即绑定

封装签名和验签两个方法验证不成功

// 签名方法

public static String jdkRSASign(String src) {

String result = null;// 签名结果

try {

// 1.初始化秘钥

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

keyPairGenerator.initialize(512);

KeyPair keyPair = keyPairGenerator.generateKeyPair();

RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();// 私钥


// 2.执行签名

PKCS8EncodedKeySpec pkcS8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PrivateKey privateKey = keyFactory.generatePrivate(pkcS8EncodedKeySpec);

Signature signature = Signature.getInstance("MD5withRSA");

signature.initSign(privateKey);

signature.update(src.getBytes("UTF-8"));

result = new String(signature.sign(),"UTF-8");

} catch (Exception e) {

e.printStackTrace();

}


return result;

}


// 验签名方法

public static boolean jdkRSACheck(String src, String sign) {

boolean result = false;

try {

// 1.初始化秘钥

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

keyPairGenerator.initialize(512);

KeyPair keyPair = keyPairGenerator.generateKeyPair();

RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();// 公钥


// 2.验证签名

X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

Signature signature = Signature.getInstance("MD5withRSA");

signature.initVerify(publicKey);

signature.update(src.getBytes("UTF-8"));

result = signature.verify(sign.getBytes("UTF-8"));

} catch (Exception e) {

e.printStackTrace();

}


return result;

}


正在回答

1 回答

package com.dhs.test;


import java.security.KeyFactory;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.NoSuchAlgorithmException;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.Signature;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

import org.bouncycastle.util.encoders.Hex;


public class RSATest {

public static final String SRC_STRING = "fdsfsfsfsfd";

public static void main(String[] args) {

try {

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

keyPairGenerator.initialize(512);

KeyPair keyPair = keyPairGenerator.generateKeyPair();

RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();// 私钥

RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();// 公钥

String sign = jdkRSASign(rsaPrivateKey, SRC_STRING);

System.out.println("sign:"+sign);

boolean bool = jdkRSACheck(rsaPublicKey, SRC_STRING, sign);

System.out.println(bool);

} catch (NoSuchAlgorithmException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 签名方法

public static String jdkRSASign(RSAPrivateKey rsaPrivateKey, String src) {

String result = null;// 签名结果

try {

// 2.执行签名

PKCS8EncodedKeySpec pkcS8EncodedKeySpec = new PKCS8EncodedKeySpec(

rsaPrivateKey.getEncoded());

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PrivateKey privateKey = keyFactory

.generatePrivate(pkcS8EncodedKeySpec);

Signature signature = Signature.getInstance("MD5withRSA");

signature.initSign(privateKey);

signature.update(src.getBytes("UTF-8"));

result = Hex.toHexString(signature.sign());

} catch (Exception e) {

e.printStackTrace();

}


return result;

}


// 验签名方法

public static boolean jdkRSACheck(RSAPublicKey rsaPublicKey, String src, String sign) {

boolean result = false;

try {

// 2.验证签名

X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(

rsaPublicKey.getEncoded());

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

Signature signature = Signature.getInstance("MD5withRSA");

signature.initVerify(publicKey);

signature.update(src.getBytes("UTF-8"));

result = signature.verify(Hex.decode(sign));

} catch (Exception e) {

e.printStackTrace();

}


return result;

}

}


1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

封装签名和验签两个方法验证不成功

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信