java吧 关注:1,238,012贴子:12,710,255
  • 16回复贴,共1

请教下这个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);
}
}


1楼2016-03-09 21:21回复
    有大神在吗? 求教 求教! 万分感谢啦


    2楼2016-03-09 21:24
    回复
      paramstring


      来自iPhone客户端3楼2016-03-09 21:57
      收起回复
        好像明白了点 但是我怎么都找不到paramString啊…………


        来自Android客户端4楼2016-03-09 22:11
        回复
          strDefaultKey


          IP属地:上海5楼2016-03-09 22:19
          收起回复
            我已前和别人对接时,别人c,我java,
            ""http://www.cnblogs.com/AloneSword/archive/2013/12/11/3469219.html"""
            你自己看。


            IP属地:上海6楼2016-03-09 22:30
            回复