在Unity.JS中以单位c#和Decrypt加密RijndaelManaged 128

时间:2014-08-13 15:24:37

标签: c# node.js encryption unity3d rijndaelmanaged

我正在寻找一种方法来加密统一c#中的字节数组并在node.js服务器上解密。

我对任何一种实现都持开放态度,但我现在已经使用下面的代码加密/解密,但我收到错误:

TypeError: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

使用RijndaelManaged 128解密以统一方式加密的文件时

查找下面的加密和解密代码:

Unity C#Encrypt

private void GenerateEncryptionKey(string userID)
{
    //Generate the Salt, with any custom logic and using the user's ID
    StringBuilder salt = new StringBuilder();
    for (int i = 0; i < 8; i++)
    {
        salt.Append("," + userID.Length.ToString());
    }

    Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes (Encoding.UTF8.GetBytes(userID), Encoding.UTF8.GetBytes(salt.ToString()), 100);
    m_cryptoKey = pwdGen.GetBytes(KEY_SIZE / 8);
    m_cryptoIV = pwdGen.GetBytes(KEY_SIZE / 8);
}

public void Save(string path)
{
    string json = MiniJSON.Json.Serialize(m_saveData);

    using (RijndaelManaged crypto = new RijndaelManaged())
    {
        crypto.BlockSize = KEY_SIZE;
        crypto.Padding = PaddingMode.PKCS7;
        crypto.Key = m_cryptoKey;
        crypto.IV = m_cryptoIV;
        crypto.Mode = CipherMode.CBC;

        ICryptoTransform encryptor = crypto.CreateEncryptor(crypto.Key, crypto.IV);

        byte[] compressed = null;

        using (MemoryStream compMemStream = new MemoryStream())
        {
            using (StreamWriter writer = new StreamWriter(compMemStream, Encoding.UTF8))
            {
                writer.Write(json);
                writer.Close();

                compressed = compMemStream.ToArray();
            }
        }

        if (compressed != null)
        {
            using (MemoryStream encMemStream = new MemoryStream(compressed))
            {
                using (CryptoStream cryptoStream = new CryptoStream(encMemStream, encryptor, CryptoStreamMode.Write))
                {
                    using (FileStream fs = File.Create(GetSavePath(path)))
                    {
                        byte[] encrypted = encMemStream.ToArray();

                        fs.Write(encrypted, 0, encrypted.Length);
                        fs.Close();
                    }
                }
            }
        }
    }
}

忽略压缩位,我最终将压缩数据进行加密,但我在此示例中删除了它。

Node.JS Decrypt

var sUserID = "hello-me";
var sSalt = "";

for (var i = 0; i < 8; i++)
{
    sSalt += "," + sUserID.length;
}

var KEY_SIZE = 128;

crypto.pbkdf2(sUserID, sSalt, 100, KEY_SIZE / 4, function(cErr, cBuffer){
    var cKey = cBuffer.slice(0, cBuffer.length / 2);
    var cIV = cBuffer.slice(cBuffer.length / 2, cBuffer.length);

    fs.readFile("save.sav", function (cErr, cData){
        try
        {
            var cDecipher = crypto.createDecipheriv("AES-128-CBC", cKey, cIV);

            var sDecoded = cDecipher.update(cData, null, "utf8");
            sDecoded += cDecipher.final("utf8");
            console.log(sDecoded);
        }
        catch(e)
        {
            console.log(e.message);
            console.log(e.stack);
        }
    });
});

我认为这个问题与填充有关!我没有使用:

cryptoStream.FlushFinalBlock();

在c#land中保存文件时因为某些原因c#后无法解密它并且它对节点解密它的能力也没有影响,但也许我只是缺少用填充解密它的东西?

感谢任何帮助

2 个答案:

答案 0 :(得分:2)

一个问题是您正在使用PasswordDeriveBytes according to this article用于PBKDF1,而Rfc2898DeriveBytes用于PBKDF2。您在节点脚本中使用PBKDF2。

然后你应该检查你的cKey和cIV值是否在C#和节点之间匹配。

答案 1 :(得分:0)

好吧,使用RijndaelManaged加密和解密时,操作顺序似乎非常重要。

以下是在Unity中加密和解密的代码,并使用问题中发布的node.js代码。

public void Save(string path)
{
    string json = MiniJSON.Json.Serialize(m_saveData);

    using (RijndaelManaged crypto = new RijndaelManaged())
    {
        crypto.BlockSize = KEY_SIZE;
        crypto.Padding = PaddingMode.PKCS7;
        crypto.Key = m_cryptoKey;
        crypto.IV = m_cryptoIV;
        crypto.Mode = CipherMode.CBC;

        ICryptoTransform encryptor = crypto.CreateEncryptor(crypto.Key, crypto.IV);

        byte[] compressed = null;

        using (MemoryStream compMemStream = new MemoryStream())
        {
            using (StreamWriter writer = new StreamWriter(compMemStream, Encoding.UTF8))
            {
                writer.Write(json);
                writer.Close();

                //compressed = CLZF2.Compress(compMemStream.ToArray());
                compressed = compMemStream.ToArray();
            }
        }

        if (compressed != null)
        {
            using (MemoryStream encMemStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(encMemStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(compressed, 0, compressed.Length);
                    cryptoStream.FlushFinalBlock();

                    using (FileStream fs = File.Create(GetSavePath(path)))
                    {
                        encMemStream.WriteTo(fs);
                    }
                }
            }
        }
    }
}

public void Load(string path)
{
    path = GetSavePath(path);

    try
    {
        byte[] decrypted = null;

        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            using (RijndaelManaged crypto = new RijndaelManaged())
            {
                crypto.BlockSize = KEY_SIZE;
                crypto.Padding = PaddingMode.PKCS7;
                crypto.Key = m_cryptoKey;
                crypto.IV = m_cryptoIV;
                crypto.Mode = CipherMode.CBC;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = crypto.CreateDecryptor(crypto.Key, crypto.IV);

                using (CryptoStream cryptoStream = new CryptoStream(fs, decryptor, CryptoStreamMode.Read))
                {
                    using (MemoryStream decMemStream = new MemoryStream())
                    {
                        var buffer = new byte[512];
                        var bytesRead = 0;

                        while ((bytesRead = cryptoStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            decMemStream.Write(buffer, 0, bytesRead);
                        }

                        //decrypted = CLZF2.Decompress(decMemStream.ToArray());
                        decrypted = decMemStream.ToArray();
                    }
                }
            }
        }

        if (decrypted != null)
        {
            using (MemoryStream jsonMemoryStream = new MemoryStream(decrypted))
            {
                using (StreamReader reader = new StreamReader(jsonMemoryStream))
                {
                    string json = reader.ReadToEnd();

                    Dictionary<string, object> saveData = MiniJSON.Json.Deserialize(json) as Dictionary<string, object>;

                    if (saveData != null)
                    {
                        m_saveData = saveData;
                    }
                    else
                    {
                        Debug.LogWarning("Trying to load invalid JSON file at path: " + path);
                    }
                }
            }
        }
    }
    catch (FileNotFoundException e)
    {
        Debug.LogWarning("No save file found at path: " + path);
    }
}