简体   繁体   English

C#加密/解密

[英]C# encryption/decryption

I am trying to create a simple system that encrypts strings, converts the encrypted byte array to a string and stores it in the database. 我正在尝试创建一个简单的系统来加密字符串,将加密的字节数组转换为字符串并将其存储在数据库中。 Later on, the values can be retrieved from the database (as strings), comverted to byte arrays, decrypted and then converted to strings again. 稍后,可以从数据库中检索值(作为字符串),将其转换为字节数组,然后解密,然后再次转换为字符串。 I am also using a 256 bit key. 我也在使用256位密钥。 However, I seem to be doing something wrong and I am not familiar enough with the concept to come up with a fix. 但是,我似乎做错了事,而且我对这个概念还不够熟悉,无法提出解决方案。

Encryption code: 加密码:

private static string EncryptString(SymmetricAlgorithm symAlg, string inString)
{
    byte[] inBlock = Encoding.Unicode.GetBytes(inString);
    ICryptoTransform xfrm = symAlg.CreateEncryptor();
    return Convert.ToBase64String(xfrm.TransformFinalBlock(inBlock, 0, inBlock.Length));
}

Decryption code: 解密代码:

private static byte[] DecryptString(SymmetricAlgorithm symAlg, string inBytes)
{
    ICryptoTransform xfrm = symAlg.CreateDecryptor();
    return xfrm.TransformFinalBlock(Convert.FromBase64String(inBytes), 0, inBytes.Length);
}

The error I am getting when decrypting: 解密时出现的错误:

System.ArgumentOutOfRangeException: Attempt to transform beyond end of buffer. System.ArgumentOutOfRangeException:尝试转换到缓冲区末尾。
Parameter name: inputCount 参数名称:inputCount
at
System.Security.Cryptography.CapiSymmetricAlgorithm.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) System.Security.Cryptography.CapiSymmetricAlgorithm.TransformFinalBlock(Byte [] inputBuffer,Int32 inputOffset,Int32 inputCount)
at
CDMEncryptionLibrary.SyncEncryption.SecureCDM.DecryptString(SymmetricAlgorithm symAlg, String inBytes) CDMEncryptionLibrary.SyncEncryption.SecureCDM.DecryptString(SymmetricAlgorithm symAlg,字符串inBytes)

I understand that the problem is the conversion from and to bytes but how can I solve it? 我了解问题在于字节与字节之间的转换,但是我该如何解决呢?

--Edit - 编辑

Decryption code is now: 解密代码现在为:

    private static string DecryptString(SymmetricAlgorithm symAlg, string inBytesString)
    {

        var inBytes = Convert.FromBase64String(inBytesString);
        ICryptoTransform xfrm = symAlg.CreateDecryptor();
        byte[] outBlock= xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length);
        return Encoding.Unicode.GetString(outBlock);
    }

with error The input data is not a complete block. 有错误输入数据不是完整的块。

You are passing the length of the String , not the length of the byte array you convert the String to. 您传递的是String的长度,而不是您将String转换为的字节数组的长度。

Ie You pass Convert.FromBase64String(inBytes) as the first parameter but inBytes.Length as the last. 即,您将Convert.FromBase64String(inBytes)作为第一个参数,但将inBytes.Length作为最后一个参数。 Simply convert before taking the length; 简单地转换后再取长度;

private static byte[] DecryptString(SymmetricAlgorithm symAlg, string inBytes)
{
    ICryptoTransform xfrm = symAlg.CreateDecryptor();
    return xfrm.TransformFinalBlock(Convert.FromBase64String(inBytes), 0, Convert.FromBase64String(inBytes).Length);
}

or to be more readable; 或更具可读性;

private static byte[] DecryptString(SymmetricAlgorithm symAlg, string inBytesString)
{
    var inBytes = Convert.FromBase64String(inBytesString);
    ICryptoTransform xfrm = symAlg.CreateDecryptor();
    return xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM