1. 程式人生 > 其它 >Excel VBA ——如何快速填充紙質登記表

Excel VBA ——如何快速填充紙質登記表

static final  String ALGORITHM = "AES";

/**
* @Author:AZJ
* @Description:生成金鑰
* @Date:2022/4/12 16:27
*/
public static SecretKey generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = new SecureRandom();
keyGenerator.init(secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey;
}

static Charset charset =Charset.forName("UTF-8");
/**
* @Author:AZJ
* @Description:加密
* @Date:2022/4/12 16:29
*/
public static byte[] encrypt(String content,SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
return aes(content.getBytes(charset), Cipher.ENCRYPT_MODE,secretKey);
}

/**
* @Author:AZJ
* @Description:解密
* @Date:2022/4/12 16:30
*/
public static String decrypt(byte[] contentArray,SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte[] result = aes(contentArray,Cipher.DECRYPT_MODE,secretKey);
return new String(result,charset);
}

private static byte[] aes(byte[] contentArray, int mode,SecretKey secretKey) throws NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(mode,secretKey);
byte[] result = cipher.doFinal(contentArray);
return result;
}

public static void main(String[] args){
    String content = "你好,我是加解密";
SecretKey secretKey;
try {
secretKey = generateKey();
byte[] encrypt = encrypt(content,secretKey);
String decrypt = decrypt(encrypt,secretKey);
System.out.println("加密後的結果為:"+encrypt);
System.out.println("解密後的結果為:"+decrypt);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
}