教学服务系统

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

信息计算2019级2班5号张欢

[复制链接]

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
发表于 2022-4-5 16:02:27 | 显示全部楼层 |阅读模式
线上提交3.11-3.14

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
 楼主| 发表于 2022-4-5 19:32:25 | 显示全部楼层
  1. import javax.crypto.BadPaddingException;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.IllegalBlockSizeException;
  4. import javax.crypto.KeyGenerator;
  5. import javax.crypto.NoSuchPaddingException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.nio.charset.Charset;
  8. import java.security.InvalidKeyException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.SecureRandom;
  11. import java.util.Base64;
  12. import java.util.Base64.Encoder;
  13. import javax.crypto.SecretKey;

  14. public class AESTest {
  15.         static final String ALGORITHM="AES";
  16.         public static SecretKey generatekey() throws NoSuchAlgorithmException {
  17.                 KeyGenerator secretGenerator=KeyGenerator.getInstance(ALGORITHM);
  18.                 SecureRandom secureRandom=new SecureRandom();
  19.                 secretGenerator.init(secureRandom);
  20.                 SecretKey secretKey=secretGenerator.generateKey();
  21.                 return secretKey;
  22.         }
  23.         final static String charsetName="UTF-8";
  24.         static Charset charset=Charset.forName("UTF-8");
  25.         //编码
  26.         public static byte[] encrypt(String content,SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
  27.                 byte[] encryptResult = aes(content.getBytes(charset),Cipher.ENCRYPT_MODE,secretKey);
  28.                 Encoder enc = Base64.getEncoder();
  29.             String encStr = enc.encodeToString(encryptResult);
  30.             System.out.println("加密后base64编码的密文 : " + encStr);  
  31.             return encryptResult;
  32.         }
  33.         //解码
  34.         public static String decrypt(byte[] contentArray,SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
  35.                 byte[] result=aes(contentArray,Cipher.DECRYPT_MODE,secretKey);
  36.                 return new String(result,charsetName);
  37.         }
  38.         private static byte[] aes(byte[] contentArray,int mode,SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
  39.                 Cipher cipher=Cipher.getInstance(ALGORITHM);
  40.                 cipher.init(mode, secretKey);
  41.                 byte[] result=cipher.doFinal(contentArray);
  42.                 return result;
  43.         }
  44.         public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
  45.                 String content="不想写代码了";
  46.                 SecretKey secretKey=generatekey();
  47.                 byte[] encryptResult=encrypt(content,secretKey);
  48.                 String decryptResult=decrypt(encryptResult,secretKey);
  49.                 System.out.println("解密后的结果为:"+decryptResult);
  50.         }
  51. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-5-6 10:32 , Processed in 0.015736 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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