dpqmu84646 2019-02-02 20:32
浏览 57

将C#加密/解密转换为PHP加密/解密,反之亦然

So I've made a desktop application (before the website) were i've made a login and a register system in it and an encryption ofcourse. And now I've had an idea to make a website with the register and login system because i thought it would be easier but the problem is i've made the website after the desktop application which means i've made a C# encryption/decryption before website and I want to convert the C# encryption to the PHP (if it is even possible) to match my database users information (password, username, mail etc.). This is my C# encryption and decryption code: Encryption:

    private static byte[] AesEncrypt(byte[] bytesToBeEncrypted, byte[] 
    passwordBytes)
    {
        byte[] encryptedBytes;
        var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; // min 8

        using (var ms = new MemoryStream())
        {
            using (var aes = new RijndaelManaged())
            {
                aes.KeySize = 256;
                aes.BlockSize = 128;

                var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                aes.Key = key.GetBytes(aes.KeySize / 8);
                aes.IV = key.GetBytes(aes.BlockSize / 8);

                aes.Mode = CipherMode.CBC;

                using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                    cs.Close();
                }

                encryptedBytes = ms.ToArray();
            }
        }

        return encryptedBytes;
    }

Decryption:

            private static byte[] AesDecrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
    {
        byte[] decryptedBytes;
        var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; // min 8 

        using (var ms = new MemoryStream())
        {
            using (var aes = new RijndaelManaged())
            {
                aes.KeySize = 256;
                aes.BlockSize = 128;

                var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                aes.Key = key.GetBytes(aes.KeySize / 8);
                aes.IV = key.GetBytes(aes.BlockSize / 8);

                aes.Mode = CipherMode.CBC;

                using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                    cs.Close();
                }

                decryptedBytes = ms.ToArray();
            }
        }

        return decryptedBytes;
    }

Method which I use to encrypt data:

            public static string Encrypt(string clearText, string password, byte[] salt = null)
    {
        var baPwd = Encoding.UTF8.GetBytes(password);
        var baPwdHash = SHA256.Create().ComputeHash(baPwd);
        var baText = Encoding.UTF8.GetBytes(clearText);
        byte[] baSalt;

                    baSalt = salt;
                    var baEncrypted = new byte[baSalt.Length + baText.Length];
                    for (var i = 0; i < baSalt.Length; i++)
                        baEncrypted[i] = baSalt[i];
                    EncryptionSalt = baSalt.ToString();
                    for (var i = 0; i < baText.Length; i++)
                        baEncrypted[i + baSalt.Length] = baText[i];
                    baEncrypted = AesEncrypt(baEncrypted, baPwdHash);
                    var result = Convert.ToBase64String(baEncrypted);
                    EncryptionSalt = baSalt.ToString();
                    return result;
                }

    }

Method which I use to decrypt data:

            public static string Decrypt(string cipherText, string password)
    {
        var baPwd = Encoding.UTF8.GetBytes(password);
        var baPwdHash = SHA256.Create().ComputeHash(baPwd);
        var baText = Convert.FromBase64String(cipherText);
        var baDecrypted = AesDecrypt(baText, baPwdHash);
        const int saltLength = 12;
        var baResult = new byte[baDecrypted.Length - saltLength];
        for (var i = 0; i < baResult.Length; i++)
            baResult[i] = baDecrypted[i + saltLength];
        var result = Encoding.UTF8.GetString(baResult);
        return result;
    }

An example of using C# encryption in my C# code:

    Encryption.Encrypt(password, Encryption.RandomBytes().ToString(), salt));

And the RandomBytes method:

            public static byte[] RandomBytes()
    {
        const int saltLength = 12;
        var ba = new byte[saltLength];
        RandomNumberGenerator.Create().GetBytes(ba);
        return ba;
    }

Firstly I need to know if it's possible, secondly if anyone wants to you could give me an example of this encryption in PHP or give me the code.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题