How can a DES/ECB/PKCS7Padding encryption algorithm written in java using BouncyCastle library be changed to be implemented in C-sharp?

the java code is as follows:

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

I wrote a C-sharp version myself, but the encrypted result of the same string is slightly different, and the length of the encrypted string is not the same as that of java
C-sharp

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; iPP)
                {
                    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;
        }

the comparison of encrypted strings is as follows:
JAVA
MLV24gU%2FeA9Fk%2CwljBB7VIlbQluuPIpk9l.
MLV24gU%2feA9Fk%2bwljBB7VIlbQluuPIpk9l. of length 871
C-sharp Length 841

seek the guidance of a great god who is familiar with BouncyCastle

Jul.13,2021
Menu