最近项目组需要开发一个用3DES进行加密的功能.我用JAVA完成了一个加密算法.但需要和另个一项部门进行联调,他那边是用C写的,两种算法最后结果不一致.我修改了JAVA的3DES算法的模式和填充方式但始终与C的结果不同.哪位高手遇到过同样的问题,是怎么解决的,望告之.多谢了.
以下是我写的JAVA代码.
package com.des.test;import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import org.apache.commons.codec.digest.DigestUtils;;public class TEST
{ public static void main(String[] args)
{
byte[] md5 = DigestUtils.md5("abc".getBytes());

TEST tripleTest = new TEST();
String key_string = "123400000000000000000000";
String src_string = "12345678";
String des_string = "";
String information = "DESEDE";// "DESEDE/ECB/PKCS5Padding";
String information2 = "DESEDE/CBC/PKCS5Padding";
try
{
SecretKey key = tripleTest.getDESKey(key_string);
SecureRandom random = new SecureRandom(); byte[] iv = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00 }; byte[] vi = new byte[8];
byte[] tt = new byte[8];
int COUNT = 0;
// while (!"Brsl0etq4fQ1fVmYgeeVdw==".equals(des_string))
// {
System.out.println("**********************" + (++COUNT)
+ "***********************"); random.nextBytes(vi);
IvParameterSpec iv_param_spec = new IvParameterSpec(iv);
// 加密
Cipher cipher1 = Cipher.getInstance(information);
cipher1.init(Cipher.ENCRYPT_MODE, key);
byte[] des_byte = cipher1.doFinal(src_string.getBytes());
System.out.println("src_string.getBytes()"
+ src_string.getBytes().length + "\n加密后数组长度des_byte:"
+ des_byte.length);
des_string = tripleTest.encodeBase64(des_byte);
System.out.println("des_string:" + des_string); // CBC加密,带初始化向量的加密方法
Cipher cipher3 = Cipher.getInstance(information2);
cipher3.init(Cipher.ENCRYPT_MODE, key, iv_param_spec);
byte[] des_byte_cbc = cipher3.doFinal(Hex.decodeHex(src_string
.toCharArray()));
System.out.println(des_byte_cbc.length);
String des_string_cbc = tripleTest.encodeBase64(des_byte_cbc);
System.out.println("des_string_cbc:" + des_string_cbc); // 解密
cipher1.init(Cipher.DECRYPT_MODE, key);
des_byte = tripleTest.decodeBase64(des_string);
byte[] src_byte = cipher1.doFinal(des_byte);
src_string = new String(src_byte);
System.out.println("src_string:" + src_string);
System.out.println("MD5::"
+ tripleTest.encodeBase64(new String(md5).getBytes())); // } } catch (InvalidKeyException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DecoderException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} } // base64转码 public static String encodeBase64(byte[] src_byte)
{
BASE64Encoder base64Encoder = new BASE64Encoder(); try
{
// 经过BASE64加密后的密文
String base64String = base64Encoder.encodeBuffer(src_byte);
String a = base64Encoder.encodeBuffer(src_byte);
String b = base64Encoder.encode(src_byte);
System.out.println("a:" + a + "b:" + b);
return base64String;
} catch (Exception e)
{
e.printStackTrace();
return null;
} } // base64解码
public static byte[] decodeBase64(String base64_string)
{
BASE64Decoder base64Decoder = new BASE64Decoder();
try
{
// 将BASE64转码过的字符串进行解码,获取明文
byte[] src_byte = base64Decoder.decodeBuffer(base64_string);
return src_byte;
} catch (Exception e)
{
e.printStackTrace();
return null;
} } // 获取密钥 public static SecretKey getDESKey(String key_string)
throws InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException, DecoderException
{
SecretKey key = null;
byte[] key_byte = null;
// 判断密钥的长度,如果不是24位,则以"0"补齐
String zeros = "000000000000000000000000";
if (key_string != null)
{
int keylength = key_string.getBytes().length;
if (keylength < 24)
{
key_string += zeros.substring(keylength);
}
} else
{
return null;
}
// key_string = encodeBase64(key_string.getBytes());
key_byte = key_string.getBytes();
System.out.println("new key_string::" + new String(key_byte)); DESedeKeySpec dks = new DESedeKeySpec(key_string.getBytes());
byte[] newbyte = Hex.decodeHex(key_string.toCharArray());
for (int i = 0; i < newbyte.length; i++)
{
System.out.println("newbyte[" + i + "]" + newbyte[i]);
}
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
key = keyFactory.generateSecret(dks); return key;
}}
大家看看还有什么需要修改的.

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【so_fast】截止到2008-07-05 17:13:55的历史汇总数据(不包括此帖):
    发帖的总数量:6                        发帖的总分数:120                      
    结贴的总数量:2                        结贴的总分数:40                       
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:4                        未结的总分数:80                       
    结贴的百分比:33.33 %               结分的百分比:33.33 %                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    楼主该结一些帖子了
      

  2.   

    关注中……java实现的3des算法的密钥是否必须是24位呢?如果不是24位,后边补ascii的‘0’可以吗?看楼主有补0的处理。
      

  3.   

    http://lijunjie337.javaeye.com/blog/717911
    看看这位高人说的
    我也遇到这个问题,不过还没解决