Demo entry 6786806

AES

   

Submitted by anonymous on Apr 04, 2019 at 12:38
Language: Java. Code size: 3.8 kB.

package hw1;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AES {
	
	/** 암호화 키 16자리 */
	public static String key = "2015125064LBORAM";
	
    public static void main(String[] args) {
    	
    	  String message = "ABCDEFGHIJKLMNOP";
    	  String modified_message = "ABCDEFGHIJKLMNOQ";
    	
        try {
        	
            System.out.println("origin str = " + message);
            System.out.println("modified str = " + modified_message);
            
            System.out.println("");
            
            String encrypt = encrypt(message);            
            String encrypt2 = encrypt(modified_message);
            System.out.println("origin message encrypt str = " + encrypt);
            System.out.println("origin message encrypt str = " + encrypt2);
            System.out.println("");
            
            int length = encrypt.length();
            int diff = 0;
            
            for(int i=0;i<length;i++) {
            	if(encrypt.charAt(i) != encrypt2.charAt(i)) {
            		diff++;
            	}
            }
            
            System.out.println("numbers of different byte = "+ diff);
            System.out.println("");
            
            String decrypt = decrypt(encrypt);
            System.out.println("decrypt str = " + decrypt);
           
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }

    /**
     * hex to byte[] : 16진수 문자열을 바이트 배열로 변환한다.
     *
     * @param hex    hex string
     * @return
     */
    public static 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;
    }

    /**
     * byte[] to hex : unsigned byte(바이트) 배열을 16진수 문자열로 바꾼다.
     *
     * @param ba        byte[]
     * @return
     */
    public static 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();
    }


    /**
     * AES 방식의 암호화
     *
     * @param message 암호화 대상 문자열
     * @return String 암호화 된 문자열
     * @throws Exception
     */
    public static String encrypt(String message) throws Exception {

        // use key coss2
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");

        // Instantiate the cipher
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] encrypted = cipher.doFinal(message.getBytes());
        
        return byteArrayToHex(encrypted);
    }
    
    /**
     * AES 방식의 복호화
     *
     * @param message 복호화 대상 문자열
     * @return String 복호화 된 문자열
     * @throws Exception
     */
    public static String decrypt(String encrypted) throws Exception {

        // use key coss2
        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;
    }

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).