数字签名API使用
来源:互联网 发布:马斯克人工智能 编辑:程序博客网 时间:2024/04/30 23:40
直接上代码:
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class TestSingature {
public static KeyPair getKeyPair() throws Exception{
KeyPairGenerator keyPairGenerator = KeyPairGenerator
.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
public static String getPublicKey(KeyPair keyPair){
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return byte2base64(bytes);
}
public static String getPrivateKey(KeyPair keyPair){
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return byte2base64(bytes);
}
public static PublicKey string2PublicKey(String pubStr)
throws Exception{
byte[] keyBytes = base642byte(pubStr);
/**
* This class represents the ASN.1 encoding of a public key,
* encoded according to the ASN.1 type
*/
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public static PrivateKey string2PrivateKey(String priStr)
throws Exception{
byte[] keyBytes = base642byte(priStr);
/**
* This class represents the ASN.1 encoding of a private key,
* encoded according to the ASN.1 type
*/
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
private static String byte2base64(byte[] bytes){
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(bytes);
}
private static byte[] base642byte(String base64) throws IOException{
BASE64Decoder base64Decoder = new BASE64Decoder();
return base64Decoder.decodeBuffer(base64);
}
private static byte[] sign(byte[] content, PrivateKey privateKey ) throws Exception{
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] bytes = md.digest(content);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encryptBytes = cipher.doFinal(bytes);
return encryptBytes;
}
private static boolean verify(byte[] content, byte[] sign, PublicKey publicKey) throws Exception{
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] bytes = md.digest(content);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] decryptBytes = cipher.doFinal(sign);
if(byte2base64(decryptBytes).equals(byte2base64(bytes))){
return true;
}else{
return false;
}
}
public static void main(String[] args) throws Exception {
KeyPair keyPair = getKeyPair();
String publicKey = getPublicKey(keyPair);
String privateKey = getPrivateKey(keyPair);
PublicKey publics = string2PublicKey(publicKey);
PrivateKey privates = string2PrivateKey(privateKey);
String a = "hello,i am chenkangxian,good night!";
byte[] sign = sign(a.getBytes(),privates);
boolean verify = verify(a.getBytes(),sign,publics);
System.out.println(verify);
}
0 0
- 数字签名API使用
- 软件数字签名向导API
- api接口的数字签名
- vc6中使用数字签名
- C#如何使用数字签名?
- 数字签名的编程使用
- 数字签名的编程使用
- php CURL的api应用和数字签名
- android 数字签名使用 -- 参考网站
- 【Office】使用数字证书进行数字签名
- 数字签名
- 数字签名
- 数字签名
- 数字签名
- 数字签名
- 数字签名
- 数字签名
- 数字签名
- weblogic10.3加载status2报错 unable to attain an URLClassLoader
- 关于Android Studio修改包名的问题
- SPOJ MBEEWALK Bee Walk(记忆化搜索+直角坐标系转化为斜坐标系)
- ABAP中内表的几个常用的清空内表语句,及它们之间的区别
- std:ios::sync_with_stdio(false);
- 数字签名API使用
- 1.0Java缓存-Ehcache配置详解及CacheManager使用
- mysql source 命令导入大的sql文件的方法
- 分享扫描文字识别软件的操作方法
- [python]python内置容器的学习
- [CQOI2017] 蒟蒻记
- java 开发为什么选择freemarker 放弃jsp
- ftp connect: No route to host 解决方案
- 调用阿里云身份证识别接口(使用appcode方式)