DES 암호화, 문자열 해독 알고리즘(java 버전)

4838 단어 des

import java.security.Key;
import java.security.Security;
 
import javax.crypto.Cipher;
 
public class DESUtil {
 
    private static String strDefaultKey = "ixAZ^%)2";
 
    private static Cipher encryptCipher = null;
 
    private static Cipher decryptCipher = null;
 
    public static String byteArr2HexStr(byte[] arrB) throws Exception {
        int iLen = arrB.length;
//각byte는 두 문자로 표시할 수 있기 때문에 문자열의 길이는 그룹 길이의 두 배이다
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = arrB[i];
//음수를 양수로 변환
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
//0F 미만의 수는 앞에서 0을 보충해야 합니다
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }
 
    public static byte[] hexStr2ByteArr(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
 
//두 문자는 한 바이트를 표시하기 때문에 바이트 그룹의 길이는 문자열의 길이를 2로 나눈다
        byte[] arrOut = new byte[iLen/2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i/2] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }
 
    public DESUtil() throws Exception {
        this(strDefaultKey);
    }
 
   /**
* 키 구성 방법 지정
     * 
* @param strKey에서 지정한 키
     * @throws Exception
     */
    public DESUtil(String strKey) throws Exception {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        Key key = getKey(strKey.getBytes());
 
        encryptCipher = Cipher.getInstance("DES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key);
 
        decryptCipher = Cipher.getInstance("DES");
        decryptCipher.init(Cipher.DECRYPT_MODE, key);
    }
 
   /**
* 암호화된 바이트 배열
     * 
* @param arrB 암호화된 바이트 배열
* @return 암호화된 바이트 배열
     * @throws Exception
     */
    public static byte[] encrypt(byte[] arrB) throws Exception {
        return encryptCipher.doFinal(arrB);
    }
 
   /**
* 암호화 문자열
     * 
* @param strIn 암호화할 문자열
* @return 암호화된 문자열
     * @throws Exception
     */
    public static String encrypt(String strIn) throws Exception {
        return byteArr2HexStr(encrypt(strIn.getBytes()));
    }
 
   /**
* 바이트 배열 복호화
     * 
* @param arrB 복호화할 바이트 배열
* @return 복호화 후 바이트 배열
     * @throws Exception
     */
    public static byte[] decrypt(byte[] arrB) throws Exception {
        return decryptCipher.doFinal(arrB);
    }
 
    public static String decrypt(String strIn) throws Exception {
        return new String(decrypt(hexStr2ByteArr(strIn)));
    }
 
    private Key getKey(byte[] arrBTmp) throws Exception {
//빈 8비트 바이트 그룹 만들기 (기본값은 0)
        byte[] arrB = new byte[8];
 
//원시 바이트 그룹을 8비트로 변환
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
 
//키 생성
        Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
 
        return key;
    }
    public static void main(String[] args) {    // TODO Auto-generated method stub    try {     String test = "Hellow Word!" ;     //DESPlus des = new DESPlus();//     DESPlus des = new DESPlus( "leemenz" ); //     System.out.println( " :" +test);     System.out.println( " :" +des.encrypt(test));     System.out.println( " :" +des.decrypt(des.encrypt(test)));    } catch (Exception e) {     // TODO: handle exception     e.printStackTrace();    }   }
 
}

좋은 웹페이지 즐겨찾기