一个用java写的使用BouncyCastle库的DES/ECB/PKCS7Padding加密算法,如何改成用C#实现?

新手上路,请多包涵

java代码如下:

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");
   }

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 static String EncodeBase64String(byte[] srcBytes) {
      BASE64Encoder en = new BASE64Encoder();
      String base64Result = en.encode(srcBytes);
      return base64Result;
   }

我自己写了个C#版本的,但是同一个字串加密后的结果有些细微差别,并且加密后的长度也和java不一样
C#的如下

internal string Encrypt(string enKey, string srcString)
        {
            String resultString = string.Empty;

                enKey = enKey.Substring(0, 8);
                byte[] ptBytes = Encoding.BigEndianUnicode.GetBytes(srcString);
                byte[] n = new byte[ptBytes.Length + 2];
                n[0] = (byte)(0xff & int.Parse("-2"));
                n[1] = (byte)(0xff & int.Parse("-1"));
                for (var i = 0; i < ptBytes.Length; i++)
                {
                    n[2 + i] = ptBytes[i];
                }
                byte[] rv = Encrypt(enKey, n);

                String basedString = Convert.ToBase64String(rv);
                resultString = basedString.Replace("\\+", ",");
                resultString = System.Web.HttpUtility.UrlEncode(resultString, Encoding.GetEncoding("iso-8859-1"));
           
            return resultString;
        }

        private byte[] Encrypt(string keys, byte[] ptBytes)
        {
            byte[] key = Encoding.Default.GetBytes(keys);
            var cipher1 = CipherUtilities.GetCipher("DES/ECB/PKCS5Padding");
            var param = new DesParameters(key);
            cipher1.Init(true, param);
            byte[] outBytes = new byte[cipher1.GetOutputSize(ptBytes.Length)];
            int len1 = cipher1.ProcessBytes(ptBytes, 0, ptBytes.Length, outBytes, 0);
            cipher1.DoFinal(outBytes, len1);

            return outBytes;
        }

加密后的字串对比如下:
JAVA的
MLV24gU%2FeA9Fk%2CwljBB7VIlbQluuPIpk9l.... 长度871
C#的
MLV24gU%2feA9Fk%2bwljBB7VIlbQluuPIpk9l.... 长度841

求熟悉BouncyCastle的大神指点下

阅读 3.2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题