JAVA_RSA_的加解密

RSA为非对称加密算法。

数字签名的过程:1、对明文数据进行HASH加密,不可逆;2、对加密后的数据再用RSA的私钥进行二次加密。

数字签名的验证过程:1、对明文数据进行HASH加密,不可逆;2、用RSA的公钥对数字签名后的数据进行解密;3、把1的结果和2的结果进行比较是否相等。

RSA加密的过程和解密的过程都需要三步:加/解密、分组、填充。这三部分每一步都可以选择各自的算法。例如:RSA/ECB/PKCS1Padding。

在这里RSA的公钥是用X509编码的。

在这里RSA的私钥使用PKCS8编码的。

 RAS算法数字签名漫画详解

1import java.security.Key; 2import java.security.KeyFactory; 3import java.security.KeyPair; 4import java.security.KeyPairGenerator; 5import java.security.PrivateKey; 6import java.security.PublicKey; 7import java.security.Signature; 8import java.security.spec.PKCS8EncodedKeySpec; 9import java.security.spec.X509EncodedKeySpec; 10import java.util.Base64; 11import java.util.HashMap; 12import java.util.Map; 13 14import javax.crypto.Cipher; 15 16public class Secret { 17 18 public static final String KEY_ALGORITHM = "RSA"; 19 public static final String SIGNATURE_ALGORITHM = "SHA256withRSA"; 20 private static final String PUBLIC_KEY = "PublicKey"; 21 private static final String PRIVATE_KEY = "PrivateKey"; 22 23 public static void main(String[] args) throws Exception{ 24 // TODO Auto-generated method stub 25 Map<String,Key> keyMap = initKeyPair(); 26 String publicKey = encryptBASE64(keyMap.get(PUBLIC_KEY).getEncoded()); 27 String privateKey = encryptBASE64(keyMap.get(PRIVATE_KEY).getEncoded()); 28 String data = "123456"; 29 byte[] miwen = encryptByPrivateKey(data.getBytes(), privateKey); 30 byte[] mingwen = decryptByPublicKey(miwen, publicKey); 31 String redata = new String(mingwen); 32 System.out.println(redata); 33 String data2 = "asdfgh"; 34 byte[] miwen2 = encryptByPublicKey(data2.getBytes(), publicKey); 35 byte[] mingwen2 = decryptByPrivateKey(miwen2, privateKey); 36 String redata2 = new String(mingwen2); 37 System.out.println(redata2); 38 String data3 = "678901"; 39 byte[] miwen3 = sign(data3.getBytes(), privateKey); 40 boolean flag = verify(data3.getBytes(), publicKey, miwen3); 41 System.out.println(flag); 42 43 44 } 45 //生成密匙对 46 public static Map<String,Key> initKeyPair() throws Exception{ 47 KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM); 48 kpg.initialize(1024); 49 KeyPair keyPair = kpg.generateKeyPair(); 50 PublicKey publicKey = keyPair.getPublic(); 51 PrivateKey privateKey = keyPair.getPrivate(); 52 Map<String,Key> keyMap = new HashMap<>(); 53 keyMap.put(PUBLIC_KEY, publicKey); 54 keyMap.put(PRIVATE_KEY, privateKey); 55 return keyMap; 56 } 57 //字节数组到公钥 58 public static PrivateKey strToPrivateKey(String str) throws Exception{ 59 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(decryptBASE64(str)); 60 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 61 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); 62 return privateKey; 63 } 64 //字节数组到私钥 65 public static PublicKey strToPublicKey(String str) throws Exception{ 66 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(decryptBASE64(str)); 67 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 68 PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); 69 return publicKey; 70 } 71 //字节数组到文件字符串 72 public static String encryptBASE64(byte[] bytes) { 73 return Base64.getEncoder().encodeToString(bytes); 74 } 75 //文件字符串到字节数组 76 public static byte[] decryptBASE64(String str) { 77 return Base64.getDecoder().decode(str); 78 } 79 //用公钥加密 80 public static byte[] encryptByPublicKey(byte[] data, String key) throws Exception{ 81 PublicKey publicKey = strToPublicKey(key); 82 Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); 83 //Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 84 //加解密算法/分組算法/填充算法 85 cipher.init(Cipher.ENCRYPT_MODE, publicKey); 86 return cipher.doFinal(data); 87 } 88 //用私钥加密 89 public static byte[] encryptByPrivateKey(byte[] data, String key) throws Exception{ 90 PrivateKey privateKey = strToPrivateKey(key); 91 Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); 92 //Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 93 //加解密算法/分組算法/填充算法 94 cipher.init(Cipher.ENCRYPT_MODE, privateKey); 95 return cipher.doFinal(data); 96 } 97 //用公钥解密 98 public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception { 99 PublicKey publicKey = strToPublicKey(key); 100 Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); 101 //Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 102 //加解密算法/分組算法/填充算法 103 cipher.init(Cipher.DECRYPT_MODE, publicKey); 104 return cipher.doFinal(data); 105 } 106 //用私钥解密 107 public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception { 108 PrivateKey privateKey = strToPrivateKey(key); 109 Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); 110 //Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 111 //加解密算法/分組算法/填充算法 112 cipher.init(Cipher.DECRYPT_MODE, privateKey); 113 return cipher.doFinal(data); 114 } 115 //对字符串进行数字签名 116 public static byte[] sign(byte[] data, String privateKey) throws Exception { 117 PrivateKey priKey = strToPrivateKey(privateKey); 118 Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); 119 signature.initSign(priKey); 120 signature.update(data); 121 return signature.sign(); 122 } 123 //对数字签名进行验证 124 public static boolean verify(byte[] data, String publicKey, byte[] sign) throws Exception { 125 PublicKey pubKey = strToPublicKey(publicKey); 126 Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); 127 signature.initVerify(pubKey); 128 signature.update(data); 129 return signature.verify(sign); 130 } 131}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

sourceTree 添加 ssh key 方法

1.使用git客户的生成公私钥:id\rsa、id\rsa.pub1.1设置Git的username和email:$gitconfigglobaluser.name"xxx"$gitconfig\globaluser.email"xxx.mail@xxx.com"1.2.生成SSH密钥过程:1.2.1.检查是不是已经存在密钥(

RSA加密、解密、签名、验签的原理及方法

一、RSA加密简介  RSA加密是一种非对称加密。可以在不直接传递密钥的情况下,完成解密。这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险。是由一对密钥来进行加解密的过程,分别称为公钥和私钥。两者之间有数学相关,该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。通常个人保存私钥,公钥是公开的(可能同时多人持有)。

RSA —— 典型非对称加密算法

RSA——JAVA代码(toc_0)RSA——加密过程图解(toc_1)(图1)构建RSA算法密匙对(toc_2)(图2)甲方向乙方发送RSA加密数据(toc_3)(图3)乙方向甲方发送RSA加密数据(toc_4)RSA——简述(

JAVA_RSA_的加解密 - HelloWorld