求助大哥们帮帮忙,我把源码贴出来帮看下什么地方需要修改,谢谢了publicclassDESedeCoderTest{publicstaticvoidmain(String[]args)throwsException{StringsHexPlainText="0123456789...
求助大哥们帮帮忙,我把源码贴出来帮看下什么地方需要修改,谢谢了
public class DESedeCoderTest {
public static void main(String[] args) throws Exception {
String sHexPlainText = "0123456789abcdef";
String StrTreeDesKey = "njnjnbasxzsbuhklnx5lmxjs";
SecretKey skSecretkey = TDESCoder.tripleDesKey(StrTreeDesKey);
byte[] byteaPlainText = hexStr2ByteArr(sHexPlainText);
byte[] byteaCryptograph = TDESCoder.enc(byteaPlainText, skSecretkey);
byte[] byteaPlainTextAftDec = TDESCoder.dec(byteaCryptograph,
skSecretkey);
}
public static String byteArr2HexStr(byte[] bytea) throws Exception {
String sHex = "";
int iUnsigned = 0;
StringBuffer sbHex = new StringBuffer();
for (int i = 0; i < bytea.length; i++) {
iUnsigned = bytea[i];
if (iUnsigned < 0) {
iUnsigned += 256;
}
if (iUnsigned < 16) {
sbHex.append("0");
}
sbHex.append(Integer.toString(iUnsigned, 16));
}
sHex = sbHex.toString();
return sHex;
}
public static byte[] hexStr2ByteArr(String sHex) throws Exception {
if (sHex.length() % 2 != 0) {
sHex = "0" + sHex;
}
byte[] bytea = bytea = new byte[sHex.length() / 2];
String sHexSingle = "";
for (int i = 0; i < bytea.length; i++) {
sHexSingle = sHex.substring(i * 2, i * 2 + 2);
bytea[i] = (byte) Integer.parseInt(sHexSingle, 16);
}
return bytea;
}
}
class TDESCoder {
private static final String S_CIPHER_ALGORITHM = "DESede/CBC/PKCS5Padding";
private static SecretKey skSecretkey;
private static byte[] _IV = {0,0,0,0,0,0,0,0};//指定的IV
public static byte[] enc(byte[] byteaPlainText, SecretKey skSecretkey)
throws Exception {
Cipher cipher = Cipher.getInstance(S_CIPHER_ALGORITHM);
IvParameterSpec ips = new IvParameterSpec(_IV);
cipher.init(Cipher.ENCRYPT_MODE, skSecretkey,ips);
byte[] byteaCryptograph = cipher.doFinal(byteaPlainText);
return byteaCryptograph;
}
public static byte[] dec(byte[] byteaCryptograph, SecretKey skSecretkey)
throws Exception {
Cipher cCipher = Cipher.getInstance(S_CIPHER_ALGORITHM);
IvParameterSpec ips = new IvParameterSpec(_IV);
cCipher.init(Cipher.DECRYPT_MODE, skSecretkey,ips);//在这报错了
byte[] byteaPlainText = cCipher.doFinal(byteaCryptograph);
return byteaPlainText;
}
public static SecretKey tripleDesKey(String StrKey) throws Exception {
byte[] key = new byte[24];
key = StrKey.getBytes();
SecretKey tripleDesKey = new SecretKeySpec(key, "DESede ");
return tripleDesKey;
}
}
没财富了 真是抱歉!!!
展开