3DES 암호 화 복호화

더 읽 기
package com.dc;

import java.security.InvalidKeyException;  
import java.security.NoSuchAlgorithmException;  
import java.security.Security;  
  
import javax.crypto.BadPaddingException;  
import javax.crypto.Cipher;  
import javax.crypto.IllegalBlockSizeException;  
import javax.crypto.KeyGenerator;  
import javax.crypto.NoSuchPaddingException;  
import javax.crypto.SecretKey;  

/* ******************       *********************
 * class       :  DcDES3Util
 * @author     :  ncc
 * create time :  2017-12-19   10:01:53
 * @version    :  1.0  
 * description :  3DES  Triple DES, DES         ,   3 56     3DES
 *         。      (DES)               ,
 *           ,  1981  ANSI     ANSI X.3.92。
 * DES  56          ,         ,     64    
 *           。     DES,3DES    。 3DES( Triple DES) 
 * DES AES       (1999 ,NIST 3-DES          ),
 *  DES         。  DES     ,                 ,
 *        :
 *   Ek() Dk()  DES          ,K  DES       ,P    ,C    , 
 *    ,3DES     :C=Ek3(Dk2(Ek1(P))) 
 *  3DES     :P=Dk1((EK2(Dk3(C))) 
 * @see        :                        
 * ************************************************/   
public class DcDES3Util {  
  
    // KeyGenerator             ,        
    private KeyGenerator keygen;  
    // SecretKey           
    private SecretKey deskey;  
    // Cipher             
    private Cipher c;  
    //                 
    private byte[] cipherByte;  
  
    /**
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     */
    public DcDES3Util() throws NoSuchAlgorithmException, NoSuchPaddingException {  
        Security.addProvider(new com.sun.crypto.provider.SunJCE());  
        //      DES        (          ,      )  
        keygen = KeyGenerator.getInstance("DESede");  
        //       
        deskey = keygen.generateKey();  
        //   Cipher  ,      DES    
        c = Cipher.getInstance("DESede");  
    }  
  
    /* ********************************************
     * method name   : Encrytor 
     * description   :        
     * @return       : byte[]
     * @param        : @param str
     * @param        : @return
     * @param        : @throws InvalidKeyException
     * @param        : @throws IllegalBlockSizeException
     * @param        : @throws BadPaddingException
     * modified      : ncc ,  2017-12-19
     * @see          : 
     * ********************************************/      
    public byte[] Encrytor(String str) throws InvalidKeyException,  
            IllegalBlockSizeException, BadPaddingException {  
        //     , Cipher       ,ENCRYPT_MODE        
        c.init(Cipher.ENCRYPT_MODE, deskey);  
        byte[] src = str.getBytes();  
        //   ,     cipherByte  
        cipherByte = c.doFinal(src);  
        return cipherByte;  
    }  
  
    /* ********************************************
     * method name   : Decryptor 
     * description   :        
     * @return       : byte[]
     * @param        : @param buff
     * @param        : @return
     * @param        : @throws InvalidKeyException
     * @param        : @throws IllegalBlockSizeException
     * @param        : @throws BadPaddingException
     * modified      : ncc ,  2017-12-19
     * @see          : 
     * ********************************************/      
    public byte[] Decryptor(byte[] buff) throws InvalidKeyException,  
            IllegalBlockSizeException, BadPaddingException {  
        //     , Cipher       ,DECRYPT_MODE        
        c.init(Cipher.DECRYPT_MODE, deskey);  
        cipherByte = c.doFinal(buff);  
        return cipherByte;  
    }  
  
    /** 
     * @param args 
     * @throws NoSuchPaddingException  
     * @throws NoSuchAlgorithmException  
     * @throws BadPaddingException  
     * @throws IllegalBlockSizeException  
     * @throws InvalidKeyException  
     */  
    public static void main(String[] args) throws Exception {  
    	DcDES3Util des3 = new DcDES3Util();  
        String msg ="        !";  
        byte[] encontent = des3.Encrytor(msg);  
        byte[] decontent = des3.Decryptor(encontent);  
        System.out.println("   :" + msg);  
        System.out.println("   :" + new String(encontent));  
        System.out.println("   :" + new String(decontent));  
  
    }  
}

 

좋은 웹페이지 즐겨찾기