一个java的des加密解密代码如下:admin管理员组文章数量:1590401
//package com.visionsky.util;
import java.security.*;
//import java.util.regex.Pattern;
//import java.util.Hashtable;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;
/**
* des加密解密
*/
public class DESPlus {
private static String strDefaultKey = "PLFP"; //默认密钥
private static final byte[] iv = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef};//des 向量
private static BASE64Encoder enc = new BASE64Encoder();//将byte[]转换成String
private static BASE64Decoder dec = new BASE64Decoder(); //将String转换成byte[]
/**
* 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @param key
* 密钥
* @return 加密后的字节数组
* @throws Exception
*/
public static byte[] encrypt(byte[] arrB, String key) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec ivp = new IvParameterSpec(DESPlus.iv);
Cipher encryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivp);
return encryptCipher.doFinal(arrB);
}
/**
* 加密字符串
*
* @param xml
* 需加密的字符串
* @param key
* 密钥
* @return 加密后的字符串
* @throws Exception
*/
public static String encrypt(String xml, String key) throws Exception {
//return DESPlus.enc.encode(encrypt(xml.getBytes(), key));
return new String(encrypt(xml.getBytes(), key));
}
/**
* 使用默认公钥加密字符串
* @param xml 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public static String encrypt(String xml) throws Exception {
return encrypt(xml, strDefaultKey);
}
/**
* 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @param key
* 密钥
* @return 解密后的字节数组
* @throws Exception
*/
public static byte[] decrypt(byte[] arrB, String key) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec ivp = new IvParameterSpec(DESPlus.iv);
Cipher decryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, ivp);
return decryptCipher.doFinal(arrB);
}
/**
* 解密字符串
*
* @param xml
* 需解密的字符串
* @param key
* 密钥
* @return 解密后的字符串
* @throws Exception
*/
public static String decrypt(String xml, String key) throws Exception {
return new String(decrypt(DESPlus.dec.decodeBuffer(xml), key));
}
/**
* 使用默认公钥解密字符串
* @param xml 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public static String decrypt(String xml) throws Exception {
return decrypt(xml, strDefaultKey);
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
更多精彩教程请关注: XP系统下载版权声明:本文标题:【一个java的DES加解密类转换成C#】 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1725581657a1030805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论