DES加密模板,和C#des加密
来源:互联网 发布:全球第一大社交网络 编辑:程序博客网 时间:2024/05/18 03:02
第一种
package
com.afreon.util;
import
java.io.IOException;
import
java.security.SecureRandom;
import
javax.crypto.Cipher;
import
javax.crypto.SecretKey;
import
javax.crypto.SecretKeyFactory;
import
javax.crypto.spec.DESKeySpec;
import
sun.misc.BASE64Decoder;
import
sun.misc.BASE64Encoder;
public
class
DesUtil {
private
final
static
String DES =
"DES"
;
public
static
void
main(String[] args)
throws
Exception {
String data =
"极限挑战"
;
String key =
"sdfdsdfdsfs"
;
System.err.println(encrypt(data, key));
System.err.println(decrypt(encrypt(data, key), key));
}
/**
* Description 根据键值进行加密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
public
static
String encrypt(String data, String key)
throws
Exception {
byte
[] bt = encrypt(data.getBytes(), key.getBytes());
String strs =
new
BASE64Encoder().encode(bt);
return
strs;
}
/**
* Description 根据键值进行解密
* @param data
* @param key 加密键byte数组
* @return
* @throws IOException
* @throws Exception
*/
public
static
String decrypt(String data, String key)
throws
IOException,
Exception {
if
(data ==
null
)
return
null
;
BASE64Decoder decoder =
new
BASE64Decoder();
byte
[] buf = decoder.decodeBuffer(data);
byte
[] bt = decrypt(buf,key.getBytes());
return
new
String(bt);
}
/**
* Description 根据键值进行加密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private
static
byte
[] encrypt(
byte
[] data,
byte
[] key)
throws
Exception {
// 生成一个可信任的随机数源
SecureRandom sr =
new
SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks =
new
DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return
cipher.doFinal(data);
}
/**
* Description 根据键值进行解密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private
static
byte
[] decrypt(
byte
[] data,
byte
[] key)
throws
Exception {
// 生成一个可信任的随机数源
SecureRandom sr =
new
SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks =
new
DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return
cipher.doFinal(data);
}
}
http:
//www.yoodb.com/article/display/266
第二种
- import java.security.Security;
- import java.util.Arrays;
- import javax.crypto.Cipher;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
- import org.bouncycastle.util.encoders.Hex;
- /***
- * 可以和c#加密结果相同 需要到两个jar包:
- * bcprov-jdk15on-148.jar
- * bcprov-ext-jdk15on-148.jar 在D:/jar/
- *
- * @ClassName Test3DES_2
- * @author 张月
- * @date 2014年4月16日
- */
- public class Test3DES_2 {
- public static void main(String[] args) throws Exception {
- String key = "30313233343536373938373635343332";
- String shuju = "0088757365722020202020202020202020202020202020202020202020202013111816000001123456bc445951f5f60762f951146ece3bed0fbde2c4198f7dc43018f1d5f2c807c47f26FFD30AAF8E3823";
- String jiamiResult = encryptStr(shuju,key);
- System.out.println("加密后:"+jiamiResult);//【与C# CBC 零填充模式 互相加解密】
- System.out.println("解密后:"+decryptStr(jiamiResult,key));
- }
- /**
- * 3DES加密 (亦称为:DESede加密)
- *
- * CBC模式
- * 填充模式:零字节填充 ZeroBytePadding
- *
- * @Method: encrypt3Str
- * @param @param shuju
- * @param @param key
- * @param @return
- * @param @throws Exception
- * @return String
- * @throws
- */
- public static String encryptStr(String shuju,String key) throws Exception {
- String result = "";
- try {
- Security.addProvider(new BouncyCastleProvider());
- byte[] bKey = Hex.decode(key);//十六进制转换成字节数据
- byte[] bMsg = Hex.decode(shuju);
- byte[] keyBytes = Arrays.copyOf(bKey, 24);
- int j = 0, k = 16;
- while (j < 8) {
- keyBytes[k++] = keyBytes[j++];
- }
- SecretKey key3 = new SecretKeySpec(keyBytes, "DESede");
- IvParameterSpec iv3 = new IvParameterSpec(new byte[8]);//初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
- Cipher cipher3 = Cipher.getInstance("DESede/CBC/ZeroBytePadding");
- cipher3.init(Cipher.ENCRYPT_MODE, key3, iv3);
- byte[] bMac = cipher3.doFinal(bMsg);
- result= new String(Hex.encode(bMac));//encode方法字节数组转换成十六进制
- } catch (Exception e) {
- e.printStackTrace();
- throw e;
- }
- return result;
- }
- /**
- * 3DES 解密
- * @Method: decryptStr
- * @param @param shuju
- * @param @param key
- * @param @return
- * @param @throws Exception
- * @return String
- * @throws
- */
- public static String decryptStr(String shuju,String key) throws Exception{
- String result = "";
- try {
- Security.addProvider(new BouncyCastleProvider());
- byte[] bKey = Hex.decode(key);//十六进制转换成字节数据
- byte[] bMsg = Hex.decode(shuju);
- byte[] keyBytes = Arrays.copyOf(bKey, 24);
- int j = 0, k = 16;
- while (j < 8) {
- keyBytes[k++] = keyBytes[j++];
- }
- SecretKey key3 = new SecretKeySpec(keyBytes, "DESede");
- IvParameterSpec iv3 = new IvParameterSpec(new byte[8]);//初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
- Cipher cipher3 = Cipher.getInstance("DESede/CBC/ZeroBytePadding");
- cipher3.init(Cipher.DECRYPT_MODE, key3, iv3);
- byte[] bMac = cipher3.doFinal(bMsg);
- result= new String(Hex.encode(bMac));//encode方法字节数组转换成十六进制
- } catch (Exception e) {
- e.printStackTrace();
- throw e;
- }
- return result;
- }
- }
0 0
- DES加密模板,和C#des加密
- C#DES加密和解密
- C#DES加密和解密
- C#DES加密解密
- C#DES加密方法
- C#DES加密解密
- C#DES加密
- C#DES加密解密
- C#Des加密&解密
- DES加密和解密
- DES加密和解密
- Des加密和解密
- DES加密和解密
- DES解密和加密
- DES加密和解密
- des加密和解密
- DES加密和解密
- 用C#实现Des加密和解密
- JQuery学习一
- iOS的响应链
- OpenCL编程bug
- 新手建站十大免费空间推荐-稳定,可用的免费空间及其使用体验
- 进程通信
- DES加密模板,和C#des加密
- 给自定义View关联一个xib
- 第十六周项目1-小玩文件(1)
- jQuery-Ajax
- 第六章 注解式控制器详解—生产者、消费者请求限定
- pdf-renderer 把 pdf 转换成图片时报:java.io.IOException: Unknown CMap: UniGB-UCS2-H 的解决办法
- iOS开发UI篇—Quartz2D使用(图形上下文栈)
- Windows窗口消息大全
- 《架构师成长之路》连载之NO.0