티스토리 뷰

반응형

package project2;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author kkaok
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class SecurityUtil {
    /*
     * 자바시큐리티 르로그램밍 책 693참조 
     * byte[] desKeyData = generateHash(strkey);
     */
    
     public final static String SECURITYKEY = "123456789";


    public final static byte[] desKeyData = {
            (byte)2, (byte)4, (byte)6, (byte)8, (byte)10, (byte)12, (byte)14, (byte)16
    };
    public final static byte[] myIV = {(byte)20,(byte)21,(byte)10,(byte)5,(byte)7,(byte)9,(byte)10,(byte)5};
   
    private static SecretKey getKey() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException{
        // 입력값을 bytes로 변환
        // byte[] desKeyData = strkey.getBytes();
        // 암호화 키 생성
         byte[] desKeyData = {
                 (byte)2, (byte)4, (byte)6, (byte)8,
                 (byte)10, (byte)12, (byte)14, (byte)16
         };
        
        DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        return keyFactory.generateSecret(desKeySpec);
    }

    
    public static String encrypt(String data) throws Exception {
        if (data == null || data.length() == 0)
            return "";
        // 암호화를 위한 세팅  
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, getKey());

        byte[] inputBytes1 = data.getBytes("UTF8");
        byte[] outputBytes1 = cipher.doFinal(inputBytes1);

        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        return encoder.encode(outputBytes1);
    }
   
    public static String decrypt(String data) throws Exception {

        if (data == null || data.length() == 0)
            return "";
        javax.crypto.Cipher cipher = javax.crypto.Cipher
                .getInstance("DES/ECB/PKCS5Padding");
        cipher.init(javax.crypto.Cipher.DECRYPT_MODE, getKey());

        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        byte[] inputBytes1 = decoder.decodeBuffer(data);
        byte[] outputBytes2 = cipher.doFinal(inputBytes1);
        return new String(outputBytes2, "UTF8");
    }

    public static String encrypt1(String data) throws Exception {
            if (data == null || data.length() == 0) return "";
     Cipher c3des = Cipher.getInstance("DES/CBC/PKCS5Padding");
     SecretKeySpec    myKey = new SecretKeySpec(desKeyData, "DES");
     IvParameterSpec ivspec = new IvParameterSpec(myIV);

     c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
        byte[] inputBytes1 = data.getBytes("UTF8");
        byte[] outputBytes1 = c3des.doFinal(inputBytes1);

        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        return encoder.encode(outputBytes1);
    }

    public static String decrypt1(String data) throws Exception {

        if (data == null || data.length() == 0)
            return "";
           
        Cipher c3des = Cipher.getInstance("DES/CBC/PKCS5Padding");
        SecretKeySpec    myKey = new SecretKeySpec(desKeyData, "DES");
        IvParameterSpec ivspec = new IvParameterSpec(myIV);

        c3des.init(Cipher.DECRYPT_MODE, myKey, ivspec);
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        byte[] inputBytes1 = decoder.decodeBuffer(data);
        byte[] outputBytes2 = c3des.doFinal(inputBytes1);
        return new String(outputBytes2, "UTF8");
    }
   
    public static String encrypt3(String data) throws Exception {
            if (data == null || data.length() == 0) return "";
           
        byte[] tdesKeyData = {
              (byte)0xA2, (byte)0x15, (byte)0x37, (byte)0x07, (byte)0xCB, (byte)0x62,
              (byte)0xC1, (byte)0xD3, (byte)0xF8, (byte)0xF1, (byte)0x97, (byte)0xDF,
              (byte)0xD0, (byte)0x13, (byte)0x4F, (byte)0x79, (byte)0x01, (byte)0x67,
              (byte)0x7A, (byte)0x85, (byte)0x94, (byte)0x16, (byte)0x31, (byte)0x92 };

     Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
     SecretKeySpec    myKey = new SecretKeySpec(tdesKeyData, "DESede");
     IvParameterSpec ivspec = new IvParameterSpec(myIV);

     c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
        byte[] inputBytes1 = data.getBytes("UTF8");
        byte[] outputBytes1 = c3des.doFinal(inputBytes1);

        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        return encoder.encode(outputBytes1);
    }

    public static String decrypt3(String data) throws Exception {
        byte[] tdesKeyData = {
              (byte)0xA2, (byte)0x15, (byte)0x37, (byte)0x07, (byte)0xCB, (byte)0x62,
              (byte)0xC1, (byte)0xD3, (byte)0xF8, (byte)0xF1, (byte)0x97, (byte)0xDF,
              (byte)0xD0, (byte)0x13, (byte)0x4F, (byte)0x79, (byte)0x01, (byte)0x67,
              (byte)0x7A, (byte)0x85, (byte)0x94, (byte)0x16, (byte)0x31, (byte)0x92 };

       
        if (data == null || data.length() == 0)
            return "";
           
        Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        SecretKeySpec    myKey = new SecretKeySpec(tdesKeyData, "DESede");
        IvParameterSpec ivspec = new IvParameterSpec(myIV);

        c3des.init(Cipher.DECRYPT_MODE, myKey, ivspec);
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        byte[] inputBytes1 = decoder.decodeBuffer(data);
        byte[] outputBytes2 = c3des.doFinal(inputBytes1);
        return new String(outputBytes2, "UTF8");
    } 
   
    public static void main(String[] args) throws Exception {
        // DES 방식의 암복호화
        System.out.println(encrypt("sangha"));
        System.out.println(decrypt(encrypt("sangha")));
        // DES 방식의 IV 사용 암복호화
        System.out.println(encrypt1("sangha"));
        System.out.println(decrypt1(encrypt1("sangha")));
       
        System.out.println(encrypt3("sangha"));
        System.out.println(decrypt3(encrypt3("sangha")));
    }
}

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함