吉良吉影qaq 2022-06-22 17:16 采纳率: 0%
浏览 57

JAVA和C# URLEncode编码

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好像也没问题
请大家指点一下哪里出了问题
  • 写回答

1条回答 默认 最新

  • 灯塔@kuaidao 2022-06-22 18:13
    关注

    和语言没什么关系。写法上没问题。结果一定是一致的。

    1.可以去网上找一个

    评论

报告相同问题?

问题事件

  • 创建了问题 6月22日

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试