为了账号安全,请及时绑定邮箱和手机立即绑定

Java 3DES 加解/密程序

标签:
Java
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);

    }
}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消