字符串在C#中加密使用TripleDES,需要在PHP中解密

时间:2015-09-15 17:00:31

标签: c# php tripledes

C#代码看起来像那样(不能像在客户端的系统中那样改变它)。

namespace Common {
public static class EncryptionHelper
{

   private const string cryptoKey = "password";

// The Initialization Vector for the DES encryption routine
   private static readonly byte[] IV = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };

/// <summary>
/// Encrypts provided string parameter
/// </summary>
public static string Encrypt(string s)
{

   string result = string.Empty;

   byte[] buffer = Encoding.ASCII.GetBytes(s);

   byte[] k = Encoding.ASCII.GetBytes(cryptoKey);

   TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
   MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();


   des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));

   des.IV = IV;

   result = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));

   return result;
  }
}
}

我发现客户从这里开始上课:http://johnnycoder.com/blog/2008/07/03/c-encryption-decryption-helper-class/

我对C#不太熟悉,我需要用PHP加密解密字符串 这段代码。

当我执行“md5($ key,true)”时,我得不到与“MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));”相同的结果;“,不确定原因。

如何将“byte [] IV”转换为PHP字符串?

任何帮助将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:2)

管理以使其正常工作:

// create test list
ArrayList<Integer> arrList = new ArrayList<>();
for (int i = 1; i <= 13; i++) {
    arrList.add(i);
}

// clone
ArrayList<Integer> newArrList = new ArrayList<>();
for (int i = 0; i < 4; i++) {
    newArrList.addAll(arrList);
}