手记

Java 3DES 加解/密程序

package trades.lib.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class Des3Crypt {

    private static final String Algorithm = "DESede/ECB/PKCS5Padding";

//  private static final String Algorithm = "DES";

    // 3DES加密法方法
    public static String encrypt(byte[] cipherTextByte) {
        SecretKey scKey = null;// 获取密钥key
        Cipher cipher = null;// 加解密器
        byte[] outputBytes = null;// 加密/解密后的数据包
        byte[] inputBytes = cipherTextByte;// 加密/解密前的数据包
        try {
            DESedeKeySpec desKeySpec = new DESedeKeySpec(
                    "zhaochenghuitanfangpinga".getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory
                    .getInstance("DESede");
            scKey = keyFactory.generateSecret(desKeySpec);
            cipher = Cipher.getInstance(Algorithm);// 生成解密器
        } catch (Exception e) {
            e.printStackTrace();
        }

        String plainTextStr = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(inputBytes);
            int opmode = Cipher.ENCRYPT_MODE;
            cipher.init(opmode, scKey);

            int blockSize = cipher.getBlockSize();
            int outputSize = cipher.getOutputSize(blockSize);

            byte[] inBytes = new byte[blockSize];
            byte[] outBytes = new byte[outputSize];
            byte[] appendBytes = new byte[blockSize];

            int realLength = 0;
            boolean fit = true;
            while (fit) {
                realLength = in.read(inBytes);
                if (realLength == blockSize) {
                    int ol = cipher.update(inBytes, 0, blockSize, outBytes);
                    out.write(outBytes, 0, ol);
                } else {
                    fit = false;
                }
            }
            // int outputLength = cipher.doFinal(appendBytes, 0,
            // blockSize,outBytes);
            if (realLength > 0 && realLength < blockSize) {
                // 不足8字节的数据块按照PKCS5方式补充
                int pad = blockSize - realLength;
                // System.out.println("不足8字节的数据块按照PKCS5方式补充:" + pad);
                for (int i = realLength; i < blockSize; i++) {
                    inBytes[i] = new Integer(pad).byteValue();// 转化为byte
                }
                outBytes = cipher.doFinal(inBytes, 0, realLength);
                out.write(outBytes);
            } else if (realLength <= 0) {
                // 恰好能分成整数组,则补充一个8位字节数组
                int outputLength = cipher.doFinal(appendBytes, 0, blockSize,
                        outBytes);
                out.write(outBytes, 0, outputLength);
            }
            outputBytes = out.toByteArray();
            plainTextStr = ByteUtil.byte2hex(outputBytes);
            // System.out.println("3des密文:" + plainTextStr);
            out.flush();
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return plainTextStr;
    }

    // 3DES解密法方法
    public static String decrypt(String plainText) {

        byte[] plainTextByte=ByteUtil.hex2byte(plainText);
        SecretKey scKey = null;// 获取密钥key
        Cipher cipher = null;// 加解密器
        byte[] outputBytes = null;// 加密/解密后的数据包
        byte[] inputBytes = plainTextByte;// 加密/解密前的数据包
        try {
            DESedeKeySpec desKeySpec = new DESedeKeySpec(
                    "zhaochenghuitanfangpinga".getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory
                    .getInstance("DESede");
            scKey = keyFactory.generateSecret(desKeySpec);
            cipher = Cipher.getInstance(Algorithm);// 生成解密器
        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] retTextByte = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(inputBytes);
            int opmode = Cipher.DECRYPT_MODE;
            cipher.init(opmode, scKey);

            int blockSize = cipher.getBlockSize();
            int outputSize = cipher.getOutputSize(blockSize);

            byte[] inBytes = new byte[blockSize];
            byte[] outBytes = new byte[outputSize];

            int realLength = 0;
            boolean fit = true;
            while (fit) {
                realLength = in.read(inBytes);
                if (realLength == blockSize) {
                    // 按块写入输出字符数组流
                    int outLength = cipher.update(inBytes, 0, blockSize,
                            outBytes);
                    out.write(outBytes, 0, outLength);
                } else
                    fit = false;
            }
            if (realLength > 0) {
                outBytes = cipher.doFinal(inBytes, 0, realLength);
            } else {
                outBytes = cipher.doFinal();
            }
//          byte[] outBytes=cipher.doFinal(plainTextByte);
            out.write(outBytes);
            outputBytes = out.toByteArray();
            retTextByte = outputBytes;
//          System.out.println("3des明文的长度:"+retTextByte.length+"\t3des明文:" + new String(retTextByte));
            out.flush();
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(retTextByte);
    }

    public void init(String key) {

    }

    public static void main(String[] args) {
        String pin1 = "888888";
        String pinVlaue = encrypt(pin1.getBytes());
        System.out.println(pinVlaue);
        String c=decrypt(pinVlaue);
        System.out.print(c);

    }
}
0人推荐
随时随地看视频
慕课网APP