import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
public class DES {
/**
* @param args
*/
private static String strDefaultKey = "national";
private Cipher encryptCipher = null;
private Cipher decryptCipher = null;
/**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
*
* @param strIn
* 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
* 默认构造方法,使用默认密钥
*
* @throws Exception
*/
public DES() throws Exception {
this(strDefaultKey);
}
/**
* 指定密钥构造方法
*
* @param strKey
* 指定的密钥
* @throws Exception
*/
public DES(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/**
* 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/**
* 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
/**
* 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/**
* 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
}
分享到:
相关推荐
DSC算法实现以及需要注意的问题,第一次
vesa dsc压缩标准文档(所有版本)和C model源码, 包括如下内容: 版本v1.1 版本v1.2 版本v1.2a 版本v1.2b 最新版C model
“A Fast Point Clouds Registration Algorithm for Laser Scanners”论文复现的KeyPointsAnd3DSC算法,可用来实现点云配准。参考博客链接:https://blog.csdn.net/weixin_43896283/article/details/133840198
1.版本:matlab2019a,不会运行可私信 2.领域:【数据聚类】 3.内容:DSC算法的Matlab实现 4.适合人群:本科,硕士等教研学习使用
介绍几种压缩算法及《笨笨数据压缩教程》,非常好的教程,通俗易明!!!
DSC12887源代码 C语言的,适用于单片机C51系统 DOC文件
该软件将 DSC 子空间聚类算法 [arXiv:1706.03860] 应用于人脸聚类问题。 该代码使用 Extended Yale B 数据集,该数据集包含 38 个个体在正面视图和不同照明条件下的每一个的 64 张图像。 评论: 1- 如果您在研究/...
labview2017dsc,LabVIEW数据记录和监控(DSC)模块,使用工具连接传统可编程逻辑控制器(PLC)与可编程自动化控制器(PAC),将数据记录至数据库中、管理警报与事件以及创建人机界面(HMI)。
需要labview的DSC模块的,里面有modbus功能,需要的可自行下载。前提是安装过2018款的labview。
几个点云配准的算法:点云配准PFH、FPFH、icp、NDT、3Dsc:粗配准算法,并计算出误差 几个点云配准的算法:点云配准PFH、FPFH、icp、NDT、3Dsc:粗配准算法,并计算出误差
无线传感网络中基于DSC 的记忆式算法研究
VESA Display Stream Compression,HDMI 2.1传输8K时采用的图像浅压缩标准
EDK2 dsc文件格式的相关编写规范和技术指导,有助于对dsc文件的认识和使用
日立sf3 dsc 3a门机调试.pdf
This document describes the EDK II Platform Description file (DSC) format. The EDK Build Tools are included as part of the EDK II compatibility package. In order to use EDK II Modules or the EDK II ...
LabVIEW 8.6 DSC ModuleLabVIEW 8.6 DSC ModuleLabVIEW 8.6 DSC ModuleLabVIEW 8.6 DSC Module
Purpose . . . . . . . . . ....Summary ....Intellectual Property ....Trademarks ....Patents ....Support for this Standard....Acknowledgments....Revision History ....Section 1 Introduction....1.1 Document Organization ....
基于词语权重的改进DSC中文网页去重算法,屠辉,刘刚,本文在Broder等人提出的DSC网页去重算法基础上考虑了网页内容和文本结构信息,在原算法中加入了基于词频,位置等的词语权重统计,提
DSC(Data Service Center)是用于管理DTU、与DTU进行数据收发的的服务软件,它需要通过开发包中动态库wcomm_dll.dll来完成和DTU之间的通讯。 动态库wcomm_dll.dll包括了和DTU通讯所需要的全部API函数,包括服务的...
利用PCL开源库编写代码3DSC+ICP算法实现点云高精度配准,并计算配准误差!基于PCL库版本1.9!