C#--System.Web.HttpUtility.UrlEncode
JAVA-URLEncoder.encode
二者转化同一串string数据,得到的结果不同
C#
private byte[] WrapBytes(byte[] srcBytes, byte[] wrapKey)
{
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = wrapKey;
des.IV = wrapKey;
ICryptoTransform crypt = des.CreateEncryptor();
byte[] cipher = crypt.TransformFinalBlock(srcBytes, 0, srcBytes.Length);
return cipher;
}
public String DESEncryptV2(String srcString)
{
string ScreKey = "q0GAwOvSi3FlqqqoUQtLg2FH".Substring(0, 8);
byte[] cipherBytes = WrapBytes(System.Text.Encoding.GetEncoding("utf-16").GetBytes(srcString);
System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(ScreKey));
String basedString = Convert.ToBase64String(cipherBytes);
String resultString = basedString.Replace("\\+", ",");
return System.Web.HttpUtility.UrlEncode(resultString, System.Text.Encoding.GetEncoding("ISO-8859-1"));
}
java
private static byte[] wrapBytes(byte[] srcBytes, byte[] wrapKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidAlgorithmParameterException {
SecretKeySpec key = new SecretKeySpec(wrapKey, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding", "BC");
cipher.init(1, key);
byte[] cipherText = cipher.doFinal(srcBytes);
return cipherText;
}
public String getEncodeString(String srcString) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
byte[] cipherBytes = wrapBytes(srcString.getBytes("ISO-8859-1"), this.encryptKey.getBytes("ISO-8859-1"));
String basedString = EncodeBase64String(cipherBytes);
String resultString = basedString.replaceAll("\\+", ",");
return URLEncoder.encode(resultString, "ISO-8859-1");
}
public String doEncrypt(String srcString) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
byte[] cipherBytes = wrapBytes(srcString.getBytes("utf-16"), this.encryptKey.getBytes("ISO-8859-1"));
String basedString = EncodeBase64String(cipherBytes);
String resultString = basedString.replaceAll("\\+", ",");
return URLEncoder.encode(resultString, "iso-8859-1");
}
tobase64string检查过了,好像不是,编码格式检查过了,是utf-8和ISO-8859-1好像也没问题
请大家指点一下哪里出了问题