앱)aesciperutil 자바 common 코드
static String key = "~~~~~~";
byte[] hexToByteArray(String hex){
if( hex == null || hex.length() == 0){
return null;
}
byte[] ba = new byte[hex.length() / 2];
for( int i =0 ; i<ba.length; i++){
ba[i] = (byte) Integer.parseInt(hex.substring(2*i, 2*i+2), 16);
}
return ba;
}
String byteArrayToHex(byte[] ba){
if( ba == null || ba.length == 0){
return null;
}
StringBuffer sb = new StringBuffer(ba.length*2);
String hexNumber;
for( int x = 0; x<ba.length; x++){
hexNumber = "0" + Integer.toHexString(0xff & ba[x]);
sb.append(hexNumber.substring(hexNumber.length() - 2);
}
return sb.toString();
}
String encrypt(String message){
SecretKeySpec skeySpec = new SecretKeySpec(key.getByte(), "AES");
Cipher cipher = Cipher.getInstance("AES");
chpher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(message.getByte());
return byteArrayToHex(encrypted);
}
String decrypt(String encrypted){
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(hexToByteArray(encrypted));
String originalString = new String(original);
return originalString;
}
public static void main(String[] args){
String enc = encrypt("~~~~~");
String dec = decrypt("~~~~~");
}
byte[] hexToByteArray(String hex){
if( hex == null || hex.length() == 0){
return null;
}
byte[] ba = new byte[hex.length() / 2];
for( int i =0 ; i<ba.length; i++){
ba[i] = (byte) Integer.parseInt(hex.substring(2*i, 2*i+2), 16);
}
return ba;
}
String byteArrayToHex(byte[] ba){
if( ba == null || ba.length == 0){
return null;
}
StringBuffer sb = new StringBuffer(ba.length*2);
String hexNumber;
for( int x = 0; x<ba.length; x++){
hexNumber = "0" + Integer.toHexString(0xff & ba[x]);
sb.append(hexNumber.substring(hexNumber.length() - 2);
}
return sb.toString();
}
String encrypt(String message){
SecretKeySpec skeySpec = new SecretKeySpec(key.getByte(), "AES");
Cipher cipher = Cipher.getInstance("AES");
chpher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(message.getByte());
return byteArrayToHex(encrypted);
}
String decrypt(String encrypted){
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(hexToByteArray(encrypted));
String originalString = new String(original);
return originalString;
}
public static void main(String[] args){
String enc = encrypt("~~~~~");
String dec = decrypt("~~~~~");
}
'백엔드개발 > 자바스프링' 카테고리의 다른 글
앱)logback.xml sample example (0) | 2018.04.04 |
---|---|
앱)loggerutil 자바 common 코드 (0) | 2018.04.02 |
앱)md5util 자바 common 코드 (0) | 2018.04.02 |
앱)servletparamutil 자바 common 코드 (0) | 2018.03.30 |
앱)hashutil 자바 common 코드 (0) | 2018.03.30 |