java处理加密文件---RSA加密和解密

public class RSA {
 public static void main(String []args)throws Exception
    {
  String ptext = "he";
  //获取公钥生成参数
  KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  kpg.initialize(1024);
  KeyPair keyPair = kpg.genKeyPair();

  RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  
  System.out.println("公共码"+publicKey.getModulus().toString());
  
  byte[] ptexts = ptext.getBytes("UTF8");
  BigInteger m = new BigInteger(ptexts);
  BigInteger c = m.modPow(publicKey.getPublicExponent(), publicKey.getModulus());
  System.out.println("密文"+c);
  
  //System.out.println("d:"+privateKey.getPrivateExponent());
  
  System.out.println("私密码"+privateKey.getModulus());
  
  BigInteger jm=c.modPow(privateKey.getPrivateExponent(), privateKey.getModulus());
  System.out.println("解码数据"+jm);
  
  byte[] mt =jm.toByteArray();
  
  System.out.print("解码结果");
  for(int i=0;i<mt.length;i++){
   System.out.print((char)mt[i]);
  }
  
    }
}

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值