/****************************************
* \n 을 <br>로 바꾼다.
* @param String
* @return String 치환된 문자열
*****************************************/
public static String repl(String text) {
if( text == null || text.equals("") ) return "";
StringBuffer sb = new StringBuffer(text);
char ch;
for (int i = 0; i < sb.length(); i++) {
ch = sb.charAt(i);
if (ch == '\r' && sb.charAt(i+1) == '\n' ) {
sb.replace(i,i+2,"<br>");
i += 3;
}
}
return sb.toString();
}
BLOG ARTICLE 분류 전체보기 | 226 ARTICLE FOUND
- 2023.01.26 자바 \n을 <br>로 치환
- 2023.01.26 자바 특수문자 변환
- 2023.01.26 자바 DES 암복호화 함수
- 2023.01.26 자바 AES 암복호화 함수
- 2023.01.26 자바 Config 파일 읽는 클래스
/**
* 특수문자를 변환한다.
* @param String
*/
public static String convertInputValue(String message){
message = message.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """)
.replace("'", "'").replace("\\", "/").replace(" ", " ").replace("\n", "<br />");
return message;
}
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DES3Util {
private static Key key = null;
static {
if(key == null) {
// Key 초기화
KeyGenerator keyGenerator;
try {
keyGenerator = KeyGenerator.getInstance("TripleDES");
keyGenerator.init(168);
key = keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
public static String encrypt(String inStr) {
StringBuffer sb = null;
try {
Cipher cipher = Cipher.getInstance("TripleDES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plaintext = inStr.getBytes("UTF8");
byte[] ciphertext = cipher.doFinal(plaintext);
sb = new StringBuffer(ciphertext.length * 2);
for(int i = 0; i < ciphertext.length; i++) {
String hex = "0" + Integer.toHexString(0xff & ciphertext[i]);
sb.append(hex.substring(hex.length()-2));
}
}catch(Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public static String decrypt(String inStr) {
String text = null;
try {
byte[] b = new byte[inStr.length()/2];
Cipher cipher = Cipher.getInstance("TripleDES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
for(int i = 0; i < b.length; i++) {
b[i] = (byte)Integer.parseInt(inStr.substring(2*i, 2*i+2), 16);
}
byte[] decryptedText = cipher.doFinal(b);
text = new String(decryptedText,"UTF8");
}catch(Exception e) {
e.printStackTrace();
}
return text;
}
}
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import java.net.URLDecoder;
public class AESUtil {
public static String aesEncode(String str, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
String iv = key.substring(0, 16);
byte[] keyBytes = new byte[16];
byte[] b = key.getBytes("UTF-8");
int len = b.length;
if (len > keyBytes.length) {
len = keyBytes.length;
}
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(1, (Key)keySpec, new IvParameterSpec(iv.getBytes()));
byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
String enStr = new String(Base64.encodeBase64((byte[])encrypted));
return enStr;
}
public static String aesDecode(String str, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
String iv = key.substring(0, 16);
byte[] keyBytes = new byte[16];
byte[] b = key.getBytes("UTF-8");
int len = b.length;
if (len > keyBytes.length) {
len = keyBytes.length;
}
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(2, (Key)keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
byte[] byteStr = Base64.decodeBase64(str);
return new String(c.doFinal(byteStr), "UTF-8");
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class ConfigProperties {
private final Properties configProp = new Properties();
private static ConfigProperties instance = null;
// private constructor
private ConfigProperties() {
}
public static ConfigProperties getInstance() {
if (instance == null) {
for (Map.Entry entry: System.getenv().entrySet())
{
System.out.println( entry.getKey() + "=" + entry.getValue() );
}
String PATH = System.getenv("...");
instance = new ConfigProperties(PATH);
}
return instance;
}
private ConfigProperties(String PATH)
{
//Private constructor to restrict new instances
// InputStream in = this.getClass().getClassLoader().getResourceAsStream(PATH);
InputStream in = null;
try {
File file = new File(PATH);
in = new FileInputStream(file);
configProp.load(in);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (in!=null) in.close();
}catch(Exception e) {
}
}
}
public String getProperty(String key)
{
return configProp.getProperty(key);
}
public Set<String> getAllPropertyNames(){
return configProp.stringPropertyNames();
}
public boolean containsKey(String key){
return configProp.containsKey(key);
}
public void setProperty(String key, String value)
{
configProp.setProperty(key, value);
}
}
- 사용법
public static final String CHATSERVER_BASE_URL = ConfigProperties.getInstance().getProperty("chat.server.baseurl");
- config file 내용
chat.server.baseurl=http://localhost/