JS-RSA加密解密
程序员文章站
2022-03-04 15:38:09
...
在上一篇文章《Java使用RSA加密解密签名及校验》中,用java实现加密解密,但是在实际应用中,如前端页面用户输入的密码传输给后台服务前,需加密,也就是公钥加密,私钥解密。
首先把《Java使用RSA加密解密签名及校验》中稍加修改,把公私钥写在代码中来,如下所示:
package com.bijian.study; import org.apache.commons.codec.binary.Base64; public class MainTest { public static void main(String[] args) throws Exception { System.out.println("--------------公钥加密私钥解密过程-------------------"); String plainText="ihep_公钥加密私钥解密"; //公钥加密过程 // byte[] cipherData=RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(RSAEncrypt.loadPublicKeyByFile(filepath)), plainText.getBytes()); String pubKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3SBa9KmzRVhdysc6gzSouxaH+mGl5xhIVu8APu+O3bkY4wlinlc6O4ZDGHS0kJHY1/Kn9D1xjzpnk7pjLORPvpgZmcuYF5BJdQLtrAxILwbpy42dnRoPs7QRSiG2sKcjsnKoRSszRIr70lFOF05R9Utumay5HFOWFiQaqhHtwp3SfvT250bRo8NKB5JtCbsKQDFsWNPcV7cyq/CKXibGjeelxTdWR7XBIeBhjMTWYO8XBhzlJA8MdGvDw+Eb+TiF/74YqhoDAriUKh36iqObPKFkgZ4uOcfvoe15NxJi6+JFahtR3FwvxukYWBlTgPghOaYaI7YX3VTAvBoRzUM92wIDAQAB"; byte[] cipherData=RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(pubKey), plainText.getBytes()); String cipher=Base64.encodeBase64String(cipherData); //私钥解密过程 // byte[] res=RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(RSAEncrypt.loadPrivateKeyByFile(filepath)), Base64.decodeBase64(cipher)); String priKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDdIFr0qbNFWF3KxzqDNKi7Fof6YaXnGEhW7wA+747duRjjCWKeVzo7hkMYdLSQkdj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qoaiqZexIOTam204O+9tqhtufeZQhTbrjCCy2hvVOh2ZEM5oRhu7CLEnLHlsFBUMRWYGT6dfrRoZMA3STVEkCgYEAw6SKdiX6vIEJ63HWFiL5DcV4KBaKd0pwy5cC6FdI7HHcK+B3Y87EJXHYyhHqPSyx5rZd3uGJSOtg0V2JHqvWba4PYBRabfsTBBmubIcijCZxr/WMzh83SFd1XR0eWE8KYRvy27U+n6dEjW/xlmG00/9GOY4wlPMxHoB6lEr93HkCgYBovoZekXRiiYjVXfDDxh5RgKXXbpESuW+5M/NoRje07uj9KwXiCagvyr71Ahx0m626ysRATSDKJ9xR6y28hmuD0e8ju29jtsR/r66vetfb5KdpmQ2CbsT73OLS0gwicjPEEZj3D0w1bc5Hx6Kurjh2Q9Dx4RqAErhqCFkOO3sWGw=="; byte[] res=RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(priKey), Base64.decodeBase64(cipher)); String restr=new String(res); System.out.println("原文:"+plainText); System.out.println("加密:"+cipher); System.out.println("解密:"+restr); System.out.println(); } }
运行结果如下所示:
--------------公钥加密私钥解密过程------------------- 原文:ihep_公钥加密私钥解密 加密:R7HZzg6aOwuGEITA4P8s7HE/RZq/W1tik4CT/5Nf+GqwuJJC7n2CJxgL72TNwXrTDr0Jku9AyRZ2GoD5mwn7DLhAANVv0O0SMAGGRX+3rg4owv3AdyjjZhV1GjQdeClHrLq/MdTbCsvBCNo4vf6sjwEpuSlqTHqr+ioP+mIqz/r3dQwT86tu4mDJ61h5M51c74M25Q648Eh2O8AzvHgVn5SHaQgjVUZqZJl5zG4WdiPwuTKZEkEnp+4ie/uW1uyv/4IkfmF9B2rEP5KJOdbIXEw5lxZeGs1nGZeUqdh5Is6wVWUHnNDOwpRk6BDqcjxH7cye3PhqpgtyO7BwLm7RIg== 解密:ihep_公钥加密私钥解密
我从网上找到JS-RSA的代码地址:https://github.com/openstack/xstatic-jsencrypt/blob/master/xstatic/pkg/jsencrypt/data/jsencrypt.js,使用方法如下:
首先引入jsencrypt.js,然后获取RSA的公钥和私钥。 var encrypt=new JSEncrypt(); encrypt.setPublicKey(RSA的公钥); encrypt.encrypt(加密的内容); 这样加密就成功了,然后解密也很简单。 encrypt.setPrivateKey(RSA的私钥); encrypt.decrypt(解密的内容);
将jsencrypt.js拷贝下来,然后F12在浏览器中运行如下:
当然,我想进一步验证JS-RSA用公钥加密得到加密串,用java来解密,看是否OK,于是把上面JS-RSA生成密文拷贝到java代码中,如下所示:
package com.bijian.study; import org.apache.commons.codec.binary.Base64; public class MainTest { public static void main(String[] args) throws Exception { String cipher = "sx2Rwuokxwm6UnnsEjMBUCAee5Lc7lg8K9XU1EYzXmEuBvhyyNxxBwsl25dFridOeYhxuVuMbxIqK1xhroyzb0d+tdFrAalQ6DK+OMaUIHjKfP1v3JIVJyTVE8BdLoTd+982PwPbe0TeKfCq5HYSjqb+5AWv5dYyVyFha4v373+HmWQxG+nTFN2lnlnPBbG0cWO+ykdmtfKRtAyhxJ5uYnWYhFzbbDfExmTMi4rwkJOWH5YmuxRRb6v1o6x32+Fr+WgtCXq6Nkq58epSzz80GPGn4BfOd4mj9wxs62GWj8O6sHm+yBgfNOB+rPE82PGcNKkL7eHoXpHGlY51KTKE2A=="; String priKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDdIFr0qbNFWF3KxzqDNKi7Fof6YaXnGEhW7wA+747duRjjCWKeVzo7hkMYdLSQkdjX8qf0PXGPOmeTumMs5E++mBmZy5gXkEl1Au2sDEgvBunLjZ2dGg+ztBFKIbawpyOycqhFKzNEivvSUU4XTlH1S26ZrLkcU5YWJBqqEe3CndJ+9PbnRtGjw0oHkm0JuwpAMWxY09xXtzKr8IpeJsaN56XFN1ZHtcEh4GGMxNZg7xcGHOUkDwx0a8PD4Rv5OIX/vhiqGgMCuJQqHfqKo5s8oWSBni45x++h7Xk3EmLr4kVqG1HcXC/G6RhYGVOA+CE5phojthfdVMC8GhHNQz3bAgMBAAECggEBAL+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5M/NoRje07uj9KwXiCagvyr71Ahx0m626ysRATSDKJ9xR6y28hmuD0e8ju29jtsR/r66vetfb5KdpmQ2CbsT73OLS0gwicjPEEZj3D0w1bc5Hx6Kurjh2Q9Dx4RqAErhqCFkOO3sWGw=="; byte[] res=RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(priKey), Base64.decodeBase64(cipher)); String restr=new String(res); System.out.println("解密:"+restr); System.out.println(); } }
运行结果如下所示:
这说明JS-RSA用公钥加密得到加密串,用java来解密,完全是OK的。
上一篇: java加解密及加解签
下一篇: 纯java实现AES加解密