java中des的秘钥,请教下这个DES 解密类 它的密钥应该在哪里呢?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package com.xxx;

import java.security.Key;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

public class wd

{

private static String strDefaultKey = "national";

private Cipher decryptCipher = (Cipher)null;

private Cipher encryptCipher = (Cipher)null;

public wd()

throws Exception

{

this(strDefaultKey);

}

public wd(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)

{

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();

}

int j = paramArrayOfByte[i];

for (;;)

{

if (j >= 0)

{

if (j < 16) {

localStringBuffer.append('0');

}

localStringBuffer.append(Integer.toString(j, 16));

i += 1;

break;

}

j += 256;

}

}

private Key getKey(byte[] paramArrayOfByte)

throws Exception

{

byte[] arrayOfByte = new byte[8];

int i = 0;

for (;;)

{

if ((i >= paramArrayOfByte.length) || (i >= arrayOfByte.length)) {

return new SecretKeySpec(arrayOfByte, "DES");

}

arrayOfByte[i] = paramArrayOfByte[i];

i += 1;

}

}

public static byte[] hexStr2ByteArr(String paramString)

throws Exception

{

paramString = paramString.getBytes();

int j = paramString.length;

byte[] arrayOfByte = new byte[j / 2];

int i = 0;

for (;;)

{

if (i >= j) {

return arrayOfByte;

}

String str = new String(paramString, i, 2);

arrayOfByte[(i / 2)] = ((byte)Integer.parseInt(str, 16));

i += 2;

}

}

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值