java 3des 24位_在java里面 我需要做一个3des加密的类,但是要求是IV必须是指定的 而且密钥也必须指定24位...

该博客主要讨论如何在Java中创建一个使用3DES加密的类,其中要求指定24位的密钥和预定义的8字节初始化向量(IV)。博主分享了相关的源代码,并在`TDESCoder`类中实现了加密和解密方法。代码在解密过程中遇到了问题,作者希望得到帮助进行修复。
摘要由CSDN通过智能技术生成

求助大哥们帮帮忙,我把源码贴出来帮看下什么地方需要修改,谢谢了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;

}

}

没财富了 真是抱歉!!!

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值