求帮忙算出一个java代码的key
这里是key==================================packagecom.qqmagic;importjava.security.Key;im...
这里是key
==================================
package com.qqmagic;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class DesUtils
{
private static String strDefaultKey = "national";
private Cipher decryptCipher = (Cipher)null;
private Cipher encryptCipher = (Cipher)null;
public DesUtils()
throws Exception
{
this(strDefaultKey);
}
public DesUtils(String paramString)
{
try
{
paramString = getKey(paramString.getBytes());
this.encryptCipher = Cipher.getInstance("DES");
this.encryptCipher.init(1, paramString);
this.decryptCipher = Cipher.getInstance("DES");
this.decryptCipher.init(2, paramString);
return;
}
catch (Exception paramString)
{
for (;;)
{
paramString.printStackTrace();
}
}
}
public static String byteArr2HexStr(byte[] paramArrayOfByte)
throws Exception
{
int k = paramArrayOfByte.length;
StringBuffer localStringBuffer = new StringBuffer(k * 2);
int i = 0;
if (i >= k) {
return localStringBuffer.toString();
}
for (int j = paramArrayOfByte[i];; j += 256) {
if (j >= 0)
{
if (j < 16) {
localStringBuffer.append('0');
}
localStringBuffer.append(Integer.toString(j, 16));
i++;
break;
}
}
}
private Key getKey(byte[] paramArrayOfByte)
throws Exception
{
byte[] arrayOfByte = new byte[8];
for (int i = 0;; i++)
{
if ((i >= paramArrayOfByte.length) || (i >= arrayOfByte.length)) {
return new SecretKeySpec(arrayOfByte, "DES");
}
arrayOfByte[i] = ((byte)paramArrayOfByte[i]);
}
}
public static byte[] hexStr2ByteArr(String paramString)
throws Exception
{
byte[] arrayOfByte1 = paramString.getBytes();
int j = arrayOfByte1.length;
byte[] arrayOfByte2 = new byte[j / 2];
for (int i = 0;; i += 2)
{
if (i >= j) {
return arrayOfByte2;
}
paramString = new String(arrayOfByte1, i, 2);
arrayOfByte2[(i / 2)] = ((byte)(byte)Integer.parseInt(paramString, 16));
}
}
public String decrypt(String paramString)
throws Exception
{
return new String(decrypt(hexStr2ByteArr(paramString)));
}
public byte[] decrypt(byte[] paramArrayOfByte)
throws Exception
{
return this.decryptCipher.doFinal(paramArrayOfByte);
}
public String encrypt(String paramString)
throws Exception
{
return byteArr2HexStr(encrypt(paramString.getBytes()));
}
public byte[] encrypt(byte[] paramArrayOfByte)
throws Exception
{
return this.encryptCipher.doFinal(paramArrayOfByte);
}
}
======================================= 展开
==================================
package com.qqmagic;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class DesUtils
{
private static String strDefaultKey = "national";
private Cipher decryptCipher = (Cipher)null;
private Cipher encryptCipher = (Cipher)null;
public DesUtils()
throws Exception
{
this(strDefaultKey);
}
public DesUtils(String paramString)
{
try
{
paramString = getKey(paramString.getBytes());
this.encryptCipher = Cipher.getInstance("DES");
this.encryptCipher.init(1, paramString);
this.decryptCipher = Cipher.getInstance("DES");
this.decryptCipher.init(2, paramString);
return;
}
catch (Exception paramString)
{
for (;;)
{
paramString.printStackTrace();
}
}
}
public static String byteArr2HexStr(byte[] paramArrayOfByte)
throws Exception
{
int k = paramArrayOfByte.length;
StringBuffer localStringBuffer = new StringBuffer(k * 2);
int i = 0;
if (i >= k) {
return localStringBuffer.toString();
}
for (int j = paramArrayOfByte[i];; j += 256) {
if (j >= 0)
{
if (j < 16) {
localStringBuffer.append('0');
}
localStringBuffer.append(Integer.toString(j, 16));
i++;
break;
}
}
}
private Key getKey(byte[] paramArrayOfByte)
throws Exception
{
byte[] arrayOfByte = new byte[8];
for (int i = 0;; i++)
{
if ((i >= paramArrayOfByte.length) || (i >= arrayOfByte.length)) {
return new SecretKeySpec(arrayOfByte, "DES");
}
arrayOfByte[i] = ((byte)paramArrayOfByte[i]);
}
}
public static byte[] hexStr2ByteArr(String paramString)
throws Exception
{
byte[] arrayOfByte1 = paramString.getBytes();
int j = arrayOfByte1.length;
byte[] arrayOfByte2 = new byte[j / 2];
for (int i = 0;; i += 2)
{
if (i >= j) {
return arrayOfByte2;
}
paramString = new String(arrayOfByte1, i, 2);
arrayOfByte2[(i / 2)] = ((byte)(byte)Integer.parseInt(paramString, 16));
}
}
public String decrypt(String paramString)
throws Exception
{
return new String(decrypt(hexStr2ByteArr(paramString)));
}
public byte[] decrypt(byte[] paramArrayOfByte)
throws Exception
{
return this.decryptCipher.doFinal(paramArrayOfByte);
}
public String encrypt(String paramString)
throws Exception
{
return byteArr2HexStr(encrypt(paramString.getBytes()));
}
public byte[] encrypt(byte[] paramArrayOfByte)
throws Exception
{
return this.encryptCipher.doFinal(paramArrayOfByte);
}
}
======================================= 展开
2个回答
展开全部
没看明白你指的是什么key?加密的密钥向量吗?
如果是想用DES加密解密的话,可以使用下面的(加上注释后不让发,所以把注释去掉了,擦,还不让写“加密解密”这样的字)
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
public class DES {
private static final String ENCODING = "utf-8";
private byte[] key,iv;
private void setKeyIv(String key){
try {
this.key = key.getBytes(ENCODING);
//偏移量,可以与密钥相同
this.iv = new String("bing1987").getBytes(ENCODING);
}
catch (Exception e) {
e.printStackTrace();
}
}
//初始构造方法,设置默认密钥
public DES() {
setKeyIv("5a8f9e3l");
}
public DES(String key) {
setKeyIv(key);
}
public void main(String content) throws Exception {
System.out.println("加前:" + content);
String encrypt = encrypt(content);
System.out.println("加后:" + encrypt);
String decrypt = decrypt(encrypt);
System.out.println("解后:" + decrypt);
}
//加mi方法:instr 待加字串 return 加后的密文
public String encrypt(String instr) throws Exception{
byte[] bytes = desPublic(Cipher.ENCRYPT_MODE, instr.getBytes(ENCODING));
return Tools.encryptBASE64(bytes);
}
//解mi方法:encryptStr 待解字串 return 解后的字串
public String decrypt(String encryptStr) throws Exception {
byte[] bytes = desPublic(Cipher.DECRYPT_MODE, Tools.decryptBASE64(encryptStr));
return new String(bytes);
}
//mode 加或解表示值
//context 待加或解的字节数组
//return 加或解后的字节数组
private byte[] desPublic(int mode, byte[] context) throws Exception
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(this.key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(this.iv);
cipher.init(mode, secretKey, iv);
return cipher.doFinal(context);
}
}
上面用到的tools工具
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Tools {
public static byte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
}
你也可以把decryptBASE64和encryptBASE64方法合并到DES类里
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询