教学服务系统

 找回密码
 立即注册
搜索
查看: 747|回复: 1

信息计算2019级2班16号吴盛盛

[复制链接]

8

主题

17

帖子

134

积分

注册会员

Rank: 2

积分
134
发表于 2022-4-5 16:51:04 | 显示全部楼层 |阅读模式
网课截图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

8

主题

17

帖子

134

积分

注册会员

Rank: 2

积分
134
 楼主| 发表于 2022-4-5 20:53:07 | 显示全部楼层
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.security.MessageDigest;
  4. import java.security.spec.AlgorithmParameterSpec;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.CipherInputStream;
  7. import javax.crypto.CipherOutputStream;
  8. import javax.crypto.SecretKey;
  9. import javax.crypto.spec.IvParameterSpec;
  10. import javax.crypto.spec.SecretKeySpec;
  11. import com.spire.doc.*;
  12. import java.io.IOException;
  13. class TestAES {
  14.         Cipher ecipher;
  15.         Cipher dcipher;
  16.         public TestAES() {
  17.                 try {
  18.                         SecretKeySpec skey = new SecretKeySpec(
  19.                                         "9f265d42ab3c66d8f50a3a2e793a30c2".getBytes(), "AES");
  20.                         this.setupCrypto(skey);
  21.                 } catch (Exception e) {
  22.                         e.printStackTrace();
  23.                 }
  24.         }
  25.         public TestAES(String key) {
  26.                 SecretKeySpec skey = new SecretKeySpec(getMD5(key), "AES");
  27.                 this.setupCrypto(skey);
  28.         }
  29.         private void setupCrypto(SecretKey key) {
  30.                 byte[] iv = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31.                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

  32.                 AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
  33.                 try {
  34.                         ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  35.                         dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

  36.                         // CBC requires an initialization vector
  37.                         ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  38.                         dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  39.                 } catch (Exception e) {
  40.                         e.printStackTrace();
  41.                 }
  42.         }
  43.         public void encrypt(InputStream in, OutputStream out) {
  44.                 byte[] buf = new byte[1024];
  45.                 try {
  46.                         out = new CipherOutputStream(out, ecipher);
  47.                         int numRead = 0;
  48.                         while ((numRead = in.read(buf)) >= 0) {
  49.                                 out.write(buf, 0, numRead);
  50.                         }
  51.                         out.close();
  52.                 } catch (java.io.IOException e) {
  53.                         e.printStackTrace();
  54.                 }
  55.         }
  56.         public String encrypt(String plaintext) {
  57.                 try {
  58.                         byte[] ciphertext = ecipher.doFinal(plaintext.getBytes("UTF-8"));
  59.                         return byteToHex(ciphertext);
  60.                 } catch (Exception e) {
  61.                         e.printStackTrace();
  62.                         return null;
  63.                 }

  64.         }
  65.         public void decrypt(InputStream in, OutputStream out) {
  66.                 try {
  67.                         byte[] buf = new byte[1024];
  68.                         in = new CipherInputStream(in, dcipher);

  69.                         // Read in the decrypted bytes and write the cleartext to out
  70.                         int numRead = 0;
  71.                         while ((numRead = in.read(buf)) >= 0) {
  72.                                 out.write(buf, 0, numRead);
  73.                         }
  74.                         out.close();
  75.                 } catch (java.io.IOException e) {
  76.                         e.printStackTrace();
  77.                 }
  78.         }
  79.         public String decrypt(String hexCipherText) {
  80.                 try {
  81.                         String plaintext = new String(
  82.                                         dcipher.doFinal(hexToByte(hexCipherText)), "UTF-8");
  83.                         return plaintext;
  84.                 } catch (Exception e) {
  85.                         e.printStackTrace();
  86.                         return null;
  87.                 }
  88.         }
  89.         public String decrypt(byte[] ciphertext) {
  90.                 try {
  91.                         String plaintext = new String(dcipher.doFinal(ciphertext), "UTF-8");
  92.                         return plaintext;
  93.                 } catch (Exception e) {
  94.                         e.printStackTrace();
  95.                         return null;
  96.                 }
  97.         }

  98.         private static byte[] getMD5(String input) {
  99.                 try {
  100.                         byte[] bytesOfMessage = input.getBytes("UTF-8");
  101.                         MessageDigest md = MessageDigest.getInstance("MD5");
  102.                         return md.digest(bytesOfMessage);
  103.                 } catch (Exception e) {
  104.                         return null;
  105.                 }
  106.         }

  107.         static final String HEXES = "0123456789ABCDEF";
  108.         public static String byteToHex(byte[] raw) {
  109.                 if (raw == null) {
  110.                         return null;
  111.                 }
  112.                 final StringBuilder hex = new StringBuilder(2 * raw.length);
  113.                 for (final byte b : raw) {
  114.                         hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(
  115.                                         HEXES.charAt((b & 0x0F)));
  116.                 }
  117.                 return hex.toString();
  118.         }
  119.         public static byte[] hexToByte(String hexString) {
  120.                 int len = hexString.length();
  121.                 byte[] ba = new byte[len / 2];
  122.                 for (int i = 0; i < len; i += 2) {
  123.                         ba[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
  124.                                         .digit(hexString.charAt(i + 1), 16));
  125.                 }
  126.                 return ba;
  127.         }

  128.         public static void main(String[] args) {
  129.                 String str="SHIYUNPENG";
  130.         }

  131. }
  132. public class TextReader{
  133. public static void main(String[] args) throws IOException {
  134.         Document doc = new Document("text.docx");//文件名
  135.                 String str=doc.getText();
  136.                 String MY="sdgs@#12ad##@2423sdf@$*((*)ghgs23846gsjgs&%$%#gsjfg%6646fhfhU52364926423648326jsfjbsdj%^%#$^%GHFJV";  //双方协商好的密钥
  137.                 String aesStr=new TestAES(MY).encrypt(str);
  138.                 System.out.println("AES加密后的字符串为:\n["+aesStr+"]\n");
  139.                 //进行解密后的内容:
  140.                 String de=new TestAES(MY).decrypt(aesStr);
  141.                 System.out.println("AES解密后的内容为:\n["+de+"]");
  142.         }}
复制代码
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-5-6 17:26 , Processed in 0.018511 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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