js Java rsa 加密解密

https://github.com/travist/jsencrypt


通过测试案例:

http://yajiao.iteye.com/blog/1772835

问题:1.getJsPublicKey的JCERSAPublicKey要改成BCRSAPublicKey

  2.decodeJsValue里注释后面的 // 标志位为0之后的是输入的有效字节 实际上把Cipher.getInstance("RSA","BC"); 换成Cipher.getInstance("RSA/None/PKCS1Padding", "BC");  就不用再特殊处理输出

  3.http://www.justabug.net/door-1-rsa/ 写到:

我们读取到公钥后,需要用到RSAPublicKey的getModulus()和getPublicExponent()方法取得公钥的e(Exponent)和n(Modulus)给到前端页面,前端可以用getparameter等方法接收,或者在页面初始化时用ajax请求。

1String Modulus=RSAPublicKey.getModulus().toString(16);
2String Exponent=RSAPublicKey.getPublicExponent().toString(16);

这里需要toString(16)把他转为16进制,供前端使用

在此案例里配合使用的js版本,给的是geModules().toString(16),而不是直接的RSAPublicKey.getEncoded()

 5.keyPairGen.initialize(KEY_SIZE, new SecureRandom()) 给定seed,基本能固定公钥私钥不变,方便测试结果


http://wlh.iteye.com/blog/134796 基本与前一种方法一样,这里要吐槽下后端,跟我说RSAPublicKey是一个复杂的对象,怎么给公钥字符串给我,我。。。就不能好好看看文档接口嚒,明明有getEncode(),byte[] 转字符串也不会,我。。。


http://sunxboy.iteye.com/blog/209156 这个版本的js跟第一个版本差不多一样,用的是getModules()


https://www.cnblogs.com/woodk/p/5918661.html  这个版本的js用的JSEncrypt,给的公钥是New String(Base64.encodeBase64(publickey.getEncoded)),因为这个版本的js里面解出n和e是先base64.decode的,返回的encrypt结果也是hex2b64过,但是我看到解出的n和之前的版本的n一样,然后用去掉hex2b64的encrypt结果塞到第一个案例去测试,始终还是没有正确结果,生成的代码比对是一样,不知道问题出在哪里。。。

http://blog.csdn.net/defonds/article/details/42775183 这篇文章也可以看看

内网这个登陆的sign验证跳过了,希望跟平台能对起来吧,估计悬。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA加密和解密过程中需要对数据进行分段操作,以保证数据的安全性和加密效率。下面是JSJava两种语言的RSA分段加解密示例代码: JS代码: ```javascript // RSA加密 function rsaEncrypt(text, publicKey) { const buffer = Buffer.from(text); const blockSize = publicKey.keySize / 8 - 11; // 1024位密钥,每段最大长度为117 let result = ""; for (let i = 0; i < buffer.length; i += blockSize) { const chunk = buffer.slice(i, i + blockSize); const encrypted = publicKey.encrypt(chunk); result += encrypted.toString("base64"); } return result; } // RSA解密 function rsaDecrypt(text, privateKey) { const buffer = Buffer.from(text, "base64"); const blockSize = privateKey.keySize / 8; // 1024位密钥,每段最大长度为128 let result = ""; for (let i = 0; i < buffer.length; i += blockSize) { const chunk = buffer.slice(i, i + blockSize); const decrypted = privateKey.decrypt(chunk); result += decrypted.toString(); } return result; } ``` Java代码: ```java // RSA加密 public static String rsaEncrypt(String text, PublicKey publicKey) throws Exception { byte[] bytes = text.getBytes("UTF-8"); int blockSize = publicKey.getModulus().bitLength() / 8 - 11; // 1024位密钥,每段最大长度为117 ByteArrayOutputStream out = new ByteArrayOutputStream(); int i = 0; while (i < bytes.length) { int length = Math.min(blockSize, bytes.length - i); byte[] chunk = Arrays.copyOfRange(bytes, i, i + length); byte[] encrypted = rsaEncrypt(chunk, publicKey); out.write(encrypted); i += blockSize; } return Base64.getEncoder().encodeToString(out.toByteArray()); } private static byte[] rsaEncrypt(byte[] data, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); } // RSA解密 public static String rsaDecrypt(String text, PrivateKey privateKey) throws Exception { byte[] bytes = Base64.getDecoder().decode(text); int blockSize = privateKey.getModulus().bitLength() / 8; // 1024位密钥,每段最大长度为128 ByteArrayOutputStream out = new ByteArrayOutputStream(); int i = 0; while (i < bytes.length) { int length = Math.min(blockSize, bytes.length - i); byte[] chunk = Arrays.copyOfRange(bytes, i, i + length); byte[] decrypted = rsaDecrypt(chunk, privateKey); out.write(decrypted); i += blockSize; } return new String(out.toByteArray(), "UTF-8"); } private static byte[] rsaDecrypt(byte[] data, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } ``` 以上代码均是用的是PKCS#1 v1.5填充方式,如果需要使用其他填充方式,需要相应地修改代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值