Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Power Lifting Lever Belt
SkullVamp
Updated:
Saturday at 10:32 PM
Ad icon
port.lk Domain for sale
Lankan-Tech
Updated:
Saturday at 3:55 PM
Colombo
Kaduwela - Two Storey House for Sale
dilrasan
Updated:
Thursday at 2:23 PM
Ad icon
Wechat qr verification
Pawan2005
Updated:
Thursday at 1:28 AM
🚀 GOOGLE AI PRO 18 MONTHS ACTIVATION 🚀
sayuru bandara
Updated:
Wednesday at 5:34 PM
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Talk!
C# AES 256 Encryption
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="EGhewg5" data-source="post: 23715779" data-attributes="member: 567887"><p>Machan me code eka balala improve karaganna dewal thiyanawanam kiyanawada...</p><p>Mata programming loku danumak naha. String, password wage dewal encrypt karaganna class ekak hada gannayi try karanne.</p><p></p><p>Me code eka gaththa source eka...</p><p><a href="https://stackoverflow.com/questions/45567962" target="_blank">https://stackoverflow.com/questions/45567962</a></p><p></p><p>[CODE]</p><p> private void Usage()</p><p> {</p><p> string message = "My secret message 1234";</p><p> string password = "3sc3RLrpd17";</p><p></p><p> // Create sha256 hash</p><p> SHA256 mySHA256 = SHA256Managed.Create();</p><p> byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password));</p><p></p><p> // Create secret IV</p><p> byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };</p><p></p><p> //using (var aes = new AesCryptoServiceProvider())</p><p> //{</p><p> // aes.GenerateIV();</p><p> //} </p><p></p><p> string encrypted = this.EncryptString(message, key, iv);</p><p> string decrypted = this.DecryptString(encrypted, key, iv);</p><p></p><p> Console.WriteLine(encrypted);</p><p> Console.WriteLine(decrypted);</p><p> }</p><p>[/CODE]</p><p></p><p></p><p>[CODE]</p><p> public string EncryptString(string plainText, byte[] key, byte[] iv)</p><p> {</p><p> // Instantiate a new Aes object to perform string symmetric encryption</p><p> Aes encryptor = Aes.Create();</p><p></p><p> encryptor.Mode = CipherMode.CBC;</p><p></p><p> // Set key and IV</p><p> byte[] aesKey = new byte[32];</p><p> Array.Copy(key, 0, aesKey, 0, 32);</p><p> encryptor.Key = aesKey;</p><p> encryptor.IV = iv;</p><p></p><p> // Instantiate a new MemoryStream object to contain the encrypted bytes</p><p> MemoryStream memoryStream = new MemoryStream();</p><p></p><p> // Instantiate a new encryptor from our Aes object</p><p> ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();</p><p></p><p> // Instantiate a new CryptoStream object to process the data and write it to the </p><p> // memory stream</p><p> CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);</p><p></p><p> // Convert the plainText string into a byte array</p><p> byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);</p><p></p><p> // Encrypt the input plaintext string</p><p> cryptoStream.Write(plainBytes, 0, plainBytes.Length);</p><p></p><p> // Complete the encryption process</p><p> cryptoStream.FlushFinalBlock();</p><p></p><p> // Convert the encrypted data from a MemoryStream to a byte array</p><p> byte[] cipherBytes = memoryStream.ToArray();</p><p></p><p> // Close both the MemoryStream and the CryptoStream</p><p> memoryStream.Close();</p><p> cryptoStream.Close();</p><p></p><p> // Convert the encrypted byte array to a base64 encoded string</p><p> string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);</p><p></p><p> // Return the encrypted data as a string</p><p> return cipherText;</p><p> }</p><p>[/CODE]</p><p></p><p>[CODE]</p><p> public string DecryptString(string cipherText, byte[] key, byte[] iv)</p><p> {</p><p> // Instantiate a new Aes object to perform string symmetric encryption</p><p> Aes encryptor = Aes.Create();</p><p></p><p> encryptor.Mode = CipherMode.CBC;</p><p></p><p> // Set key and IV</p><p> byte[] aesKey = new byte[32];</p><p> Array.Copy(key, 0, aesKey, 0, 32);</p><p> encryptor.Key = aesKey;</p><p> encryptor.IV = iv;</p><p></p><p> // Instantiate a new MemoryStream object to contain the encrypted bytes</p><p> MemoryStream memoryStream = new MemoryStream();</p><p></p><p> // Instantiate a new encryptor from our Aes object</p><p> ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();</p><p></p><p> // Instantiate a new CryptoStream object to process the data and write it to the </p><p> // memory stream</p><p> CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write);</p><p></p><p> // Will contain decrypted plaintext</p><p> string plainText = String.Empty;</p><p></p><p> try</p><p> {</p><p> // Convert the ciphertext string into a byte array</p><p> byte[] cipherBytes = Convert.FromBase64String(cipherText);</p><p></p><p> // Decrypt the input ciphertext string</p><p> cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);</p><p></p><p> // Complete the decryption process</p><p> cryptoStream.FlushFinalBlock();</p><p></p><p> // Convert the decrypted data from a MemoryStream to a byte array</p><p> byte[] plainBytes = memoryStream.ToArray();</p><p></p><p> // Convert the decrypted byte array to string</p><p> plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);</p><p> }</p><p> finally</p><p> {</p><p> // Close both the MemoryStream and the CryptoStream</p><p> memoryStream.Close();</p><p> cryptoStream.Close();</p><p> }</p><p></p><p> // Return the decrypted data as a string</p><p> return plainText;</p><p> }</p><p>[/CODE]</p></blockquote><p></p>
[QUOTE="EGhewg5, post: 23715779, member: 567887"] Machan me code eka balala improve karaganna dewal thiyanawanam kiyanawada... Mata programming loku danumak naha. String, password wage dewal encrypt karaganna class ekak hada gannayi try karanne. Me code eka gaththa source eka... [url]https://stackoverflow.com/questions/45567962[/url] [CODE] private void Usage() { string message = "My secret message 1234"; string password = "3sc3RLrpd17"; // Create sha256 hash SHA256 mySHA256 = SHA256Managed.Create(); byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password)); // Create secret IV byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; //using (var aes = new AesCryptoServiceProvider()) //{ // aes.GenerateIV(); //} string encrypted = this.EncryptString(message, key, iv); string decrypted = this.DecryptString(encrypted, key, iv); Console.WriteLine(encrypted); Console.WriteLine(decrypted); } [/CODE] [CODE] public string EncryptString(string plainText, byte[] key, byte[] iv) { // Instantiate a new Aes object to perform string symmetric encryption Aes encryptor = Aes.Create(); encryptor.Mode = CipherMode.CBC; // Set key and IV byte[] aesKey = new byte[32]; Array.Copy(key, 0, aesKey, 0, 32); encryptor.Key = aesKey; encryptor.IV = iv; // Instantiate a new MemoryStream object to contain the encrypted bytes MemoryStream memoryStream = new MemoryStream(); // Instantiate a new encryptor from our Aes object ICryptoTransform aesEncryptor = encryptor.CreateEncryptor(); // Instantiate a new CryptoStream object to process the data and write it to the // memory stream CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write); // Convert the plainText string into a byte array byte[] plainBytes = Encoding.ASCII.GetBytes(plainText); // Encrypt the input plaintext string cryptoStream.Write(plainBytes, 0, plainBytes.Length); // Complete the encryption process cryptoStream.FlushFinalBlock(); // Convert the encrypted data from a MemoryStream to a byte array byte[] cipherBytes = memoryStream.ToArray(); // Close both the MemoryStream and the CryptoStream memoryStream.Close(); cryptoStream.Close(); // Convert the encrypted byte array to a base64 encoded string string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length); // Return the encrypted data as a string return cipherText; } [/CODE] [CODE] public string DecryptString(string cipherText, byte[] key, byte[] iv) { // Instantiate a new Aes object to perform string symmetric encryption Aes encryptor = Aes.Create(); encryptor.Mode = CipherMode.CBC; // Set key and IV byte[] aesKey = new byte[32]; Array.Copy(key, 0, aesKey, 0, 32); encryptor.Key = aesKey; encryptor.IV = iv; // Instantiate a new MemoryStream object to contain the encrypted bytes MemoryStream memoryStream = new MemoryStream(); // Instantiate a new encryptor from our Aes object ICryptoTransform aesDecryptor = encryptor.CreateDecryptor(); // Instantiate a new CryptoStream object to process the data and write it to the // memory stream CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write); // Will contain decrypted plaintext string plainText = String.Empty; try { // Convert the ciphertext string into a byte array byte[] cipherBytes = Convert.FromBase64String(cipherText); // Decrypt the input ciphertext string cryptoStream.Write(cipherBytes, 0, cipherBytes.Length); // Complete the decryption process cryptoStream.FlushFinalBlock(); // Convert the decrypted data from a MemoryStream to a byte array byte[] plainBytes = memoryStream.ToArray(); // Convert the decrypted byte array to string plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length); } finally { // Close both the MemoryStream and the CryptoStream memoryStream.Close(); cryptoStream.Close(); } // Return the decrypted data as a string return plainText; } [/CODE] [/QUOTE]
Insert quotes…
Verification
Dawasata paya keeyak thibeda?
Post reply
Top
Bottom