RSA签名验证的JAVA JDK实现方式

package com.crypt.my;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
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;

public class JavaMD5RSASign {
	
	private static String src = "i look";
	
	public static void main(String[] args) {
		MD5RSASign();
	}
	
	
	
	
	
	public static void MD5RSASign(){
		
		try {
			
			// 生成一对密钥
			KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");  //获取密钥生成器实例
			keyPairGenerator.initialize(512);  // 初始化长度
			KeyPair keyPair = keyPairGenerator.generateKeyPair();  
			RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();//生成公钥
			RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();  // 生成私钥
			
			//用私钥进行签名
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());  //私钥转换成pkcs8格式
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); // 用key工厂对象生成私钥
			Signature signature = Signature.getInstance("MD5withRSA");  //  md5 RSA签名对象
			signature.initSign(privateKey);  //初始化签名
			signature.update(src.getBytes());
			byte[] result = signature.sign();  //对消息进行签名
			System.out.println("签名结果:"+result);
			
			
			//用公钥进行验证
			X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
			PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
			signature.initVerify(publicKey);
			signature.update(src.getBytes());
			boolean verify = signature.verify(result);
			System.out.println("验证结果:"+verify);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Truelicense 是一个 Java 应用程序许可证管理库,它提供了一种安全,灵活和易于使用的方法来保护您的 Java 应用程序。 在 Truelicense 中使用 RSA,可以实现数字签名验证,从而确保许可证的完整性和真实性。下面是使用 Truelicense 实现 RSA 的基本步骤: 1. 生成 RSA 密钥对 使用 JDK 自带的 keytool 工具生成 RSA 密钥对,例如: ``` keytool -genkeypair -alias mykey -keyalg RSA -keysize 2048 -validity 365 -keystore mykeystore.jks ``` 2. 配置 Truelicense 在 Truelicense 的配置文件中,指定密钥库文件和密钥库密码,例如: ``` license.keyStoreType = JKS license.keyStorePath = /path/to/mykeystore.jks license.keyStorePassword = mykeystorepassword license.keyAlias = mykey license.keyPassword = mykeypassword ``` 3. 签名许可证 在许可证生成时,使用私钥对许可证进行数字签名,例如: ```java // 加载许可证模板 LicenseTemplate template = LicenseTemplate.newStandard() .subject("MyApp License") .issued(new Date()) .expires(new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000L)) .build(); // 生成许可证 License license = License.create(template, privateKey); // 输出许可证 System.out.println(license.toString()); ``` 4. 验证许可证 在应用程序启动时,使用公钥对许可证进行验证,例如: ```java // 加载许可证 License license = License.load(new ByteArrayInputStream(licenseData)); // 验证许可证 if (license.verify(publicKey)) { System.out.println("License is valid"); } else { System.out.println("License is invalid"); } ``` 以上是使用 Truelicense 实现 RSA 的基本步骤,您可以根据您的具体需求进行适当的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深入沟通_it6688668

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值