数据摘要加密解密(三)

今天主要实现DES加密及3DES加密类的实现,直接见程序

IEncrypt接口类:

1 package com.xqrj.security.encrypt;
2
3 import com.xqrj.security.ISecurity;
4
5 public interface IEncrypt extends ISecurity {
6
7 }

DesEncryptImpl加密抽象类,主要实现在此抽象类中

 1 package com.xqrj.security.encrypt;
2
3 import java.security.SecureRandom;
4
5 import javax.crypto.Cipher;
6 import javax.crypto.SecretKey;
7 import javax.crypto.SecretKeyFactory;
8 import javax.crypto.spec.DESKeySpec;
9 import javax.crypto.spec.SecretKeySpec;
10
11 import com.xqrj.util.PublicUtil;
12
13 public abstract class DesEncryptImpl implements IEncrypt {
14 protected String algorithm = "DES";
15 protected int encodeMode = 1;
16 protected byte[] key;
17 protected byte[] otherData = "DES/ECB/NoPadding".getBytes();
18
19 public byte[] buildData(byte[] buff) throws SecurityException {
20 try {
21 ////DES算法要求有一个可信任的随机数源
22 // SecureRandom secureRandom = new SecureRandom();
23 ////从原始密匙数据创建DESKeySpec对象//获得DES密钥
24 // DESKeySpec desKeySpec = new DESKeySpec(key);
25 ////创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象//获得DES加密密钥工厂
26 // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
27 ////生成加密密钥
28 // SecretKey secureKey = keyFactory.generateSecret(desKeySpec);
29 SecretKey secureKey = new SecretKeySpec(key,algorithm);
30 //Cipher对象实际完成加密操作
31 Cipher cipher = Cipher.getInstance(new String(otherData));
32 //用密匙初始化Cipher对象
33 cipher.init(Cipher.ENCRYPT_MODE, secureKey);
34 //执行加密操作
35 byte[] data = cipher.doFinal(getNoPaddingBytes(buff));
36
37 switch(encodeMode) {
38 case 1: //BASE64
39 return PublicUtil.getBase64Encode(data).getBytes();
40 case 2: //HEX
41 return new String(PublicUtil.bytesToHex(data)).getBytes();
42 default: //BASE64
43 return PublicUtil.getBase64Encode(data).getBytes();
44 }
45 } catch(SecurityException e) {
46 throw new SecurityException(e.getMessage());
47 } catch(Exception e) {
48 e.printStackTrace();
49 throw new SecurityException("DesEncrypt对数据加密时出错!");
50 }
51 }
52
53 public void setAlgorithm(String algorithm) {
54 this.algorithm = algorithm;
55 }
56 public void setEncodeMode(int encodeMode) {
57 this.encodeMode = encodeMode;
58 }
59 public void setKey(byte[] key) {
60 this.key = key;
61 }
62 public void setOther(byte[] otherData) {
63 this.otherData = otherData;
64 }
65 /**
66 * 获取数据填充后的数据
67 * VC不足八的倍数后面添0,JAVA为不填充,所以补足8的倍数后面添0
68 * @param data 需要填充的数据
69 * @return void
70 */
71 private byte[] getNoPaddingBytes(byte[] data) {
72 int len = data.length;
73 int temp = len%8;
74 if (temp > 0) {
75 //取得总长度
76 len = len + (8-temp);
77 }
78 byte[] res = new byte[len];
79
80 System.arraycopy(data, 0, res, 0, data.length);
81 return res;
82 }
83 }

其中有一私有方法:getNoPaddingBytes,此方法是将不足位数的数据后补0,如果与VC进行通讯,需要此转换,我现在默认实现是补位的。

DesEncrypt类:无具体代码,只是提供用于调用

1 package com.xqrj.security.encrypt;
2
3 public class DesEncrypt extends DesEncryptImpl {
4
5 }

AES加密调用方式与DES相同(JDK已封装),只不过传入的参数不同,所以只实现DES的方式,同样AES加密也可以用,呵,在测试类里有体现

SecurityTest测试类:

 1 package com.xqrj.security;
2
3 import java.security.Security;
4
5 import com.xqrj.security.decrypt.DesDecrypt;
6 import com.xqrj.security.decrypt.RsaPrivateDecrypt;
7 import com.xqrj.security.decrypt.RsaPublicDecrypt;
8 import com.xqrj.security.digest.DigestDigest;
9 import com.xqrj.security.encrypt.DesEncrypt;
10 import com.xqrj.security.encrypt.RsaPrivateEncrypt;
11 import com.xqrj.security.encrypt.RsaPublicEncrypt;
12
13 public class SecurityTest {
14 public static void main(String[] args) {
15 //需要加密的数据
16 byte[] buff = "abcdefgh12345678".getBytes();
17 //密钥KEY
18 String key = "";
19 //使用何种算法
20 String algorithm = "";
21 //其它数据
22 String other = "";
23 //数据编码(1:BASE64 2:HEX)
24 int encodeMode = 1;
25 //结果数据
26 byte[] resBuff = null;
27 try {
28 //*********************************************************************************
29 //摘要测试
30 DigestDigest digestEncrypt = new DigestDigest();
31 //MD2、MD5用HEX进行编码
32 digestEncrypt.setAlgorithm("MD2");
33 digestEncrypt.setEncodeMode(2);
34 resBuff = digestEncrypt.buildData(buff);
35 System.out.println("生成的MD2摘要数据:"+new String(resBuff));
36 digestEncrypt.setAlgorithm("MD5");
37 resBuff = digestEncrypt.buildData(buff);
38 System.out.println("生成的MD5摘要数据:"+new String(resBuff));
39 //SHA用BASE进行编码
40 digestEncrypt.setAlgorithm("SHA");
41 digestEncrypt.setEncodeMode(1);
42 resBuff = digestEncrypt.buildData(buff);
43 System.out.println("生成的SHA-1摘要数据:"+new String(resBuff));
44 digestEncrypt.setAlgorithm("SHA-256");
45 resBuff = digestEncrypt.buildData(buff);
46 System.out.println("生成的SHA-256摘要数据:"+new String(resBuff));
47 digestEncrypt.setAlgorithm("SHA-384");
48 resBuff = digestEncrypt.buildData(buff);
49 System.out.println("生成的SHA-384摘要数据:"+new String(resBuff));
50 digestEncrypt.setAlgorithm("SHA-512");
51 resBuff = digestEncrypt.buildData(buff);
52 System.out.println("生成的SHA-512摘要数据:"+new String(resBuff));
53 //*********************************************************************************
54 //DES测试(加密)
55 //添加新安全算法,如果用JCE就要把它添加进去
56 Security.addProvider(new com.sun.crypto.provider.SunJCE());
57 DesEncrypt desEncrypt = new DesEncrypt();
58 key = "11111111";
59 desEncrypt.setAlgorithm("DES");
60 desEncrypt.setKey(key.getBytes());
61 desEncrypt.setOther("DES/ECB/NoPadding".getBytes());
62 desEncrypt.setEncodeMode(2);
63 resBuff = desEncrypt.buildData(buff);
64 System.out.println("DES加密后的数据:"+new String(resBuff));
65 //*********************************************************************************
66 //3DES测试(加密)
67 key = "111111112222222211111111";
68 desEncrypt.setAlgorithm("DESede");
69 desEncrypt.setKey(key.getBytes());
70 desEncrypt.setOther("DESede/ECB/NoPadding".getBytes());
71 desEncrypt.setEncodeMode(2);
72 resBuff = desEncrypt.buildData(buff);
73 System.out.println("3DES加密后的数据:"+new String(resBuff));
74 //*********************************************************************************
75 //AES测试(加密)
76 key = "1111111122222222";
77 desEncrypt.setAlgorithm("AES");
78 desEncrypt.setKey(key.getBytes());
79 desEncrypt.setOther("AES/ECB/NoPadding".getBytes());
80 desEncrypt.setEncodeMode(2);
81 resBuff = desEncrypt.buildData(buff);
82 System.out.println("AES加密后的数据:"+new String(resBuff));
83 //*********************************************************************************
84 } catch(Exception e) {
85 e.printStackTrace();
86 }
87 }
88 }

如需与VC进行加密解密,VC程序请参考:http://www.cnblogs.com/erwin/archive/2009/04/14/1435288.html

以上JAVA代码与VC代码已进行过测试,DES及3DES加密后的数据完全可以对应上(源数据中不可以有中文)

转载于:https://www.cnblogs.com/ynjxxk/archive/2012/03/29/2423204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值