DES 双向加密算法 DESUtil
来源:互联网 发布:淘宝村淘收费标准 编辑:程序博客网 时间:2024/05/21 19:48
package com.xgh.encryption.algorithm.test;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import junit.framework.TestCase;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/*** * DES 双向加密算法 * @author xgh * */public class DESUtil extends TestCase{/*** * DES 双向加密算法 * @param content 待加密内容 * @param password 加密密钥 * @return */public byte[] testEncryptDES() throws Exception{ String content = "Hello,明天天气不错";String password = "123456";KeyGenerator kgen = KeyGenerator.getInstance("DES");kgen.init(128, new SecureRandom(password.getBytes()));SecretKey secretKey = kgen.generateKey();byte[] enCodeFormat = secretKey.getEncoded();SecretKeySpec key = new SecretKeySpec(enCodeFormat, "DES");Cipher cipher = Cipher.getInstance("DES");// 创建密码器byte[] byteContent = content.getBytes("UTF-8");cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化byte[] result = cipher.doFinal(byteContent);System.out.println("DES加密原数据:");System.out.println(content);System.out.println("DES加密后的Base64结果:");System.out.println(new BASE64Encoder().encode(result));System.out.println("------------------------------------");return result; // 加密}/*** * DES 双向加密算法 * @param content 待解密内容 * @param password 解密密钥 * @return */public byte[] testDecryptDES() throws Exception{String content = "2M8d6HQ0qtXqSSuL83ILs348ls6C8JlZnfu0UFgCkOw=";String password = "123456";byte[] contentbyte = new BASE64Decoder().decodeBuffer(content);KeyGenerator kgen = KeyGenerator.getInstance("DES");kgen.init(128, new SecureRandom(password.getBytes()));SecretKey secretKey = kgen.generateKey();byte[] enCodeFormat = secretKey.getEncoded();SecretKeySpec key = new SecretKeySpec(enCodeFormat, "DES");Cipher cipher = Cipher.getInstance("DES");// 创建密码器cipher.init(Cipher.DECRYPT_MODE, key);// 初始化byte[] result = cipher.doFinal(contentbyte);System.out.println("解密结果:");System.out.println(new String(result,"UTF-8"));System.out.println("------------------------------------");return result; // 加密}}
0 0
- DES 双向加密算法 DESUtil
- DES加密算法
- DES加密算法
- DES 加密算法
- DES加密算法
- DES加密算法
- DES 加密算法
- DES 加密算法
- DES加密算法
- DES加密算法
- DES加密算法
- DES加密算法
- DES加密算法
- DES加密算法
- DES加密算法
- DES加密算法
- des加密算法
- DES加密算法
- iOS原生网络请求使用(NSURL,NSURLRequest/NSMutableURLRequest,NSURLConnection)
- 手工焊接电磁炮控制板
- 多线程读取同一个内存数据会出错吗
- 黑马程序员--设计模式之单例模式
- sencha touch2.x 自动提示配置 eclipse spket jsb
- DES 双向加密算法 DESUtil
- nginx源码安装
- Oracle触发器
- Avoid passing null as the view root windowManager使用
- 黑马程序员——C语言基础---C语言的基础语法
- AES 双向加密算法 AESUtil
- [require.js]初识require.js
- 数组指针与指针数组
- Extjs5 Tab标签右键单击菜单