- 浏览: 996079 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (367)
- JavaScript (23)
- Java (60)
- Python (41)
- 其他 (36)
- SQL (4)
- 开发工具 (26)
- Linux (15)
- AJAX (6)
- Cache (3)
- 正则 (4)
- 架构 (9)
- 手机 (3)
- MySQL (4)
- Android (115)
- vps (1)
- 网站 (4)
- scale (3)
- 搜索引擎 (3)
- iPhone (2)
- hessian (1)
- hessdroid (1)
- 411 (1)
- jstat (1)
- gc (1)
- gallery (1)
- 惯性 (1)
- eclipse (1)
- mac wget error (1)
- miui file explorer 无用 解决办法 (1)
- vim (1)
最新评论
-
qingyezhangluo:
哎。楼主您既然是分享代码的为什么要加密的呢?而且问你密码还不回 ...
android应用换皮肤(转) -
MagicError:
kavoe 写道下载文件有密码。。。。
http抓包工具 -
knightdf:
我先试下再来
JAVA的RAS加密例子 -
kavoe:
下载文件有密码。。。。
http抓包工具 -
changanfounder:
hmc1985 写道setCallbackDuringFlin ...
android gallery滑动惯性问题
接下来我们介绍对称加密算法,最常用的莫过于DES数据加密算法。
DES
DES-Data Encryption Standard,即数据加密算法。是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位。
通过java代码实现如下:Coder类见 Java加密技术(一)
延续上一个类的实现,我们通过MD5以及SHA对字符串加密生成密钥,这是比较常见的密钥生成方式。
再给出一个测试类:
得到的输出内容如下:
由控制台得到的输出,我们能够比对加密、解密后结果一致。这是一种简单的加密解密方式,只有一个密钥。
其实DES有很多同胞兄弟,如DESede(TripleDES)、AES、Blowfish、RC2、RC4(ARCFOUR)。这里就不过多阐述了,大同小异,只要换掉ALGORITHM换成对应的值,同时做一个代码替换SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);就可以了,此外就是密钥长度不同了。
DES
DES-Data Encryption Standard,即数据加密算法。是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位。
通过java代码实现如下:Coder类见 Java加密技术(一)
- import java.security.Key;
- import java.security.SecureRandom;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.DESKeySpec;
- /**
- * DES安全编码组件
- *
- * <pre>
- * 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)
- * DES key size must be equal to 56
- * DESede(TripleDES) key size must be equal to 112 or 168
- * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
- * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
- * RC2 key size must be between 40 and 1024 bits
- * RC4(ARCFOUR) key size must be between 40 and 1024 bits
- * 具体内容 需要关注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html
- * </pre>
- *
- * @author 梁栋
- * @version 1.0
- * @since 1.0
- */
- public abstract class DESCoder extends Coder {
- /**
- * ALGORITHM 算法 <br>
- * 可替换为以下任意一种算法,同时key值的size相应改变。
- *
- * <pre>
- * DES key size must be equal to 56
- * DESede(TripleDES) key size must be equal to 112 or 168
- * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
- * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
- * RC2 key size must be between 40 and 1024 bits
- * RC4(ARCFOUR) key size must be between 40 and 1024 bits
- * </pre>
- *
- * 在Key toKey(byte[] key)方法中使用下述代码
- * <code>SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);</code> 替换
- * <code>
- * DESKeySpec dks = new DESKeySpec(key);
- * SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
- * SecretKey secretKey = keyFactory.generateSecret(dks);
- * </code>
- */
- public static final String ALGORITHM = "DES";
- /**
- * 转换密钥<br>
- *
- * @param key
- * @return
- * @throws Exception
- */
- private static Key toKey(byte[] key) throws Exception {
- DESKeySpec dks = new DESKeySpec(key);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
- SecretKey secretKey = keyFactory.generateSecret(dks);
- // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码
- // SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
- return secretKey;
- }
- /**
- * 解密
- *
- * @param data
- * @param key
- * @return
- * @throws Exception
- */
- public static byte[] decrypt(byte[] data, String key) throws Exception {
- Key k = toKey(decryptBASE64(key));
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- cipher.init(Cipher.DECRYPT_MODE, k);
- return cipher.doFinal(data);
- }
- /**
- * 加密
- *
- * @param data
- * @param key
- * @return
- * @throws Exception
- */
- public static byte[] encrypt(byte[] data, String key) throws Exception {
- Key k = toKey(decryptBASE64(key));
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- cipher.init(Cipher.ENCRYPT_MODE, k);
- return cipher.doFinal(data);
- }
- /**
- * 生成密钥
- *
- * @return
- * @throws Exception
- */
- public static String initKey() throws Exception {
- return initKey(null);
- }
- /**
- * 生成密钥
- *
- * @param seed
- * @return
- * @throws Exception
- */
- public static String initKey(String seed) throws Exception {
- SecureRandom secureRandom = null;
- if (seed != null) {
- secureRandom = new SecureRandom(decryptBASE64(seed));
- } else {
- secureRandom = new SecureRandom();
- }
- KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
- kg.init(secureRandom);
- SecretKey secretKey = kg.generateKey();
- return encryptBASE64(secretKey.getEncoded());
- }
- }
import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; /** * DES安全编码组件 * * <pre> * 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR) * DES key size must be equal to 56 * DESede(TripleDES) key size must be equal to 112 or 168 * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive) * RC2 key size must be between 40 and 1024 bits * RC4(ARCFOUR) key size must be between 40 and 1024 bits * 具体内容 需要关注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html * </pre> * * @author 梁栋 * @version 1.0 * @since 1.0 */ public abstract class DESCoder extends Coder { /** * ALGORITHM 算法 <br> * 可替换为以下任意一种算法,同时key值的size相应改变。 * * <pre> * DES key size must be equal to 56 * DESede(TripleDES) key size must be equal to 112 or 168 * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive) * RC2 key size must be between 40 and 1024 bits * RC4(ARCFOUR) key size must be between 40 and 1024 bits * </pre> * * 在Key toKey(byte[] key)方法中使用下述代码 * <code>SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);</code> 替换 * <code> * DESKeySpec dks = new DESKeySpec(key); * SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); * SecretKey secretKey = keyFactory.generateSecret(dks); * </code> */ public static final String ALGORITHM = "DES"; /** * 转换密钥<br> * * @param key * @return * @throws Exception */ private static Key toKey(byte[] key) throws Exception { DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey secretKey = keyFactory.generateSecret(dks); // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码 // SecretKey secretKey = new SecretKeySpec(key, ALGORITHM); return secretKey; } /** * 解密 * * @param data * @param key * @return * @throws Exception */ public static byte[] decrypt(byte[] data, String key) throws Exception { Key k = toKey(decryptBASE64(key)); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, k); return cipher.doFinal(data); } /** * 加密 * * @param data * @param key * @return * @throws Exception */ public static byte[] encrypt(byte[] data, String key) throws Exception { Key k = toKey(decryptBASE64(key)); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, k); return cipher.doFinal(data); } /** * 生成密钥 * * @return * @throws Exception */ public static String initKey() throws Exception { return initKey(null); } /** * 生成密钥 * * @param seed * @return * @throws Exception */ public static String initKey(String seed) throws Exception { SecureRandom secureRandom = null; if (seed != null) { secureRandom = new SecureRandom(decryptBASE64(seed)); } else { secureRandom = new SecureRandom(); } KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM); kg.init(secureRandom); SecretKey secretKey = kg.generateKey(); return encryptBASE64(secretKey.getEncoded()); } }
延续上一个类的实现,我们通过MD5以及SHA对字符串加密生成密钥,这是比较常见的密钥生成方式。
再给出一个测试类:
- import static org.junit.Assert.*;
- import org.junit.Test;
- /**
- *
- * @author 梁栋
- * @version 1.0
- * @since 1.0
- */
- public class DESCoderTest {
- @Test
- public void test() throws Exception {
- String inputStr = "DES";
- String key = DESCoder.initKey();
- System.err.println("原文:\t" + inputStr);
- System.err.println("密钥:\t" + key);
- byte[] inputData = inputStr.getBytes();
- inputData = DESCoder.encrypt(inputData, key);
- System.err.println("加密后:\t" + DESCoder.encryptBASE64(inputData));
- byte[] outputData = DESCoder.decrypt(inputData, key);
- String outputStr = new String(outputData);
- System.err.println("解密后:\t" + outputStr);
- assertEquals(inputStr, outputStr);
- }
- }
import static org.junit.Assert.*; import org.junit.Test; /** * * @author 梁栋 * @version 1.0 * @since 1.0 */ public class DESCoderTest { @Test public void test() throws Exception { String inputStr = "DES"; String key = DESCoder.initKey(); System.err.println("原文:\t" + inputStr); System.err.println("密钥:\t" + key); byte[] inputData = inputStr.getBytes(); inputData = DESCoder.encrypt(inputData, key); System.err.println("加密后:\t" + DESCoder.encryptBASE64(inputData)); byte[] outputData = DESCoder.decrypt(inputData, key); String outputStr = new String(outputData); System.err.println("解密后:\t" + outputStr); assertEquals(inputStr, outputStr); } }
得到的输出内容如下:
原文: DES 密钥: f3wEtRrV6q0= 加密后: C6qe9oNIzRY= 解密后: DES
由控制台得到的输出,我们能够比对加密、解密后结果一致。这是一种简单的加密解密方式,只有一个密钥。
其实DES有很多同胞兄弟,如DESede(TripleDES)、AES、Blowfish、RC2、RC4(ARCFOUR)。这里就不过多阐述了,大同小异,只要换掉ALGORITHM换成对应的值,同时做一个代码替换SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);就可以了,此外就是密钥长度不同了。
- /**
- * DES key size must be equal to 56
- * DESede(TripleDES) key size must be equal to 112 or 168
- * AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
- * Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
- * RC2 key size must be between 40 and 1024 bits
- * RC4(ARCFOUR) key size must be between 40 and 1024 bits
- **/
发表评论
-
android屏幕适配
2012-11-16 17:24 2145屏幕适配一直是一个让人头疼的问题,论坛上讨论这个问题的最后也 ... -
禁止Eclipse中xml文件Run as的XSL Transformation生成out.xml以方便Android应用开发
2012-08-26 21:38 2585可以在Eclipse里面配置,菜单Windows->P ... -
android 自带的主题 theme 的使用
2012-08-25 23:21 2334在android的sdk 安装目录data\r ... -
android开发之gallery 实现滚动一张且短距离滑动实现滚动
2011-12-19 18:50 1886首先gallery的特点就不用多说了吧,惯性滚动、半屏翻页,但 ... -
使用Jstat监控gc情况(收藏)
2011-12-19 13:22 1803性能测试过程中,我们 ... -
eclipse生成javadoc乱码解决
2011-10-24 09:37 1233eclipse在生成javadoc的时候出现乱码,是因为 ... -
java中在静态方法或变量中动态获取当前类的类名
2011-10-14 11:20 3648java中在静态方法中动态获取当前类的类名或者动态获取当前类的 ... -
飞鸽传书实现原理
2011-10-13 11:35 3121飞鸽传书的实现原理: (1)最关键的是局域网用户列表的 ... -
Android开发——利用Cursor+CursorAdapter实现界面实时更新(转)
2011-09-20 15:47 3039好久没有更新博客了 ... -
Nginx使用反向代理时 Hessian 的 411 错误解决方案【转】
2011-09-15 13:48 2327问题描述: 用 Hessian 实现 web se ... -
"Copy" did not complete normally. Please see the log for more information.
2011-05-06 17:19 4003在用android日志的时候老是弹出一个窗口,内容 ... -
jdk api下载地址备份
2011-02-15 10:58 1539中文jdk6的api: JDK6 API 中文版 H ... -
JAVA NIO 简介(转)
2010-12-09 13:03 1022http://www.iteye.com/topic/8344 ... -
netty的资料
2010-10-31 16:06 1255http://hornetq.sourceforge.net/ ... -
Java程序发邮件小例子(转载收藏)
2010-10-27 21:25 1531今天试了一个Java写的发邮件小例子,需要的jar包有 ... -
使用Netty 构造一个异步的httpclient
2010-09-21 00:35 7800原文地址:http://dev.firnow.com/co ... -
Netty使用初步
2010-09-21 00:26 13860收藏自:http://www.kafk ... -
集群环境下SESSION处理(转)
2010-09-08 15:24 1806本文转自:http://blog.csdn.net/l ... -
Java集合的Stack、Queue、Map的遍历
2010-09-08 13:00 1390在集合操作中,常常 ... -
<转>,防止刷新/后退引起的重复提交问题的Java Token代码,非Struts
2009-12-10 13:31 1875贴子转自http://hi.baidu.com/bobylou ...
相关推荐
java加密技术 java加密技术 java加密技术
Java 加密技术汇总
Java加密技术(二)——对称加密算法DES&AES Java加密技术(三)——PBE算法 Java加密技术(四)——非对称加密算法RSA Java加密技术(五)——非对称加密算法的由来DH Java加密技术(六)——数字签名算法DSA Java...
Java加密技术
java加密技术大全,包括了目前为止所有的java加密技术,MD5,AES,DES,RSA等的实现代码
JAVA加密技术.docx
Java加密技术(一) 关键字: java 加密基础, base64, md5, mac, sha, 单向加密 。。。。。。。。。。。。。。
Java加密技术(一),相当有用的加密技术,值得学习参考!希望对你有点帮助!
Java加密技术全集
java加密技术研究
java加密技术 与单向加密算法MD5&SHA;&MAC; 对称加密算法DES&AES; PBE 算法
snowolf-Java 加密技术
java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密技术java加密...
写的不错的一个保护java源代码的加密技术教程,很实用!
Java加密技术 pdf版 加密解密,曾经是我一个毕业设计的重要组件。在工作了多年以后回想当时那个加密、解密算法,实在是太单纯了。 言归正传,这里我们主要描述Java已经实现的一些加密解密算法,最后介绍数字证书
本文详细介绍了Java中的加密技术,包括对称加密、非对称加密和哈希算法等,并通过实例演示了如何在实际项目中应用这些加密技术。在对称加密部分,介绍了AES加密算法及其在Java中的实现方法,包括加密和解密过程。在...
全书包含3个部分,基础篇对Java企业级应用的安全知识、密码学核心知识、与Java加密相关的API和通过权限文件加强系统安全方面的知识进行了全面的介绍;实践篇不仅对电子邮件传输算法、消息摘要算法、对称加密算法、非...
java 加密
Java加密技术(三),相当有用的加密技术(三),值得学习参考!希望对你有点帮助!