PCLCrypto异常'CryptographicException:错误的PKCS7填充。长度无效'

时间:2016-07-31 07:14:07

标签: xamarin.ios xamarin.forms pcl-crypto

我正在努力使用PCLCryptho libraby,如果不检索异常'CryptographicException:Bad PKCS7 padding',我无法正常工作。长度无效'。运行一次代码是有效的,但是在彼此失败后运行它多次(使用不同的输入字符串)。解密发生在程序的新实例之后。我在iOS上使用Xamarin Forms运行此代码。这是我的代码(我每次都使用相同的VI,现在将盐保存在Settinsg中):

public static string EncryptAnswer(string answer, string passWord)
{
    try
    {
        var keyMaterial = CreateKey(passWord);
        var cipherTextBuffer = GetBytes(answer);

        var symmetricAlgorithm = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
        var symmetricKey = symmetricAlgorithm.CreateSymmetricKey(keyMaterial);

        using (var encryptor = WinRTCrypto.CryptographicEngine.CreateEncryptor(symmetricKey, GetBytes("vivivivivivivivi")))
        {
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                {
                    using (var bWriter = new BinaryWriter(cs))
                    {
                        bWriter.Write(cipherTextBuffer, 0, cipherTextBuffer.Length);
                        cs.FlushFinalBlock();
                    }
                }
                return GetString(ms.ToArray());
            }
        }
    }
    catch (Exception e)
    {
        return string.Empty;
    }
}

public static string DecryptAnswer(string encryptedAnswer, string passWord)
{
    try
    {
        var cipherTextBuffer = GetBytes(encryptedAnswer);
        var keyMaterial = CreateKey(passWord);

        var symmetricAlgorithm = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
        var symmetricKey = symmetricAlgorithm.CreateSymmetricKey(keyMaterial);
        using (var decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(symmetricKey, GetBytes("vivivivivivivivi")))
        {
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
                using (var binWriter = new BinaryWriter(cs))
                {
                    binWriter.Write(cipherTextBuffer, 0, cipherTextBuffer.Length);
                }
                return GetString(ms.ToArray());
            }
        }
    }
    catch (Exception e)
    {
    }
    return string.Empty;
}

public static byte[] CreateSalt()
{
    var salt = WinRTCrypto.CryptographicBuffer.GenerateRandom(8);
    CrossSettings.Current.AddOrUpdateValue("Salt", GetString(salt));

    return salt;
}

private static byte[] GetSalt()
{
    var saltString = CrossSettings.Current.GetValueOrDefault<string>("Salt");
    var salt = GetBytes(saltString);

    return salt;
}

private static byte[] CreateKey(string passWord)
{
    var key = NetFxCrypto.DeriveBytes.GetBytes(passWord, GetSalt(), 1000, 32);
    return key;
}

private static byte[] GetBytes(string str)
{
    return Encoding.Unicode.GetBytes(str);
}

private static string GetString(byte[] bytes)
{
    return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
}

这似乎与我找到的答案和例子相同。有人能告诉我什么是错的吗?

0 个答案:

没有答案