자바 특정 키 에 따라 문자열 복호화

9841 단어
package com.test;

import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class CryptoUtilTest {

    private final static String DES = "DES";
    private final static String ENCODE = "GBK";


    /**
     * Description         
     * @param data      
     * @param key   
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE));
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     *         
     * @param data      
     * @param key      
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf, key.getBytes(ENCODE));
        return new String(bt, ENCODE);
    }

    /**
     * Description         
     * 
     * @param data
     * @param key
     *               byte  
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        //             
        SecureRandom sr = new SecureRandom();
        //          DESKeySpec  
        DESKeySpec dks = new DESKeySpec(key);
        //         ,     DESKeySpec   SecretKey  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher          
        Cipher cipher = Cipher.getInstance(DES);
        //       Cipher  
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

    /**
     * Description         
     * 
     * @param data
     * @param key    byte  
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        //             
        SecureRandom sr = new SecureRandom();
        //          DESKeySpec  
        DESKeySpec dks = new DESKeySpec(key);
        //         ,     DESKeySpec   SecretKey  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher          
        Cipher cipher = Cipher.getInstance(DES);
        //       Cipher  
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }
    
    public static void main(String[] args){
        String data = "12AU@_@is(m)8;1:'0js:\"]\"qASI08";
        String key ="qwerrewq"; //  
        System.out.println("   ===>"+data);
        try {
            String jiami = encrypt(data, key);
            System.err.println("   :"+jiami );
            
            String jiemi = decrypt(jiami, key);
            System.err.println("   :"+jiemi);

        } catch (Exception e) {
            System.out.println("     "+" "+"        ");
            e.printStackTrace();
        }
    }
    
    
}

    
    

 
다음으로 전송:https://www.cnblogs.com/sunhaoyu/p/10704346.html

좋은 웹페이지 즐겨찾기