RSA前台js加密后台Java解密

RSA前台使用js加密,后台使用Java解密时遇到解密字节超过128byte,但是代码经过多次检查没有发现错误,错误有可能是:
观察前台传递过来的密文,和前台加密后,没有传递的密文对比,你会发现,两个的长度不一样,他们不一样的原因是:
miyao1 = miyao1.replaceAll("%2F","/");
miyao1 = miyao1.replaceAll("%2B","+");
miyao1 = miyao1.replaceAll("%3D","=");
其中的%2F和/是互换的关系,
%2B和+是互换的关系,
%3D和=是互换的关系。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA是一种非对称加密算法,它可以用于对文件进行加密解密。在Java中,可以使用Java加密库来实现RSA加密解密。下面是一个简单的示例代码: ```java import java.io.*; import java.security.*; import javax.crypto.*; public class RSAEncryption { private static final String PUBLIC_KEY_FILE = "public.key"; private static final String PRIVATE_KEY_FILE = "private.key"; public static void main(String[] args) throws Exception { // 生成密钥对 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); SecureRandom random = new SecureRandom(); keyGen.initialize(1024, random); KeyPair keyPair = keyGen.generateKeyPair(); // 保存公钥和私钥到文件 saveKeyToFile(keyPair.getPublic(), PUBLIC_KEY_FILE); saveKeyToFile(keyPair.getPrivate(), PRIVATE_KEY_FILE); // 加密文件 encryptFile("plaintext.txt", "ciphertext.txt", keyPair.getPublic()); // 解密文件 decryptFile("ciphertext.txt", "decrypted.txt", keyPair.getPrivate()); } private static void saveKeyToFile(Key key, String fileName) throws IOException { byte[] keyBytes = key.getEncoded(); FileOutputStream fos = new FileOutputStream(fileName); fos.write(keyBytes); fos.close(); } private static void encryptFile(String inputFile, String outputFile, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); byte[] buffer = new byte[100]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { byte[] output = cipher.update(buffer, 0, bytesRead); if (output != null) { fos.write(output); } } byte[] output = cipher.doFinal(); if (output != null) { fos.write(output); } fis.close(); fos.flush(); fos.close(); } private static void decryptFile(String inputFile, String outputFile, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); byte[] buffer = new byte[128]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { byte[] output = cipher.update(buffer, 0, bytesRead); if (output != null) { fos.write(output); } } byte[] output = cipher.doFinal(); if (output != null) { fos.write(output); } fis.close(); fos.flush(); fos.close(); } } ``` 上面的代码中,首先生成密钥对,然后保存公钥和私钥到文件中。接着,使用公钥对明文文件进行加密,将密文保存到文件中。最后,使用私钥对密文文件进行解密,将明文保存到文件中。 需要注意的是,RSA算法只能加密比密钥长度小的数据,因此在加密大文件时,需要将文件分块加密,并将每个加密块保存到文件中。在解密时,将每个加密块读取出来,进行解密,然后将解密结果拼接起来即可。 另外,由于RSA算法的加密解密速度比较慢,因此在加密大文件时,可能需要使用其他加密算法,如AES算法,来加密文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值