반응형

MD5 (Message-Digest algorithm 5)

코드(소스) 보기

더보기
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

		public static String encrypt(String str){

			String MD5 = ""; 

			try{

				MessageDigest md = MessageDigest.getInstance("MD5"); 

				md.update(str.getBytes()); 

				byte byteData[] = md.digest();

				StringBuffer sb = new StringBuffer(); 

				for(int i = 0 ; i < byteData.length ; i++){

					sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));

				}

				MD5 = sb.toString();

				

			}catch(NoSuchAlgorithmException e){

				e.printStackTrace(); 

				MD5 = null; 

			}

			return MD5;

		}

}

 

사용법

public String util() {
		String str = "자바킹";
		String md5_en = MD5Util.encrypt(str);

		System.out.println("MD 5 암호화 : "+md5_en);//MD5 암호화
		return str;
}

SHA-256 (Secure Hash Algorithm 256)

코드(소스) 보기

더보기
import java.security.MessageDigest;

public class SHA256Util {

	public static String encrypt(String planText) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			md.update(planText.getBytes());
			byte byteData[] = md.digest();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < byteData.length; i++) {
				sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
			}
			StringBuffer hexString = new StringBuffer();
			for (int i = 0; i < byteData.length; i++) {
				String hex = Integer.toHexString(0xff & byteData[i]);
				if (hex.length() == 1) {
					hexString.append('0');
				}
				hexString.append(hex);
			}
			return hexString.toString();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
}

사용법

public String util() {
		String str = "자바킹";
		String sha256_en = SHA256Util.encrypt(str);

		System.out.println("SHA 256 암호화 : "+sha256_en);
		return str;
}

AES-128 (Advanced Encryption Standard 128)

코드(소스) 보기

더보기
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AES128Util {

    public static String encrypt(String input, String key) {
        byte[] crypted = null;
        try {
 
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
 
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());
        } catch(Exception e) {
            System.out.println(e.toString());
        }
 
        BASE64Encoder encoder = new BASE64Encoder();
 
        String str = encoder.encode(crypted);
 
        return new String(str);
    }
 
    public static String decrypt(String input, String key) {
        byte[] output = null;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
 
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
 
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skey);
            output = cipher.doFinal(decoder.decodeBuffer(input));
 
        } catch(Exception e) {
            System.out.println(e.toString());
        }
        return new String(output);
    }
}

사용법

public String util() {
		String str = "자바킹";
		String aes128key = "123456789abcdefg"; //사용자 지정 생성
		
		String aes_en = AES128Util.encrypt(str,aes128key);
		String aes_de = AES128Util.decrypt(aes_en,aes128key);

		System.out.println("AES 128 암호화 : "+aes_en);//AES128 암호화
		System.out.println("AES 128 복호화 : "+aes_de);//AES128 복호화
		return str;
}

3 DES (Triple Data Encryption Algorithm)

코드(소스) 보기

더보기
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;
    }
   
}

사용법

public String util() {
		String str = "자바킹";
		
        	String des3_en = DES3Util.encrypt(str);
		String des3_de = DES3Util.decrypt(des3_en);

		System.out.println("3DES 암호화 : "+des3_en); //DES3 암호화
		System.out.println("3DES 복호화 : "+des3_de); //DES3 복호화
		return str;
}

"자바킹" 문자열 암호화 종류별 결과

반응형

+ Recent posts