728x90
반응형
package egovframework.util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class RandomAESKeyGen {
public static String generate(final int keyLen) throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(keyLen);
SecretKey secretKey = keyGen.generateKey();
byte[] encoded = secretKey.getEncoded();
return DatatypeConverter.printHexBinary(encoded).toLowerCase();
}
public static String generate2(final int keyLen) throws NoSuchAlgorithmException {
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[keyLen/8];
random.nextBytes(bytes);
return DatatypeConverter.printHexBinary(bytes).toLowerCase();
}
public static String generateBase64(final int keyLen) throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(keyLen);
SecretKey secretKey = keyGen.generateKey();
byte[] encoded = secretKey.getEncoded();
String key = DatatypeConverter.printHexBinary(encoded).toLowerCase();
byte[] base64Key = Base64.encodeBase64(key.getBytes());
return new String(base64Key);
}
public static String generate2Base64(final int keyLen) throws NoSuchAlgorithmException {
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[keyLen/8];
random.nextBytes(bytes);
String key = DatatypeConverter.printHexBinary(bytes).toLowerCase();
byte[] base64Key = Base64.encodeBase64(key.getBytes());
return new String(base64Key);
}
public static void main(String[] args) {
String key = null;
String result="";
for(int i=0; i< 5; ++i) {
try {
key = RandomAESKeyGen.generateBase64(128);
} catch (NoSuchAlgorithmException e) {
System.out.println("Exception caught");
e.printStackTrace();
}
System.out.println("KeyGenerator 128 key 순번:[" + i + "]" + key);
}
System.out.println("==================");
for(int i=0; i< 5; ++i) {
try {
key = RandomAESKeyGen.generateBase64(256);
} catch (NoSuchAlgorithmException e) {
System.out.println("Exception caught");
e.printStackTrace();
}
System.out.println("KeyGenerator 256key 순번:[" + i + "]" + key);
}
System.out.println("==================");
for(int i=0; i< 5; ++i) {
try {
key = RandomAESKeyGen.generate2Base64(128);
} catch (NoSuchAlgorithmException e) {
System.out.println("Exception caught");
e.printStackTrace();
}
System.out.println("SecureRandom 128 key 순번:[" + i + "]" + key);
}
System.out.println("==================");
for(int i=0; i< 5; ++i) {
try {
key = RandomAESKeyGen.generate2Base64(256);
} catch (NoSuchAlgorithmException e) {
System.out.println("Exception caught");
e.printStackTrace();
}
System.out.println("SecureRandom 256key 순번:[" + i + "]" + key);
}
}
}
728x90
반응형
'프로그램 > 자바' 카테고리의 다른 글
Java 에서 ValidatorException 등 인증서 관련 에러 해결 (1) | 2023.05.10 |
---|---|
자바 cors 우회하기 get,post방식 (0) | 2022.07.12 |
자바로 크로스 도메인 우회처리 (0) | 2021.12.14 |
자바로 PWA PUSH 만들기 (0) | 2021.08.06 |
기본 Java 타입과 DB 필드 매핑 (0) | 2021.08.04 |
댓글