加解密:基于 java 实现 des ede3 cbc pkcs#5 算法-尊龙官方平台

加解密:基于 java 实现 des ede3 cbc pkcs#5 算法

el/2024/3/25 16:44:47

加解密:基于 java 实现 des ede3 cbc pkcs#5 算法

code:

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 {public 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,};public static final byte[] iv	= {(byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef,};/*** 十六进制打印字节数组* @param b byte[]*/public static void printbytes(byte[] b){for(int i=0;i<b.length;i){system.out.printf("x ", b[i]);}system.out.println();}public static void encrypt_des_ede_cbc_pkcs() throws exception{byte[] in = "test1280".getbytes("utf-8");cipher cipher = cipher.getinstance("desede/cbc/pkcs5padding");secretkeyfactory skf = secretkeyfactory.getinstance("desede");secretkey sk = skf.generatesecret(new desedekeyspec(key));ivparameterspec ips = new ivparameterspec(iv);cipher.init(cipher.encrypt_mode, sk, ips);byte[] out = cipher.dofinal(in);printbytes(out);}public static void decrypt_des_ede_cbc_pkcs() throws exception{byte[] out = {(byte) 0x64, (byte) 0x5a, (byte) 0x6b, (byte) 0xd6, (byte) 0xbf, (byte) 0xf8, (byte) 0x36, (byte) 0xb2,(byte) 0x4f, (byte) 0xd1, (byte) 0x74, (byte) 0xf6, (byte) 0xe7, (byte) 0xf6, (byte) 0xaf, (byte) 0xdb,};cipher cipher = cipher.getinstance("desede/cbc/pkcs5padding");secretkeyfactory skf = secretkeyfactory.getinstance("desede");secretkey sk = skf.generatesecret(new desedekeyspec(key));ivparameterspec ips = new ivparameterspec(iv);cipher.init(cipher.decrypt_mode, sk, ips);byte[] in = cipher.dofinal(out);printbytes(in);}public static void main(string[] args) throws exception{// todo auto-generated method stubencrypt_des_ede_cbc_pkcs();decrypt_des_ede_cbc_pkcs();}}

执行:

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 实现 des ede3 cbc pkcs#5 算法,请参见:

https://blog.csdn.net/test1280/article/details/105254483


http://www.ngui.cc/el/5127054.html

相关文章

openssl:undefined reference to symbol 'evp_encryptupdate@@libcrypto.so.10'

openssl:undefined reference to symbol ‘evp_encryptupdatelibcrypto.so.10’ 查看 openssl 版本: $ openssl version -a openssl 1.0.2k-fips 26 jan 2017 built on: reproducible build, date unspecified platform: linux-x86_64 options: bn(6…

goland (idea) 取消拼写检查

goland (idea) 取消拼写检查 默认情况下,idea会帮助我们检查不规范的拼写,标记出潜在的问题,例如: 可以通过设置idea,达到取消拼写检查的目的,如下: 强迫症的我表示很舒服。 参考࿱…

go build: mod: x509: certificate has expired or is not yet valid

go build: mod: x509: certificate has expired or is not yet valid 今天在编译go程序是出现报错: go: github.com/buger/jsonparserv0.0.0-20181115193947-bf1c66bbce23: get https://goproxy.io/github.com/buger/jsonparser/v/v0.0.0-20181115193947-bf1c66bb…

centos 8:vmware section %packages does not end with a %end

centos 8:vmware section %packages does not end with a %end vmware版本: 产品:vmware workstation 14 pro 版本:14.1.3 build-9474260centos镜像版本:centos-8.1.1911-x86_64 在安装中遇到了如下报错:…

linux: sysctl 命令

linux: sysctl 命令 sysctl 命令用于运行时配置系统内核参数。 常用参数:sysctl -a | -p | -w 关联man手册: man sysctl man sysctl.conf关联文件(目录): /proc/sys /etc/sysctl.conf1.查看运行时内核参数 以 ne…

openssl: error: storage size of ‘ctx’ isn’t known

openssl: error: storage size of ‘ctx’ isn’t known 问题 code: #include #include #include #include #include #include void hmac(const char *alg…

openssl: 兼容openssl1.1.0及旧版本

openssl: 兼容openssl1.1.0及旧版本 openssl 1.1.0 版本和低版本有很多接口不兼容问题,例如: openssl 1.1.0 版本中,很多 struct 是不透明的,不能在栈中直接声明变量,需要通过指定的函数来在堆中创建非透明的struct对…

linux:在非本机ip地址监听

linux:在非本机ip地址监听 最常见的场景是: 双机部署 nginx keepalived(vip virtual ip)实现高可用,对外提供的服务地址是 keepalived 配置的vip。 出于安全考虑,要求 nginx 只能在 vip 上监听服务端口…

xshell:保持终端连接状态(活动、不断开)

xshell:保持终端连接状态(活动、不断开) 基于安全原因,终端访问是受限的,我们希望终端长时间保持连接状态,此时可以利用 xshell 的功能: 选择会话 “属性->连接->保持活动状态” 中设置每…

golang: encoding/json包关于<>字符的转义

golang: encoding/json包关于&<>字符的转义 版本环境: $ go version go version go1.13.6 windows/amd64示例代码: package mainimport ("encoding/json""log" )func main() {object : struct {value string}{value:&q…
网站地图