代码拉取完成,页面将自动刷新
package com.cs.coder.util;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* @author Vince
* @date 2019/12/10 14:24
*/
public class AES {
private static final String AES_MODE = "AES/ECB/PKCS5Padding";
// private static final int keySize = 16;
// test search key gitwanlishi
// wwww
public static byte[] decrypt(byte[] key, byte[] data, int keySize) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(trimKeySize(key, keySize), "AES");
Cipher instance = Cipher.getInstance(AES_MODE);
instance.init(2, secretKeySpec);
return instance.doFinal(data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("SameParameterValue")
private static byte[] trimKeySize(byte[] key, int keySize) {
byte[] bArr2 = new byte[keySize];
if (key.length <= keySize) {
keySize = key.length;
}
System.arraycopy(key, 0, bArr2, 0, keySize);
return bArr2;
}
public static byte[] encrypt(byte[] key, byte[] data, int keySize) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(trimKeySize(key, keySize), "AES");
Cipher instance = Cipher.getInstance(AES_MODE);
instance.init(1, secretKeySpec);
return instance.doFinal(data);
} catch (NoSuchAlgorithmException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) {
e.printStackTrace();
return null;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。