This is a discussion on Uses of a symmetric key algorithm (Rijndael) to encrypt and decrypt data. within the Other Web Programming Languages forums, part of the Web Development category; Uses of a symmetric key algorithm (Rijndael) to encrypt and decrypt data. EXAMPLE: Symmetric key encryption and decryption using Rijndael ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| Uses of a symmetric key algorithm (Rijndael) to encrypt and decrypt data. EXAMPLE: Symmetric key encryption and decryption using Rijndael algorithm. using System; using System.IO; using System.Text; using System.Security.Cryptography; public class RijndaelSimple { public static string Encrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize) { byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); PasswordDeriveBytes password = new PasswordDeriveBytes( passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keySize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor( keyBytes, initVectorBytes); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); byte[] cipherTextBytes = memoryStream.ToArray(); memoryStream.Close(); cryptoStream.Close(); string cipherText = Convert.ToBase64String(cipherTextBytes); return cipherText; } public static string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize) { byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] cipherTextBytes = Convert.FromBase64String(cipherText); PasswordDeriveBytes password = new PasswordDeriveBytes( passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keySize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform decryptor = symmetricKey.CreateDecryptor( keyBytes, initVectorBytes); MemoryStream memoryStream = new MemoryStream(cipherTextBytes); CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); byte[] plainTextBytes = new byte[cipherTextBytes.Length]; int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); string plainText = Encoding.UTF8.GetString(plainTextByteis, 0, decryptedByteCount); return plainText; } } public class RijndaelSimpleTest { static void Main(string[] args) { string plainText = "Hello, World!"; string passPhrase = "Pas5pr@se"; string saltValue = "s@1tValue"; string hashAlgorithm = "SHA1"; int passwordIterations = 2; string initVector = "@1B2c3D4e5F6g7H8"; int keySize = 256; Console.WriteLine(String.Format("Plaintext : {0}", plainText)); string cipherText = RijndaelSimple.Encrypt(plainText, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, KeySize); Console.WriteLine(String.Format("Encrypted : {0}", cipherText)); plainText = RijndaelSimple.Decrypt(cipherText, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize); Console.WriteLine(String.Format("Decrypted : {0}", plainText)); } } |
|
#2
| |||
| |||
| Hi I have a problem with decrypting the text encrypted with Rijndael key. Samples ENCRYPTED : QPg+uQrXTRXw6aKCgYyO0A== DECRYPTED : 0602673132 ENCRYPTED : 4QUwzz3Ugm2MlhoKjXczFA== DECRYPTED : 0112953009 ENCRYPTED : OTR7BXNnSfS0UPembrdypw== DECRYPTED : 0602903611 ENCRYPTED : n6npiRD5KC6SVUrgxlT82g== DECRYPTED : 0322223472 ENCRYPTED : 8StjcgyaqCTpI4hZ2hMhbg== DECRYPTED : 0552288393 ENCRYPTED : oVzOmMO+rKsBr7PuQ/p+6g== DECRYPTED : 0112708360 ENCRYPTED : zL2/rcyLChHKlU6UQMWb6Q== DECRYPTED : 0112487004 ENCRYPTED : M5hw0hnmkVMhDP88YH6/VA== DECRYPTED : 0812467344 ENCRYPTED : uKF7PRjMn7hqhTeVoAkXZQ== DECRYPTED : 0452225599 ENCRYPTED : icjrYU0TyXCyivKIGigcZA== DECRYPTED : 0662220971 Could you help me to find passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, and keySize ? |
|
#3
| |||
| |||
| hi, i am getting an error while executing this code at the line 1 at "using" saying that it requires class or interface,how can i correct it? |
|
#4
| |||
| |||
| Hello friends there are webdesigningcompany.net will help you to remove your web development error. I also use this site for my web developing application. This is really a good site for web development and web site design. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Decrypt Data Using a Symmetric (Rijndael) Key (VB.NET) | ruka13b | VB.NET Programming | 0 | 07-28-2008 10:51 PM |
| what algorithm used for garpage collection? | leoraja8 | Java Programming | 2 | 07-20-2007 07:43 AM |
| What mean by scheduling algorithm? | prasath | Operating Systems | 1 | 07-19-2007 12:10 PM |
| encrypt and decrypt query string | hanusoftware | VB.NET Programming | 0 | 06-07-2007 05:55 AM |
| Yahoo Algorithm | Joe | Yahoo | 0 | 02-23-2007 06:48 AM |
Our Partners |