aes 암호화 AES/ECB/PKCS7Padding|||AES/ECB/PKCS5Padding
25775 단어 작업 노트
암호화 방식: ECB, CBC, CFB, OFB, CTR
채우기 방식: PKCS5Padding, PKCS7Padding, zeropadding, iso10126, ansix923
블록 길이: 128비트, 192비트, 256비트
키 길이: 16비트, 24비트, 32비트
/**
*
*/
public static String CHARSET="UTF-8";
public static String ALGORITHM = "AES";
/**
* :ECB、CBC、CFB、OFB、CTR
* :PKCS5Padding、PKCS7Padding、zeropadding、iso10126、ansix923
* :128 、192 、256
*/
@AllArgsConstructor
@Getter
public enum AlgorithmECBEnum{
ALGORITHM_ECB_5("AES/ECB/PKCS5Padding"),
ALGORITHM_ECB_7("AES/ECB/PKCS7Padding"),
ALGORITHM_ECB_z("AES/ECB/zeroPadding"),
ALGORITHM_ECB_i("AES/ECB/iso10126"),
ALGORITHM_ECB_a("AES/ECB/ansix923")
;
private String algorithm;
}
@AllArgsConstructor
@Getter
public enum AlgorithmCBCEnum{
ALGORITHM_CBC_5("AES/CBC/PKCS5Padding"),
ALGORITHM_CBC_7("AES/CBC/PKCS7Padding"),
ALGORITHM_CBC_z("AES/CBC/zeroPadding"),
ALGORITHM_CBC_i("AES/CBC/iso10126"),
ALGORITHM_CBC_a("AES/CBC/ansix923"),
;
private String algorithm;
}
@AllArgsConstructor
@Getter
public enum AlgorithmCFBEnum{
ALGORITHM_CFB_5("AES/CFB/PKCS5Padding"),
ALGORITHM_CFB_7("AES/CFB/PKCS7Padding"),
ALGORITHM_CFB_z("AES/CFB/zeroPadding"),
ALGORITHM_CFB_i("AES/CFB/iso10126"),
ALGORITHM_CFB_a("AES/CFB/ansix923")
;
private String algorithm;
}
@AllArgsConstructor
@Getter
public enum AlgorithmOFBEnum{
ALGORITHM_OFB_5("AES/OFB/PKCS5Padding"),
ALGORITHM_OFB_7("AES/OFB/PKCS7Padding"),
ALGORITHM_OFB_z("AES/OFB/zeroPadding"),
ALGORITHM_OFB_i("AES/OFB/iso10126"),
ALGORITHM_OFB_a("AES/OFB/ansix923")
;
private String algorithm;
}
@AllArgsConstructor
@Getter
public enum AlgorithmCTREnum{
ALGORITHM_CTR_5("AES/CTR/PKCS5Padding"),
ALGORITHM_CTR_7("AES/CTR/PKCS7Padding"),
ALGORITHM_CTR_z("AES/CTR/zeroPadding"),
ALGORITHM_CTR_i("AES/CTR/iso10126"),
ALGORITHM_CTR_a("AES/CTR/ansix923")
;
private String algorithm;
}
/**
* key
* 16=128 bit
* 24=192 bit
* 32=256 bit
*/
@AllArgsConstructor
@Getter
public enum BitEnum{
ALGORITHM_128(16),
ALGORITHM_192(24),
ALGORITHM_256(32);
private Integer bit;
public static boolean contains(Integer bit){
if (null == bit){
return false;
}
for (BitEnum bitEnum : BitEnum.values()){
if (bitEnum.getBit().equals(bit)){
return true;
}
}
return false;
}
}
/**
* PKCS7Padding
*/
public static boolean initialized = false;
public static void initialize(){
if (initialized) return;
Security.addProvider(new BouncyCastleProvider());
initialized = true;
}
/**
* encode ecb
* @param key
* @param content
* @param algorithmEnum /
* @return
*/
public static String encodeECB(String key, String content, AlgorithmECBEnum algorithmEnum){
Assert.notNull(content," ");
Assert.notNull(key," key ");
Assert.notNull(algorithmEnum," / ");
int length = key.length();
if (!BitEnum.contains(length)){
throw new RuntimeException("key 16 24 32 ");
}
try{
Cipher cipher = Cipher.getInstance(algorithmEnum.getAlgorithm());
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET),ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE,keySpec);
byte[] bytes = cipher.doFinal(content.getBytes(CHARSET));
return Base64Util.encodeBase64(bytes);
}catch (Exception e){
throw new RuntimeException(e);
}
}
/**
* encode cbc
* @param key
* @param content
* @param algorithmEnum /
* @param iv CBC , iv,
* @return
*/
public static String encodeCBC(String key, String content, AlgorithmCBCEnum algorithmEnum,String iv){
Assert.notNull(content," ");
Assert.notNull(key," key ");
Assert.notNull(iv," iv ");
Assert.notNull(algorithmEnum," / ");
int length = key.length();
if (!BitEnum.contains(length)){
throw new RuntimeException("key 16 24 32 ");
}
try{
Cipher cipher = Cipher.getInstance(algorithmEnum.getAlgorithm());
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET),ALGORITHM);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(CHARSET));
cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivParameterSpec);
byte[] bytes = cipher.doFinal(content.getBytes(CHARSET));
return Base64Util.encodeBase64(bytes);
}catch (Exception e){
throw new RuntimeException(e);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[난치병] 시스템 시간이 잘못되어 SSL 연결이 실패했습니다.우리 안드로이드 박스의 클라이언트는 매번 첫 번째 서버에 연결할 때마다 ssl 오류가 발생하여 연결이 실패합니다. SSL_connect error:00000001:lib(0):func(0):reason(1) 네트워크...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.