• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

DES 加解密的封装和 使用的例子

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 21次 | 进入软件测试论坛讨论

领测软件测试网
原创:[email protected] ,欢迎喜欢Java的网友加我



DES加密封装

package org.jtool.desutils;import java.security.SecureRandom;import javax.crypto.*;import javax.crypto.spec.DESKeySpec;public class DESEncrypt {    private byte[] desKey;    public DESEncrypt(byte[] desKey) {        this.desKey = desKey;    }    public byte[] doEncrypt(byte[] plainText) throws Exception {        //      DES算法要求有一个可信任的随机数源        SecureRandom sr = new SecureRandom();        byte rawKeyData[] = desKey;/* 用某种方法获得密匙数据 */        // 从原始密匙数据创建DESKeySpec对象        DESKeySpec dks = new DESKeySpec(rawKeyData);        // 创建一个密匙工厂,然后用它把DESKeySpec转换成        // 一个SecretKey对象        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");        SecretKey key = keyFactory.generateSecret(dks);        // Cipher对象实际完成加密操作        Cipher cipher = Cipher.getInstance("DES");        // 用密匙初始化Cipher对象        cipher.init(Cipher.ENCRYPT_MODE, key, sr);        // 现在,获取数据并加密        byte data[] = plainText;/* 用某种方法获取数据 */        // 正式执行加密操作        byte encryptedData[] = cipher.doFinal(data);        return encryptedData;    }}



DES解密封装

package org.jtool.desutils;import java.security.SecureRandom;import javax.crypto.*;import javax.crypto.spec.DESKeySpec;public class DESDecrypt {    private byte[] desKey;    public DESDecrypt(byte[] desKey) {        this.desKey = desKey;    }    public byte[] doDecrypt(byte[] encryptText) throws Exception {        //      DES算法要求有一个可信任的随机数源        SecureRandom sr = new SecureRandom();        byte rawKeyData[] = desKey; /* 用某种方法获取原始密匙数据 */        // 从原始密匙数据创建一个DESKeySpec对象        DESKeySpec dks = new DESKeySpec(rawKeyData);        // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成        // 一个SecretKey对象        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");        SecretKey key = keyFactory.generateSecret(dks);        // Cipher对象实际完成解密操作        Cipher cipher = Cipher.getInstance("DES");        // 用密匙初始化Cipher对象        cipher.init(Cipher.DECRYPT_MODE, key, sr);        // 现在,获取数据并解密        byte encryptedData[] = encryptText;/* 获得经过加密的数据 */        // 正式执行解密操作        byte decryptedData[] = cipher.doFinal(encryptedData);        return decryptedData;    }}



DES加解密使用样例

package org.jtool.desutils;/** *  */public class DESTest {    public static void main(String[] args) throws Exception {        String key = "ABCDEFGH";        String value = "AABBCCDDEE";        DESEncrypt desEncrypt = new DESEncrypt(key.getBytes());        byte[] encryptText = desEncrypt.doEncrypt(value.getBytes());        System.out.println("doEncrypt - " + toHexString(encryptText));        System.out.println("doEncrypt - " + new String(encryptText));        DESDecrypt desDecrypt = new DESDecrypt(key.getBytes());        byte[] decryptText = desDecrypt.doDecrypt(encryptText);        System.out.println("doDecrypt - " + new String(decryptText));        System.out.println("doDecrypt - " + toHexString(decryptText));    }    /**     * 16进制显示数据     *      * @param value 字节数组     * @return     */    public static String toHexString(byte[] value) {        String newString = "";        for (int i = 0; i < value.length; i++) {            byte b = value[i];            String str = Integer.toHexString(b);            if (str.length() > 2) {                str = str.substring(str.length() - 2);            }            if (str.length() < 2) {                str = "0" + str;            }            newString += str;        }        return newString.toUpperCase();    }}

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网