教学服务系统

 找回密码
 立即注册
搜索
查看: 615|回复: 0

信息计算2019陆慧明

[复制链接]

9

主题

18

帖子

77

积分

注册会员

Rank: 2

积分
77
发表于 2022-5-21 21:55:54 | 显示全部楼层 |阅读模式
  1. package test;

  2. import java.io.*;
  3. import java.security.*;

  4. import javax.crypto.Cipher;
  5. import javax.crypto.CipherInputStream;
  6. import javax.crypto.CipherOutputStream;
  7. import javax.crypto.KeyGenerator;

  8. public class DesUtil {
  9.         private static String keyfileName = "DesKey.xml";

  10.         public static void decrypt(String file, String dest) throws Exception {
  11.                 Cipher cipher = Cipher.getInstance("DES");
  12.                 cipher.init(Cipher.DECRYPT_MODE, getKey());
  13.                 InputStream is = new FileInputStream(file);
  14.                 OutputStream out = new FileOutputStream(dest);
  15.                 CipherOutputStream cos = new CipherOutputStream(out, cipher);
  16.                 byte[] buffer = new byte[1024];
  17.                 int r;
  18.                 while ((r = is.read(buffer)) >= 0) {
  19.                         cos.write(buffer, 0, r);
  20.                 }
  21.                 cos.close();
  22.                 is.close();
  23.                 out.close();
  24.         }

  25.         public static void encrypt(String file, String destFile) throws Exception {
  26.                 Cipher cipher = Cipher.getInstance("DES");
  27.                 cipher.init(Cipher.ENCRYPT_MODE, getKey());
  28.                 InputStream is = new FileInputStream(file);
  29.                 OutputStream out = new FileOutputStream(destFile);
  30.                 CipherInputStream cis = new CipherInputStream(is, cipher);
  31.                 byte[] buffer = new byte[1024];
  32.                 int r;
  33.                 while ((r = cis.read(buffer)) > 0) {
  34.                         out.write(buffer, 0, r);
  35.                 }
  36.                 cis.close();
  37.                 is.close();
  38.                 out.close();
  39.         }

  40.         private static Key getKey() {
  41.                 Key kp = null;
  42.                 try {
  43.                         String fileName = keyfileName;
  44.                         InputStream is = new FileInputStream(fileName);
  45.                         ObjectInputStream oos = new ObjectInputStream(is);
  46.                         kp = (Key) oos.readObject();
  47.                         oos.close();
  48.                 } catch (Exception e) {
  49.                         e.printStackTrace();
  50.                 }
  51.                 return kp;
  52.         }

  53.         public static void main(String[] args) throws Exception {
  54.                 DesUtil.saveDesKey();
  55.                 DesUtil.encrypt("desinput.txt", "desoutput.txt");
  56.                 DesUtil.decrypt("desoutput.txt", "desinput2.txt");
  57.         }

  58.         public static void saveDesKey() {
  59.                 try {
  60.                         SecureRandom sr = new SecureRandom();
  61.                         KeyGenerator kg = KeyGenerator.getInstance("DES");
  62.                         kg.init(sr);
  63.                         FileOutputStream fos = new FileOutputStream(keyfileName);
  64.                         ObjectOutputStream oos = new ObjectOutputStream(fos);
  65.                         Key key = kg.generateKey();
  66.                         oos.writeObject(key);
  67.                         oos.close();
  68.                 } catch (Exception e) {
  69.                         e.printStackTrace();
  70.                 }
  71.         }
  72. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 07:52 , Processed in 0.016333 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表