DES算法

数据加密标准(英语:Data Encryption Standard,缩写为 DES)是一种对称密钥加密块密码算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。它基于使用56位密钥的对称算法。这个算法因为包含一些机密设计元素,相对短的密钥长度以及怀疑内含美国国家安全局(NSA)的后门而在开始时有争议,DES因此受到了强烈的学院派式的审查,并以此推动了现代的块密码及其密码分析的发展。                                                                                                                                                ---维基百科 

一、DES简介

1、DES:数据加密标准,是对称加密算法领域中的典型算法

2、特点:秘钥偏短(56位),生命周期短

3、JDK内部提供了算法的实现

二、相关代码

1、生成密钥

public static byte[] getKey() throws Exception

{

KeyGenerator keyGen = KeyGenerator.getInstance("DES");

keyGen.init(56);

SecretKey secretKey = keyGen.generateKey();

return secretKey.getEncoded();

}

2、DES加密

/**

* DES加密

* @param data 要加密的原始数据

* @param key  密钥

* @return 加密后的数据

* @throws Exception

*/

public static byte[] encrypt(byte[] data, byte[] key) throws Exception

{

SecretKey secretKey = new SecretKeySpec(key, "DES");

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

return cipher.doFinal(data);

}

3、DES解密

/**

* DES解密

* @param data 要解密的数据

* @param key  密钥

* @return 解密后的数据

* @throws Exception

*/

public static byte[] decrypt(byte[] data, byte[] key) throws Exception

{

SecretKey secretKey = new SecretKeySpec(key, "DES");

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

cipher.init(Cipher.DECRYPT_MODE, secretKey);

return cipher.doFinal(data);

}

4、字节转十六进制

/**

* 字节转十六进制

*

* @param resultBytes

*            输入源

* @return 十六进制字符串

*/

public static String fromBytesToHex(byte[] resultBytes) {

StringBuilder builder = new StringBuilder();

for (int i = 0; i < resultBytes.length; i++) {

if (Integer.toHexString(0xFF & resultBytes[i]).length() == 1) {

builder.append("0").append(Integer.toHexString(0xFF & resultBytes[i]));

} else {

builder.append(Integer.toHexString(0xFF & resultBytes[i]));

}

}

return builder.toString();

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 之前一直对加密相关的算法知之甚少,只知道类似DES、RSA等加密算法能对数据传输进行加密,且各种加密算法各有...
    Henryzhu阅读 3,048评论 0 14
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • 在开发应用过程中,客户端与服务端经常需要进行数据传输,涉及到重要隐私安全信息时,开发者自然会想到对其进行加密,即使...
    闲庭阅读 3,294评论 0 11
  • 姓名:于川皓 学号:16140210089 转载自:https://baike.baidu.com/item/RS...
    道无涯_cc76阅读 2,594评论 0 1
  • 早餐:一碗皮蛋瘦肉粥 午餐:半块豆腐,半碗豆角,一碗饭 晚餐: 运动:早起跳操半小时,骑行1小时到北大桥 感悟:有...
    影子3623253阅读 148评论 0 2