CryptographicException: 坏的PKCS7填充。无效长度 82

问题描述 投票:0回答:1

试图实现EncryptionDecryption......方法是将数据序列化为JSON,然后加密JSON来保存它。有时,它没有任何问题,而有时我得到这个糟糕的padding错误。

保存加载机制

public static void SaveGameJson()
{

    Debug.Log("TRYING TO SAVE THE GMAE THROUGH Json File");
    SaveManager data = new SaveManager();

    string jsonData = JsonUtility.ToJson(data); // encrypting the json data..
//    string jsonData = JsonConvert.SerializeObject(data); // encrypting the json data..

    string encryptedJsonData = EncryptionRijndael.Encrypt(jsonData);
    WriteToFile(JsonfileName, encryptedJsonData);
    Debug.LogError("Json data Saved: " + jsonData);
    Debug.LogError("Json Encrypted data: " + encryptedJsonData);
}



public static SaveManager LoadGameJson()
{
    string path = GetFilePath(JsonfileName);
    Debug.Log("TRYING TO LOAD THE GMAE THROUGH FILE");

    if (File.Exists(path))
    {
        string encryptedJsonData = ReadFromFile(JsonfileName); // reading encrypted data from json Encrypted file
        string jsonData = EncryptionRijndael.Decrypt(encryptedJsonData); // decrypting json data from encrypted string
        Debug.LogError("Json data Load: " + jsonData);
        Debug.LogError("Encrypted Data: " + encryptedJsonData);
        SaveManager data = JsonUtility.FromJson<SaveManager>(jsonData);
        //SaveManager data = JsonConvert.DeserializeObject<SaveManager>(jsonData);

        return data;
    }
    else
    {
        Debug.LogError("save file not found on:" + path);
        return null;
    }
}

附加功能

    static void WriteToFile(string fileName, string json) {

    string path = GetFilePath(fileName);

    FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate);
    using (StreamWriter writer = new StreamWriter(fileStream)) {
        writer.Write(json);
    }
}

加密

public static string Encrypt(string strPlain)
{
    string password = strPassword;
    if (!useSecure)
        return strPlain;

    try
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, GetIV(), Iterations);

        byte[] key = rfc2898DeriveBytes.GetBytes(8);

        using (var memoryStream = new MemoryStream())
        using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, GetIV()), CryptoStreamMode.Write))
        {
            memoryStream.Write(GetIV(), 0, GetIV().Length);

            byte[] plainTextBytes = Encoding.UTF8.GetBytes(strPlain);

            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();

            return Convert.ToBase64String(memoryStream.ToArray());
        }
    }
    catch (Exception e)
    {
        Debug.LogWarning("Encrypt Exception: " + e);
        return strPlain;
    }
}

解密

 public static string Decrypt(string strEncript)
 {

    string password = strPassword;

    if (!useSecure)
        return strEncript;

    try
    {
        byte[] cipherBytes = Convert.FromBase64String(strEncript);

        using (var memoryStream = new MemoryStream(cipherBytes))
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] iv = GetIV();
            memoryStream.Read(iv, 0, iv.Length);

            // use derive bytes to generate key from password and IV
            var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, iv, Iterations);

            byte[] key = rfc2898DeriveBytes.GetBytes(8);

            using (var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read))
            using (var streamReader = new StreamReader(cryptoStream))
            {
                string strPlain = streamReader.ReadToEnd();
                return strPlain;
            }
        }
    }
    catch (Exception e)
    {
        Debug.LogWarning("Decrypt Exception: " + e);
        return strEncript;
    }
}

JSON得到保存enter image description here

从加密的字符串中加载JSONenter image description here

PS:在日志中似乎加密工作正常,但当它试图解密字符串时,它通过这个异常。有什么更好的办法吗?

c# unity3d encryption game-development
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.