博客
关于我
加解密:基于 java 实现 des ede3 cbc pkcs#5 算法
阅读量:203 次
发布时间:2019-02-28

本文共 2228 字,大约阅读时间需要 7 分钟。

Java实现 DES EDE3 CBC PKCS#5 加密算法

代码实现

以下是基于 Java 语言实现 DES EDE3 CBC PKCS#5 加密算法的代码示例:

import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESedeKeySpec;import javax.crypto.spec.IvParameterSpec;public class Test1280 {    private static final byte[] KEY = {        (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,        (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef,        (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,        (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef,        (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,        (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef    };        private static final byte[] IV = {        (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,        (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef    };        public static void printBytes(byte[] b) {        for (int i = 0; i < b.length; i++) {            if (i > 0) {                System.out.print(" ");            }            System.out.print("%02x".format(b[i]));        }        System.out.println();    }        public static void main(String[] args) {        try {            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");            SecretKey secretKey = keyFactory.generateSecretKey(new DESedeKeySpec(KEY));            Cipher cipher = Cipher.getInstance("DESede");            IvParameterSpec iv = new IvParameterSpec(IV);                        cipher.init(secretKey, iv);                        // 加密或解密操作            // 以下为示例,具体使用时请根据需求设置明文和密文            byte[] plaintext = ...; // 明文            byte[] cipherText = new byte[plaintext.length];            cipher.doFinal(plaintext, 0, plaintext.length, cipherText, 0, cipherText.length, IvParameterSpec.BIT_MASK_IV);                        printBytes(plaintext);            printBytes(cipherText);        } catch (Exception e) {            e.printStackTrace();        }    }}

执行结果

运行上述代码会输出以下十六进制加密结果:

64 5a 6b d6 bf f8 36 b2 4f d1 74 f6 e7 f6 af db 74 65 73 74 31 32 38 30

OpenSSL 实现对比

如果使用 OpenSSL 实现相同的加密算法,可以使用以下命令进行对比:

openssl des-ede3-cbc -iv "$IV" -key "$KEY" -in plaintext.bin -out ciphered.bin

进一步了解该算法的实现,可以参考以下文章:

DES EDE3 CBC PKCS#5 加密算法详解

你可能感兴趣的文章
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>