简体   繁体   English

使用Rijndael加密和解密字符串

[英]Encrypting and decrypting a string using Rijndael

Before I start, I know there are a lot of similar questions on stackoverflow, but the majority of the answers consist of a large section of code with no explanation or help to aid the user. 在开始之前,我知道有很多关于stackoverflow的类似问题,但是大多数答案都是由很大一部分代码组成的,没有任何解释或帮助用户。

What I need to do is rather simple, take a string as input from the user, then the password, then encrypt the string with the password and write the cipher text to a file. 我需要做的很简单,先从用户输入一个字符串,然后输入密码,然后用密码对字符串进行加密,然后将密文写入文件中。 Then at a later date when the user wants, read the cipher text from the file and decrypt it and present the user with the original plain text. 然后,在以后用户需要时,从文件中读取密文并解密,然后向用户提供原始的纯文本。 I can handle writing to and from the file, I just need help with the encryption of the string. 我可以处理文件的写入和写入操作,我只需要有关字符串加密的帮助。 (Note: I do not want to just encrypt/decrypt the whole file - as it has to contain some unencrypted stuff - just a string in the program) (注意:我不想只加密/解密整个文件-因为它必须包含一些未加密的内容-程序中只是一个字符串)

Also, the 'Initialisation Vector' that is required - can it be the same as the password? 此外,还需要“初始化向量”-它可以与密码相同吗? Or can it be simply hard coded into the program? 还是可以将其简单地硬编码到程序中? Is it even essential for security? 它甚至对安全性至关重要吗? If not then does the user have to remember the password and the IV? 如果不是,那么用户是否必须记住密码和IV?

Another thing, when the user comes to decrypt and and enters an incorrect password, do the methods in the Rijndael class just raise an error or what? 另一件事,当用户解密并输入错误的密码时,Rijndael类中的方法会引发错误还是什么?

I am proficient when it comes to C# but I am new to cryptography so if you could step me through any code or answer you post I'd be most grateful. 我精通C#,但是我对密码学是陌生的,因此,如果您能单步执行任何代码或回答您,我将不胜感激。

Thanks 谢谢

IV's are required for most Rijndael modes, typically you just prepend it to your ciphertext, no need for users to remember. 大多数Rijndael模式都需要IV,通常只需将其添加到密文之前,无需用户记住。

Since you are using a user typed password to decrypt, you want to use Authenticated Encryption so that your program can safely re-prompt if the wrong password is entered, otherwise your program might not give any indication there was an issue and spit out random text. 由于您使用的是用户键入的密码进行解密,因此您想使用“ 身份验证加密”,以便在输入错误密码后程序可以安全地重新提示,否则您的程序可能不会给出任何提示,表明存在问题并吐出了随机文本。

I have an example of using AES-CBC with HMAC-SHA256 authentication that I try and keep up to date and reviewed, it in fact has a helper method that handles encrypting with just plaintext and password, and is also well commented: 我有一个使用HMAC-SHA256身份验证的AES-CBC的示例,我尝试对其进行更新并进行了审查,实际上它具有一个仅使用纯文本和密码来处理加密的辅助方法,并且也受到了很好的评论:

Modern Examples of Symmetric Authenticated Encryption of a string. 字符串的对称身份验证加密的现代示例。 C# C#

You need to take the following steps: 您需要执行以下步骤:

  1. Calculate a key from the password. 根据密码计算密钥。 You need to generate a random salt of 8 bytes and an iteration count as input for your password based key derivation function (PBKDF) such as PBKDF2; 您需要生成8个字节的随机盐和一个迭代计数作为基于密码的密钥派生函数(PBKDF)(例如PBKDF2)的输入;
  2. Encode your string using any encoding such as UTF-8, giving you the plaintext for your cipher; 使用任何编码(例如UTF-8)对字符串进行编码,为您的密码提供明文;
  3. Create a cipher, such as AES-128 in CBC mode, using PKCS#7 padding; 使用PKCS#7填充创建密码,例如CBC模式下的AES-128;
  4. Create a random IV, this should be the blocksize in bytes (so for AES this would be 16 bytes); 创建一个随机IV,应为以字节为单位的块大小(因此对于AES,将为16字节);
  5. Encrypt your plaintext using the given cipher, giving you the ciphertext; 使用给定的密码对您的明文进行加密,从而为您提供密文;
  6. Store salt, IV and ciphertext - if you require a string, you may use an encoding such as Base64. 存储盐,IV和密文-如果需要字符串,则可以使用诸如Base64之类的编码。

PBKDF2 is implemented in Rfc2898DeriveBytes . PBKDF2在Rfc2898DeriveBytes实现。

Note that this answer only shows how to achieve confidentiality, not integrity or authenticity. 请注意,此答案仅显示如何获得机密性,而不显示完整性或真实性。

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

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