SHA256withRSA签名验签 & BASE64加解密

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import org.apache.commons.codec.binary.Base64;

public class SignUtil {
	
	private static final String ENCODING = "UTF-8";
	private static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
	
	/**
	 * SHA256WithRSA签名
	 * @param data
	 * @param privateKey
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 * @throws InvalidKeyException
	 * @throws SignatureException
	 * @throws UnsupportedEncodingException
	 */
	public static byte[] sign256(String data, PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, 
	  																		 SignatureException, UnsupportedEncodingException {
	    
		Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
	       
		signature.initSign(privateKey);
	       
		signature.update(data.getBytes(ENCODING));
	         
		return signature.sign();
	}
	
	public static boolean verify256(String data, byte[] sign, PublicKey publicKey){
		if(data == null || sign == null || publicKey == null){
			return false;
		}
		
		try {
			Signature signetcheck = Signature.getInstance(SIGNATURE_ALGORITHM);
			signetcheck.initVerify(publicKey);
			signetcheck.update(data.getBytes("UTF-8"));
			return signetcheck.verify(sign);
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 二进制数据编码为BASE64字符串 
	 * @param data
	 * @return
	 */
	public static String encodeBase64(byte[] bytes){
		return new String(Base64.encodeBase64(bytes));
	}
	
	/**
	 * BASE64解码
	 * @param bytes
	 * @return
	 */
	public static byte[] decodeBase64(byte[] bytes) { 
		byte[] result = null;
		try {
			result = Base64.decodeBase64(bytes);
		} catch (Exception e) {
			return null;
		}
        return result;  
    }
}

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是SHA256withRSA签名验签Java代码示例: **签名代码:** ``` import java.security.*; import java.util.Base64; public class RSASignatureExample { public static void main(String[] args) throws Exception { String message = "This is a message to be signed."; // Generate key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair keyPair = keyGen.generateKeyPair(); // Create a signature object Signature signature = Signature.getInstance("SHA256withRSA"); // Initialize the signature object with the private key PrivateKey privateKey = keyPair.getPrivate(); signature.initSign(privateKey); // Update the signature object with the message signature.update(message.getBytes()); // Sign the message byte[] signatureBytes = signature.sign(); // Print the signature in Base64 encoding System.out.println("Signature: " + Base64.getEncoder().encodeToString(signatureBytes)); } } ``` **验签代码:** ``` import java.security.*; import java.util.Base64; public class RSASignatureExample { public static void main(String[] args) throws Exception { String message = "This is a message to be signed."; String signatureString = "PLl6yBb6IhOv0Jd3k1y/YlR9a3UOx0WJQy6RbbS/3W8mQ2ztcL5x+JShHl6O8gTlJn4wtNQ7Ggz0ZiPwq9SInQf/9d4eG4qSkZCzVgP9U9bQY7jwFVhWXfzqU6l5K8SxyA2pBw+PQs0N2dJQ2Xk9V3tXZP5h+UcMXwLQe/3K7uh2M="; // Generate key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair keyPair = keyGen.generateKeyPair(); // Create a signature object Signature signature = Signature.getInstance("SHA256withRSA"); // Initialize the signature object with the public key PublicKey publicKey = keyPair.getPublic(); signature.initVerify(publicKey); // Update the signature object with the message signature.update(message.getBytes()); // Verify the signature boolean verified = signature.verify(Base64.getDecoder().decode(signatureString)); // Print the verification result System.out.println("Signature verified: " + verified); } } ``` 以上代码示例中,我们使用SHA256withRSA算法对一个字符进行签名验签。在签名时,我们首先生成了一个RSA密钥对,然后使用私钥对消息进行签名,并打印出Base64编码签名结果。在验签时,我们使用同样的RSA密钥对和签名算法,使用公钥初始化签名对象,并使用该对象对消息进行验证,最终打印验证结果。需要注意的是,验签时需要将签名结果从Base64编码中解码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值