PHP和C#3DES加密 [英] PHP and C# 3DES Encryption

查看:534
本文介绍了PHP和C#3DES加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要下面的函数从PHP转换为C#(asp.net)

Need to convert the following function from PHP to C# (asp.net)

function encrypt_3DES($message, $key){
    // Se establece un IV por defecto
    $bytes = array(0,0,0,0,0,0,0,0); //byte [] IV = {0, 0, 0, 0, 0, 0, 0, 0}
    $iv = implode(array_map("chr", $bytes)); //PHP 4 >= 4.0.2

    // Se cifra
    $ciphertext = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv); //PHP 4 >= 4.0.2
    return $ciphertext;
}

在哪里
$消息是一个字符串连接code和 $键是关键

Where $message is a string to encode and $key is the key

$键是基地64 EN codeD,这是德codeD调用函数之前

The $key is base 64 encoded and it is decoded before calling the function

$key = $this->decodeBase64($key);
$ciphertext = $this->encrypt_3DES($message, $key);

下面的C#code我用:

Following C# code I used:

key = Base64Decode(key);
ciphertext = encrypt_3DES(order, key,true);

其中,

 private  string Base64Decode(string base64EncodedData)
    {
        byte[] base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
        return Encoding.GetEncoding(28591).GetString(base64EncodedBytes);
        // 28591 for php compatibility
    }  

  private string encrypt_3DES(string message, string k,bool useHashing)
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(message);

        //If hashing use get hashcode regards to your key
        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(Encoding.GetEncoding(28591).GetBytes(k));
            //Always release the resources and flush data
            // of the Cryptographic service provide. Best Practice

            hashmd5.Clear();
        }
        else
            keyArray = UTF8Encoding.GetEncoding(28591).GetBytes(k);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)
        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)

        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        //transform the specified region of bytes array to resultArray
        byte[] resultArray =
          cTransform.TransformFinalBlock(toEncryptArray, 0,
          toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor
        tdes.Clear();
        //Return the encrypted data into unreadable string format
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);

    }

从PHP和C#的结果是不一样的。

The results from PHP and C# are not the same.

推荐答案

我发现了一个code,在这个西班牙网站对我的作品。
http://www.resuelvetusproblemas.com/convertir-encriptacion- EN-PHP-3DES-EN-C /

I found a code that works for me in this spanish web. http://www.resuelvetusproblemas.com/convertir-encriptacion-en-php-3des-en-c/

这是在C#中的作用将是一样的,你已经用PHP编写的

This is the function in C# would be the same as you've written in PHP

    public static byte[] TripleDESEncrypt(string texto, byte[] key)
    {
        using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
        {
            byte[] iv_0 = { 0, 0, 0, 0, 0, 0, 0, 0 };

            byte[] toEncryptArray = Encoding.ASCII.GetBytes(texto);               

            tdes.IV = iv_0;

            //assign the secret key
            tdes.Key = key;

            tdes.Mode = CipherMode.CBC;

            tdes.Padding = PaddingMode.Zeros;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            //transform the specified region of bytes array to resultArray
            byte[] resultArray =
              cTransform.TransformFinalBlock(toEncryptArray, 0,
              toEncryptArray.Length);

            //Clear to Best Practices
            tdes.Clear();

            return resultArray;
        }
    }

这篇关于PHP和C#3DES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆