DES代码讲解

public class DESDemo {


/*
 * DES加密介绍
DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。DES加密算法出自IBM的研究,
后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少,因为DES使用56位密钥,以现代计算能力,
24小时内即可被破解。虽然如此,在某些简单应用中,我们还是可以使用DES加密算法,本文简单讲解DES的JAVA实现
。
注意:DES加密和解密过程中,大于等于8位
 */
	public static void main(String[] args) throws Exception {
            // 明文  
            String str = "hello des";  
            // 密码  
            String password = "12345678";  //大于等于8位
            String desc = DESUlits.doWork(str, password,0);  
            System.out.println("密文:" + desc);  
            // 解密  
            str = DESUlits.doWork(desc, password,1);  
            System.out.println("明文:" +str);  
	}
}


加密和解密的工具类

public class DESUlits {

	/** 
     * 加密解密接口 
     * @param data      数据 
     * @param password  加密解密密码 必须8位字节 
     * @param flag      加密解密标志 0:加密 ,1:解密 
     * @return 
     */  
    public static String doWork(String data, String password,int flag) {  
        try {  
        	//1.DES算法要求有一个可信任的随机数源
            SecureRandom random = new SecureRandom(); 
            //2.创建一个DESKeySpec对象
            DESKeySpec desKey = new DESKeySpec(password.getBytes()); 
            //3.创建一个密匙工厂
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            //4.将DESKeySpec对象转换成SecretKey对象
            SecretKey securekey = keyFactory.generateSecret(desKey); 
            //5.Cipher对象实际完成加密/解密操作
            Cipher cipher = Cipher.getInstance("DES");  
            //加密   
            if(flag == 0){    
            	//创建一个BASE64Encoder对象进行加密
            	sun.misc.BASE64Encoder base64encoder = new sun.misc.BASE64Encoder();
            	//用密匙初始化Cipher对象
                cipher.init(Cipher.ENCRYPT_MODE, securekey, random);  
                //进行加密操作
                return base64encoder.encode(cipher.doFinal(data.getBytes("UTF-8")));  
            }else{ 
            	//解密
            	创建一个BASE64Decoder对象进行解密
            	sun.misc.BASE64Decoder base64decoder = new sun.misc.BASE64Decoder();
            	
                byte[] encodeByte = base64decoder.decodeBuffer(data); 
                //用密匙初始化Cipher对象
                cipher.init(Cipher.DECRYPT_MODE, securekey, random); 
                //执行解密操作
                byte[] decoder = cipher.doFinal(encodeByte);  
                return new String(decoder,"UTF-8");  
            }  
              
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
// DES.cpp: implementation of the DES class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "codeDlg.h" #include "DES.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// DES::DES() { } DES::~DES() { } // initial permutation IP const static char IP_Table[64] = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; // final permutation IP^-1 const static char IPR_Table[64] = { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; // expansion operation matrix static const char E_Table[48] = { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }; // 32-bit permutation function P used on the output of the S-boxes const static char P_Table[32] = { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 }; // permuted choice table (key) const static char PC1_Table[56] = { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; // permuted choice key (table) const static char PC2_Table[48] = { 14, 17, 11, 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值