数据安全管理:RSA加密算法,签名验签流程详解
程序员文章站
2023-11-09 16:24:40
本文源码: "GitHub·点这里" || "GitEE·点这里" 一、RSA算法简介 1、加密解密 RSA加密是一种非对称加密,在公开密钥加密和电子商业中RSA被广泛使用。可以在不直接传递密钥的情况下,完成加解密操作。这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险。是由一对密钥来进 ......
本文源码:github·点这里 || gitee·点这里
一、rsa算法简介
1、加密解密
rsa加密是一种非对称加密,在公开密钥加密和电子商业中rsa被广泛使用。可以在不直接传递密钥的情况下,完成加解密操作。这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险。是由一对密钥来进行加解密的过程,分别称为公钥和私钥。该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。
2、签名验签
数字签名就是信息的来源添加一段无法被伪造的加密字符串,这段数字串作为对信息的来源真实性的一个有效证明。这个过程称为签名和验签。
二、场景描述
- 消息发送方:甲方,持有公钥
- 消息接收方:乙方,持有私钥
1、加密解密过程
(1)、乙方生成一对密钥即公钥和私钥,私钥不公开,乙方自己持有,公钥为公开,甲方持有。
(2)、乙方收到甲方加密的消息,使用私钥对消息进行解密,获取明文。
2、签名验签过程
(1)、乙方收到消息后,需要回复甲方,用私钥对回复消息签名,并将消息明文和消息签名回复甲方。
(2)、甲方收到消息后,使用公钥进行验签,如果验签结果是正确的,则证明消息是乙方回复的。
三、源代码实现
1、密钥字符串获取
- 代码生成
private static hashmap<string, string> getthekeys() { hashmap<string, string> keypairmap = new hashmap<string, string>(); keypairgenerator keypairgen = null; try { keypairgen = keypairgenerator.getinstance("rsa"); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } // 密钥大小:1024 位 keypairgen.initialize(1024); keypair keypair = keypairgen.generatekeypair(); string publickey = printbase64binary(keypair.getpublic().getencoded()); string privatekey = printbase64binary(keypair.getprivate().getencoded()); keypairmap.put("publickey", publickey); keypairmap.put("privatekey", privatekey); return keypairmap ; }
- 读取文件
文件位置
public static final string pub_key = "rsakey/public.key" ; public static final string pri_key = "rsakey/private.key" ;
文件加载
public static string getkey (string keyplace) throws exception { bufferedreader br= null; try { br= new bufferedreader(new inputstreamreader(rsacryptutil.class.getclassloader(). getresourceasstream(keyplace))); string readline= null; stringbuilder keyvalue = new stringbuilder(); while((readline= br.readline())!=null){ if(!(readline.charat(0)=='-')){ keyvalue.append(readline); } } return keyvalue.tostring(); } catch (exception e) { throw new exception("rsa密钥读取错误",e) ; } finally{ if (br != null) { try { br.close(); } catch (exception e) { system.out.println("密钥读取流关闭异常"); } } } }
2、公钥和私钥
- 公钥字符串生成公钥
public static rsapublickey createpublickey(string publickeyvalue) throws exception { try { byte[] buffer = datatypeconverter.parsebase64binary(publickeyvalue); keyfactory keyfactory = keyfactory.getinstance("rsa"); x509encodedkeyspec keyspec = new x509encodedkeyspec(buffer); return (rsapublickey) keyfactory.generatepublic(keyspec); } catch (exception e) { throw new exception("公钥创建失败", e); } }
- 私钥字符串生成私钥
public static rsaprivatekey createprivatekey(string privatekeyvalue) throws exception { try { byte[] buffer = javax.xml.bind.datatypeconverter.parsebase64binary(privatekeyvalue); pkcs8encodedkeyspec keyspec = new pkcs8encodedkeyspec(buffer); keyfactory keyfactory = keyfactory.getinstance("rsa"); return (rsaprivatekey) keyfactory.generateprivate(keyspec); } catch (exception e) { throw new exception("私钥创建失败", e); } }
3、加密和解密
- 公钥加密
public static string encrypt(rsapublickey publickey, byte[] cleardata) throws exception { if (publickey == null) { throw new exception("加密公钥为空, 无法加密"); } try { cipher cipher = cipher.getinstance("rsa") ; cipher.init(cipher.encrypt_mode, publickey); byte[] output = cipher.dofinal(cleardata); return printbase64binary(output); } catch (exception e) { throw new exception("公钥加密失败",e); } }
- 私钥解密
public static string decrypt(rsaprivatekey privatekey, byte[] cipherdata) throws exception { if (privatekey == null) { throw new exception("解密私钥为空, 无法解密"); } try { cipher cipher = cipher.getinstance("rsa") ; cipher.init(cipher.decrypt_mode, privatekey); byte[] output = cipher.dofinal(cipherdata); return new string(output); } catch (badpaddingexception e) { throw new exception("私钥解密失败",e); } }
4、签名和验签
- 私钥签名
public static string sign (string signdata, privatekey privatekey) throws exception { byte[] keybytes = privatekey.getencoded(); pkcs8encodedkeyspec keyspec = new pkcs8encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance("rsa"); privatekey key = keyfactory.generateprivate(keyspec); signature signature = signature.getinstance("md5withrsa"); signature.initsign(key); signature.update(signdata.getbytes()); return printbase64binary(signature.sign()); }
- 公钥验签
public static boolean verify(string srcdata, publickey publickey, string sign) throws exception { byte[] keybytes = publickey.getencoded(); x509encodedkeyspec keyspec = new x509encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance("rsa"); publickey key = keyfactory.generatepublic(keyspec); signature signature = signature.getinstance("md5withrsa"); signature.initverify(key); signature.update(srcdata.getbytes()); return signature.verify(parsebase64binary(sign)); }
5、编码和解码
/** * 字节数组转字符 */ public static string printbase64binary(byte[] bytes) { return datatypeconverter.printbase64binary(bytes); } /** * 字符转字节数组 */ public static byte[] parsebase64binary(string value) { return datatypeconverter.parsebase64binary(value); }
6、测试代码块
- 密钥生成测试
public static void testcreatekey () throws exception { hashmap<string, string> map = rsacryptutil.getthekeys(); string privatekeystr=map.get("privatekey"); string publickeystr=map.get("publickey"); system.out.println("私钥:"+privatekeystr); system.out.println("公钥:"+publickeystr); //消息发送方 string origindata="cicada-smile"; system.out.println("原文:"+origindata); string encryptdata = rsacryptutil.encrypt(rsacryptutil.createpublickey(publickeystr), origindata.getbytes()); system.out.println("加密:"+encryptdata); //消息接收方 string decryptdata=rsacryptutil.decrypt(rsacryptutil.createprivatekey(privatekeystr), rsacryptutil.parsebase64binary(encryptdata)); system.out.println("解密:"+decryptdata); }
- 密钥读取测试
public static void testreadkey () throws exception { string value = getkey("rsakey/public.key"); system.out.println(value); string privatekeystr = getkey(rsacryptutil.pri_key) ; string publickeystr = getkey(rsacryptutil.pub_key) ; //消息发送方 string origindata="cicada-smile"; system.out.println("原文:"+origindata); string encryptdata = rsacryptutil.encrypt(rsacryptutil.createpublickey(publickeystr), origindata.getbytes()); system.out.println("加密:"+encryptdata); //消息接收方 string decryptdata=rsacryptutil.decrypt(rsacryptutil.createprivatekey(privatekeystr), rsacryptutil.parsebase64binary(encryptdata)); system.out.println("解密:"+decryptdata); }
- 签名验签测试
public static void testsignverify () throws exception { string signdata = "cicada-smile" ; string privatekeystr = getkey(rsacryptutil.pri_key) ; string publickeystr = getkey(rsacryptutil.pub_key) ; string signvalue = sign(signdata,rsacryptutil.createprivatekey(privatekeystr)) ; boolean flag = verify(signdata,rsacryptutil.createpublickey(publickeystr),signvalue); system.out.println("原文:"+signdata); system.out.println("签名:"+signvalue); system.out.println("验签:"+flag); }
四、源代码地址
github·地址 https://github.com/cicadasmile gitee·地址 https://gitee.com/cicadasmile