java des密码在windows上的不同结果
我有一个API文档,需要加密密钥才能进行身份验证, 我设法构建并编译了他们的示例代码,但在windows上的结果与linux不同 当我在Windows上运行和测试时,一切似乎都是正确的,并与API一起工作 在Linux上进行相同的测试会产生不同的结果。我需要它在Linux上工作,因为那是主服务器 我在两个环境中使用并运行相同的jar文件 这是我试图加密的密钥(它是一个动态密钥): 2136230美元486B91E1BEA5D082BA3601CD803585CE美元20140409美元20140409美元ABCDEFGH美元预留$CTC 这是Windows上的正确输出(显然相当长): 这是来自Linux的错误输出:java des密码在windows上的不同结果,java,linux,windows,security,encryption,Java,Linux,Windows,Security,Encryption,我有一个API文档,需要加密密钥才能进行身份验证, 我设法构建并编译了他们的示例代码,但在windows上的结果与linux不同 当我在Windows上运行和测试时,一切似乎都是正确的,并与API一起工作 在Linux上进行相同的测试会产生不同的结果。我需要它在Linux上工作,因为那是主服务器 我在两个环境中使用并运行相同的jar文件 这是我试图加密的密钥(它是一个动态密钥): 2136230美元486B91E1BEA5D082BA3601CD803585CE美元20140409美元20140
F66D4CE1238B30EE54ABC74966D7AC3064FEA3ADFB9D37548E41509CE4FED9CB1D146651B491F2433169999A85F73DAF9ACD07A090DF3D85477BE4201ADC9E1A0181EA7CB763050A
这是什么原因造成的?如何纠正
这是我们从API公司收到的程序源代码:
public class DESUtil
{
private static final String Algorithm = "DESede/ECB/PKCS5Padding";// DESede/ECB/PKCS5Padding;DESede
private static final String DESede = "DESede";
public static byte[] encrypt(byte[] keybyte, byte[] src)
throws NoSuchAlgorithmException, NoSuchPaddingException, Exception
{
SecretKey deskey = new SecretKeySpec(keybyte, DESede);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
}
public static byte[] decrypt(byte[] keybyte, byte[] src)
throws NoSuchAlgorithmException, NoSuchPaddingException, Exception
{
SecretKey deskey = new SecretKeySpec(keybyte, DESede);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
}
public static String byte2hex(byte[] b)
{
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n <b.length; n++)
{
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs.append("0").append(stmp);
else
hs.append(stmp);
}
return hs.toString().toUpperCase(Locale.getDefault());
}
public static byte[] hex2byte(String hexStr)
{
if (hexStr.length() % 2 != 0)
{
AppLogger.error("hex2bytes's hexStr length is not even.");
return null;
}
byte[] toBytes = new byte[hexStr.length() / 2];
for (int i = 0, j = 0; i <hexStr.length(); j++, i = i + 2)
{
int tmpa = Integer.decode(
"0X" + hexStr.charAt(i) + hexStr.charAt(i + 1)).intValue();
toBytes[j] = (byte) (tmpa & 0XFF);
}
return toBytes;
}
public static void main(String[] args)
{
Security.addProvider(new com.sun.crypto.provider.SunJCE());
final byte[] rawKey = "db90e7eb".getBytes();
final byte[] keyBytes = new byte[24];
for (int i = 0; i <rawKey.length; i++)
{
keyBytes[i] = rawKey[i];
}
for (int i = rawKey.length; i <keyBytes.length; i++)
{
keyBytes[i] = (byte)0;
}
String szSrc = "20926330$AD75B1697FB5EB6345B2D412124030D2$10086$10086$10.164.111$ABCDEFGH$Reserved$CTC";
System.out.println("string before encrypt:" + szSrc);
byte[] encoded = null;
try
{
encoded = encrypt(keyBytes, szSrc.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("string after encrypt::" + byte2hex(encoded));
byte[] srcBytes = null;
try
{
srcBytes = decrypt(keyBytes, encoded);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("string before decode: :" + (new String(srcBytes)));
}
}
公共类DESUtil
{
私有静态最终字符串算法=“DESede/ECB/pkcs5ppadding”;//DESede/ECB/pkcs5ppadding;DESede
私有静态最终字符串DESede=“DESede”;
公共静态字节[]加密(字节[]密钥字节,字节[]src)
抛出NoSuchAlgorithmException、NoSuchPaddingException、Exception
{
SecretKey deskey=新的SecretKeySpec(keybyte,DESede);
Cipher c1=Cipher.getInstance(算法);
c1.init(Cipher.ENCRYPT_模式,deskey);
返回c1.doFinal(src);
}
公共静态字节[]解密(字节[]keybyte,字节[]src)
抛出NoSuchAlgorithmException、NoSuchPaddingException、Exception
{
SecretKey deskey=新的SecretKeySpec(keybyte,DESede);
Cipher c1=Cipher.getInstance(算法);
c1.init(Cipher.DECRYPT_模式,deskey);
返回c1.doFinal(src);
}
公共静态字符串字节2hex(字节[]b)
{
StringBuffer hs=新的StringBuffer();
字符串stmp=“”;
对于(int n=0;n几乎可以肯定您使用的是szSrc.getBytes()
,它使用平台的默认字符编码
如果它在Windows上工作,请尝试使用szSrc.getBytes(“ISO-8859-1”)
作为启动程序,但如果此字符串来自外部服务,则应动态确定编码方案(例如,如果它来自Servlet,请使用httpRequest.getCharacterEncoding()
).我猜可能是字符集编码问题或salt问题。将代码与代码或问题中的szSrc
值一起使用,会导致加密值与问题中的两个输出值不同。您应该只提供一个导致问题的输入值和输出值而不是随机无关的数据。
public class DESUtil
{
private static final String Algorithm = "DESede/ECB/PKCS5Padding";// DESede/ECB/PKCS5Padding;DESede
private static final String DESede = "DESede";
public static byte[] encrypt(byte[] keybyte, byte[] src)
throws NoSuchAlgorithmException, NoSuchPaddingException, Exception
{
SecretKey deskey = new SecretKeySpec(keybyte, DESede);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
}
public static byte[] decrypt(byte[] keybyte, byte[] src)
throws NoSuchAlgorithmException, NoSuchPaddingException, Exception
{
SecretKey deskey = new SecretKeySpec(keybyte, DESede);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
}
public static String byte2hex(byte[] b)
{
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n <b.length; n++)
{
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs.append("0").append(stmp);
else
hs.append(stmp);
}
return hs.toString().toUpperCase(Locale.getDefault());
}
public static byte[] hex2byte(String hexStr)
{
if (hexStr.length() % 2 != 0)
{
AppLogger.error("hex2bytes's hexStr length is not even.");
return null;
}
byte[] toBytes = new byte[hexStr.length() / 2];
for (int i = 0, j = 0; i <hexStr.length(); j++, i = i + 2)
{
int tmpa = Integer.decode(
"0X" + hexStr.charAt(i) + hexStr.charAt(i + 1)).intValue();
toBytes[j] = (byte) (tmpa & 0XFF);
}
return toBytes;
}
public static void main(String[] args)
{
Security.addProvider(new com.sun.crypto.provider.SunJCE());
final byte[] rawKey = "db90e7eb".getBytes();
final byte[] keyBytes = new byte[24];
for (int i = 0; i <rawKey.length; i++)
{
keyBytes[i] = rawKey[i];
}
for (int i = rawKey.length; i <keyBytes.length; i++)
{
keyBytes[i] = (byte)0;
}
String szSrc = "20926330$AD75B1697FB5EB6345B2D412124030D2$10086$10086$10.164.111$ABCDEFGH$Reserved$CTC";
System.out.println("string before encrypt:" + szSrc);
byte[] encoded = null;
try
{
encoded = encrypt(keyBytes, szSrc.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("string after encrypt::" + byte2hex(encoded));
byte[] srcBytes = null;
try
{
srcBytes = decrypt(keyBytes, encoded);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("string before decode: :" + (new String(srcBytes)));
}
}