您的位置:首页 > 其它

数据摘要加密解密(三)

2012-03-29 13:25 274 查看
今天主要实现DES加密及3DES加密类的实现,直接见程序

IEncrypt接口类:

package com.xqrj.security.encrypt;

import com.xqrj.security.ISecurity;

public interface IEncrypt extends ISecurity {

}


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

package com.xqrj.security.encrypt;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

import com.xqrj.util.PublicUtil;

public abstract class DesEncryptImpl implements IEncrypt {
protected String algorithm = "DES";
protected int encodeMode = 1;
protected byte[] key;
protected byte[] otherData = "DES/ECB/NoPadding".getBytes();

public byte[] buildData(byte[] buff) throws SecurityException {
try {
////DES算法要求有一个可信任的随机数源
//            SecureRandom secureRandom = new SecureRandom();
////从原始密匙数据创建DESKeySpec对象//获得DES密钥
//            DESKeySpec desKeySpec = new DESKeySpec(key);
////创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象//获得DES加密密钥工厂
//            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
////生成加密密钥
//            SecretKey secureKey = keyFactory.generateSecret(desKeySpec);
SecretKey secureKey = new SecretKeySpec(key,algorithm);
//Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(new String(otherData));
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, secureKey);
//执行加密操作
byte[] data = cipher.doFinal(getNoPaddingBytes(buff));

switch(encodeMode) {
case 1:   //BASE64
return PublicUtil.getBase64Encode(data).getBytes();
case 2:   //HEX
return new String(PublicUtil.bytesToHex(data)).getBytes();
default:  //BASE64
return PublicUtil.getBase64Encode(data).getBytes();
}
} catch(SecurityException e) {
throw new SecurityException(e.getMessage());
} catch(Exception e) {
e.printStackTrace();
throw new SecurityException("DesEncrypt对数据加密时出错!");
}
}

public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
public void setEncodeMode(int encodeMode) {
this.encodeMode = encodeMode;
}
public void setKey(byte[] key) {
this.key = key;
}
public void setOther(byte[] otherData) {
this.otherData = otherData;
}
/**
* 获取数据填充后的数据
* VC不足八的倍数后面添0,JAVA为不填充,所以补足8的倍数后面添0
* @param data 需要填充的数据
* @return void
*/
private byte[] getNoPaddingBytes(byte[] data) {
int len = data.length;
int temp = len%8;
if (temp > 0) {
//取得总长度
len = len + (8-temp);
}
byte[] res = new byte[len];

System.arraycopy(data, 0, res, 0, data.length);
return res;
}
}


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

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

package com.xqrj.security.encrypt;

public class DesEncrypt extends DesEncryptImpl {

}


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

SecurityTest测试类:

package com.xqrj.security;

import java.security.Security;

import com.xqrj.security.decrypt.DesDecrypt;
import com.xqrj.security.decrypt.RsaPrivateDecrypt;
import com.xqrj.security.decrypt.RsaPublicDecrypt;
import com.xqrj.security.digest.DigestDigest;
import com.xqrj.security.encrypt.DesEncrypt;
import com.xqrj.security.encrypt.RsaPrivateEncrypt;
import com.xqrj.security.encrypt.RsaPublicEncrypt;

public class SecurityTest {
public static void main(String[] args) {
//需要加密的数据
byte[] buff = "abcdefgh12345678".getBytes();
//密钥KEY
String key = "";
//使用何种算法
String algorithm = "";
//其它数据
String other = "";
//数据编码(1:BASE64 2:HEX)
int encodeMode = 1;
//结果数据
byte[] resBuff = null;
try {
//*********************************************************************************
//摘要测试
DigestDigest digestEncrypt = new DigestDigest();
//MD2、MD5用HEX进行编码
digestEncrypt.setAlgorithm("MD2");
digestEncrypt.setEncodeMode(2);
resBuff = digestEncrypt.buildData(buff);
System.out.println("生成的MD2摘要数据:"+new String(resBuff));
digestEncrypt.setAlgorithm("MD5");
resBuff = digestEncrypt.buildData(buff);
System.out.println("生成的MD5摘要数据:"+new String(resBuff));
//SHA用BASE进行编码
digestEncrypt.setAlgorithm("SHA");
digestEncrypt.setEncodeMode(1);
resBuff = digestEncrypt.buildData(buff);
System.out.println("生成的SHA-1摘要数据:"+new String(resBuff));
digestEncrypt.setAlgorithm("SHA-256");
resBuff = digestEncrypt.buildData(buff);
System.out.println("生成的SHA-256摘要数据:"+new String(resBuff));
digestEncrypt.setAlgorithm("SHA-384");
resBuff = digestEncrypt.buildData(buff);
System.out.println("生成的SHA-384摘要数据:"+new String(resBuff));
digestEncrypt.setAlgorithm("SHA-512");
resBuff = digestEncrypt.buildData(buff);
System.out.println("生成的SHA-512摘要数据:"+new String(resBuff));
//*********************************************************************************
//DES测试(加密)
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
DesEncrypt desEncrypt = new DesEncrypt();
key = "11111111";
desEncrypt.setAlgorithm("DES");
desEncrypt.setKey(key.getBytes());
desEncrypt.setOther("DES/ECB/NoPadding".getBytes());
desEncrypt.setEncodeMode(2);
resBuff = desEncrypt.buildData(buff);
System.out.println("DES加密后的数据:"+new String(resBuff));
//*********************************************************************************
//3DES测试(加密)
key = "111111112222222211111111";
desEncrypt.setAlgorithm("DESede");
desEncrypt.setKey(key.getBytes());
desEncrypt.setOther("DESede/ECB/NoPadding".getBytes());
desEncrypt.setEncodeMode(2);
resBuff = desEncrypt.buildData(buff);
System.out.println("3DES加密后的数据:"+new String(resBuff));
//*********************************************************************************
//AES测试(加密)
key = "1111111122222222";
desEncrypt.setAlgorithm("AES");
desEncrypt.setKey(key.getBytes());
desEncrypt.setOther("AES/ECB/NoPadding".getBytes());
desEncrypt.setEncodeMode(2);
resBuff = desEncrypt.buildData(buff);
System.out.println("AES加密后的数据:"+new String(resBuff));
//*********************************************************************************
} catch(Exception e) {
e.printStackTrace();
}
}
}


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

以上JAVA代码与VC代码已进行过测试,DES及3DES加密后的数据完全可以对应上(源数据中不可以有中文)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: