Encrypt and decrypt a string in c




















GetBytes SaltKey. CreateEncryptor keyBytes , Encoding. Write plainTextBytes , 0 , plainTextBytes. Length ; cryptoStream. ToArray ; cryptoStream.

CreateDecryptor keyBytes , Encoding. Read plainTextBytes , 0 , plainTextBytes. Length ; memoryStream. Close ; cryptoStream. Close ; return Encoding. Thursday, July 12, PM. Here is an encrption using popular RSA Alogorithm. Encrypt tempBytes, true ; stringBuilder. Append Convert. FromBase64String inputString. Reverse encryptedBytes ; arrayList. AddRange rsaCryptoServiceProvider. GetString arrayList. ToArray Type. GetType "System.

WriteLine "Verifying the hash WriteLine "The hashes are the same. WriteLine "The hashes are not same. Cryptography; using System. Write ; cs. Write inputbyteArray, 0, inputbyteArray. Length ; cs. ToBase64String ms. Message, ex. Similarly, for decrypting the string , 3 is subtracted from the ASCII value of the characters to print an original string.

In the above program, we have used simple logic for encrypting and decrypting a given string by simply adding and subtracting the particular key from ASCII value. RSA is another method for encrypting and decrypting the message.

As other people have said, Cryptography is not simple so it's best to avoid "rolling your own" encryption algorithm. You can, however, "roll your own" wrapper class around something like the built-in RijndaelManaged cryptography class. Rijndael is the algorithmic name of the current Advanced Encryption Standard , so you're certainly using an algorithm that could be considered "best practice".

The RijndaelManaged class does indeed normally require you to "muck about" with byte arrays, salts, keys, initialization vectors etc. The following class is one I wrote a while ago to perform exactly the kind of thing you're after, a simple single method call to allow some string-based plaintext to be encrypted with a string-based password, with the resulting encrypted string also being represented as a string.

Of course, there's an equivalent method to decrypt the encrypted string with the same password. Unlike the first version of this code, which used the exact same salt and IV values every time, this newer version will generate random salt and IV values each time. Since salt and IV must be the same between the encryption and decryption of a given string, the salt and IV is prepended to the cipher text upon encryption and extracted from it again in order to perform the decryption. The result of this is that encrypting the exact same plaintext with the exact same password gives and entirely different ciphertext result each time.

The "strength" of using this comes from using the RijndaelManaged class to perform the encryption for you, along with using the RfcDeriveBytes function of the System. Cryptography namespace which will generate your encryption key using a standard and secure algorithm specifically, PBKDF2 based upon the string-based password you supply. Finally, it's important to note that this is still unauthenticated encryption. Encryption alone provides only privacy i. Without knowing your exact requirements, it's difficult to say whether the code here is sufficiently secure for your needs, however, it has been produced to deliver a good balance between relative simplicity of implementation vs "quality".

For example, if your "receiver" of an encrypted string is receiving the string directly from a trusted "sender", then authentication may not even be necessary.

If you require something more complex, and which offers authenticated encryption, check out this post for an implementation. You can download a simple VS sample solution which includes a few unit tests here. If you are targeting ASP. See this article for more details. If you need to store a password in memory and would like to have it encrypted you should use SecureString :. See this page for an implementation example:. You may be looking for the ProtectedData class, which encrypts data using the user's logon credentials.

It does involve using bytes, but when it comes down to it you kind of do want encryption and decryption to be tough to figure out otherwise it will be easy to hack. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 9 years, 9 months ago. Active 2 years, 8 months ago.

Viewed k times. Improve this question. Will 2 2 silver badges 13 13 bronze badges. Richard Richard 5, 5 5 gold badges 26 26 silver badges 36 36 bronze badges.

Crypto is not simple. Read blogs. All crypto operates on byte arrays. Use Encoding. Any answer to this question that just suggests some crypto algorithm and fails to discuss identity, key management, integrity He apparently doesn't care much about the purpose of the functionality. Tarik No, he isn't exaggerating the situation.

Cryptography can be used to secure a system.



0コメント

  • 1000 / 1000