博客
关于我
加解密:基于 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 加密算法详解

你可能感兴趣的文章
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql where中如何判断不为空
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
mysql 不区分大小写
查看>>
mysql 两列互转
查看>>
MySQL 中开启二进制日志(Binlog)
查看>>
MySQL 中文问题
查看>>
MySQL 中日志的面试题总结
查看>>
mysql 中的all,5分钟了解MySQL5.7中union all用法的黑科技
查看>>