<?xml version="1.0" encoding="iso-8859-9" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwizguide.com/rss_namespace/">
 <channel>
  <title>Datakent Forum : Rijndeal kullanım örneği</title>
  <link>http://forum.datakent.com/</link>
  <description>XML içerik linki; Datakent Forum : C# &amp; ASP.NET : Rijndeal kullanım örneği</description>
  <pubDate>Fri, 01 May 2026 05:45:28 +0000</pubDate>
  <lastBuildDate>Fri, 23 Mar 2012 09:30:45 +0000</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.54</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>forum.datakent.com/RSS_post_feed.asp?TID=2345</WebWizForums:feedURL>
  <image>
   <title>Datakent Forum</title>
   <url>http://forum.datakent.com/forum_images/datakent.com_forums.png</url>
   <link>http://forum.datakent.com/</link>
  </image>
  <item>
   <title>Rijndeal kullanım örneği :   //////////////////////////...</title>
   <link>http://forum.datakent.com/forum_posts.asp?TID=2345&amp;PID=4964#4964</link>
   <description>
    <![CDATA[<strong>Yazar:</strong> <a href="http://forum.datakent.com/member_profile.asp?PF=1" rel="nofollow">murat turan</a><br /><strong>Konu:</strong> 2345<br /><strong>Gönderim Zamanı:</strong> 23.Mart.2012 Saat 09:30<br /><br /><pre ="Code"><span ="CodeComment">///////////////////////////////////////////////////////////////////////////////// SAMPLE: Symmetric key encryption and decryption using Rijndael algorithm.// // To run this sample, create a new Visual C# project using the Console// Application template and replace the contents of the Class1.cs file with// the code below.//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.// // <a href="http://www.obviex.com/Legal.aspx#Samples" target="_blank"><u><font color="#000080" face="Courier New">Copyright (C) 2002 Obviex(TM)</font></u></a>. All rights reserved.//</span> <span ="CodeKeyword">using</span> System;<span ="CodeKeyword">using</span> System.IO;<span ="CodeKeyword">using</span> System.Text;<span ="CodeKeyword">using</span> System.Security.Cryptography;<span ="CodeComment">/// &lt;summary&gt;/// <span ="CodeComment">This class uses a symmetric key algorithm (Rijndael/AES) to encrypt and</span> /// <span ="CodeComment">decrypt data. As long as encryption and decryption routines use the same</span>/// <span ="CodeComment">parameters to generate the keys, the keys are guaranteed to be the same.</span>/// <span ="CodeComment">The class uses static functions with duplicate code to make it easier to</span>/// <span ="CodeComment">demonstrate encryption and decryption logic. In a real-life application,</span> /// <span ="CodeComment">this may not be the most efficient way of handling encryption, so - as</span>/// <span ="CodeComment">soon as you feel comfortable with it - you may want to redesign this class.</span>/// &lt;/summary&gt;</span><span ="CodeKeyword">public class</span> RijndaelSimple{    <span ="CodeComment">/// &lt;summary&gt;    /// <span ="CodeComment">Encrypts specified plaintext using Rijndael symmetric key algorithm</span>    /// <span ="CodeComment">and returns a base64-encoded result.</span>    /// &lt;/summary&gt;    /// &lt;param name="plainText"&gt;    /// <span ="CodeComment">Plaintext value to be encrypted.</span>    /// &lt;/param&gt;    /// &lt;param name="passPhrase"&gt;    /// <span ="CodeComment">Passphrase from which a pseudo-random password will be derived. The</span>    /// <span ="CodeComment">derived password will be used to generate the encryption key.</span>    /// <span ="CodeComment">Passphrase can be any string. In this example we assume that this</span>    /// <span ="CodeComment">passphrase is an ASCII string.</span>    /// &lt;/param&gt;    /// &lt;param name="saltValue"&gt;    /// <span ="CodeComment">Salt value used along with passphrase to generate password. Salt can</span>    /// <span ="CodeComment">be any string. In this example we assume that salt is an ASCII string.</span>    /// &lt;/param&gt;    /// &lt;param name="hashAlgorithm"&gt;    /// <span ="CodeComment">Hash algorithm used to generate password. Allowed values are: "MD5" and</span>    /// <span ="CodeComment">"SHA1". SHA1 hashes are a bit slower, but more secure than MD5 hashes.</span>    /// &lt;/param&gt;    /// &lt;param name="passwordIterations"&gt;    /// <span ="CodeComment">Number of iterations used to generate password. One or two iterations</span>    /// <span ="CodeComment">should be enough.</span>    /// &lt;/param&gt;    /// &lt;param name="initVector"&gt;    /// <span ="CodeComment">Initialization vector (or IV). This value is required to encrypt the</span>    /// <span ="CodeComment">first block of plaintext data. For RijndaelManaged class IV must be</span>     /// <span ="CodeComment">exactly 16 ASCII characters long.</span>    /// &lt;/param&gt;    /// &lt;param name="keySize"&gt;    /// <span ="CodeComment">Size of encryption key in bits. Allowed values are: 128, 192, and 256.</span>     /// <span ="CodeComment">Longer keys are more secure than shorter keys.</span>    /// &lt;/param&gt;    /// &lt;returns&gt;    /// <span ="CodeComment">Encrypted value formatted as a base64-encoded string.</span>    /// &lt;/returns&gt;</span>    <span ="CodeKeyword">public static string</span> Encrypt(<span ="CodeKeyword">string</span>   plainText,                                 <span ="CodeKeyword">string</span>   passPhrase,                                 <span ="CodeKeyword">string</span>   saltValue,                                 <span ="CodeKeyword">string</span>   hashAlgorithm,                                 <span ="CodeKeyword">int</span>      passwordIterations,                                 <span ="CodeKeyword">string</span>   initVector,                                 <span ="CodeKeyword">int</span>      keySize)    {        <span ="CodeComment">// Convert strings into byte arrays.        // Let us assume that strings only contain ASCII codes.        // If strings include Unicode characters, use Unicode, UTF7, or UTF8         // encoding.</span>        <span ="CodeKeyword">byte</span>&#091;&#093; initVectorBytes = Encoding.ASCII.GetBytes(initVector);        <span ="CodeKeyword">byte</span>&#091;&#093; saltValueBytes  = Encoding.ASCII.GetBytes(saltValue);                <span ="CodeComment">// Convert our plaintext into a byte array.        // Let us assume that plaintext contains UTF8-encoded characters.</span>        <span ="CodeKeyword">byte</span>&#091;&#093; plainTextBytes  = Encoding.UTF8.GetBytes(plainText);                <span ="CodeComment">// First, we must create a password, from which the key will be derived.        // This password will be generated from the specified passphrase and         // salt value. The password will be created using the specified hash         // algorithm. Password creation can be done in several iterations.</span>        PasswordDeriveBytes password = <span ="CodeKeyword">new</span> PasswordDeriveBytes(                                                        passPhrase,                                                         saltValueBytes,                                                         hashAlgorithm,                                                         passwordIterations);                <span ="CodeComment">// Use the password to generate pseudo-random bytes for the encryption        // key. Specify the size of the key in bytes (instead of bits).</span>        <span ="CodeKeyword">byte</span>&#091;&#093; keyBytes = password.GetBytes(keySize / <span ="CodeNumericLiteral">8</span>);                <span ="CodeComment">// Create uninitialized Rijndael encryption object.</span>        RijndaelManaged symmetricKey = <span ="CodeKeyword">new</span> RijndaelManaged();                <span ="CodeComment">// It is reasonable to set encryption mode to Cipher Block Chaining        // (CBC). Use default options for other symmetric key parameters.</span>        symmetricKey.Mode = CipherMode.CBC;                        <span ="CodeComment">// Generate encryptor from the existing key bytes and initialization         // vector. Key size will be defined based on the number of the key         // bytes.</span>        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(                                                         keyBytes,                                                          initVectorBytes);                <span ="CodeComment">// Define memory stream which will be used to hold encrypted data.</span>        MemoryStream memoryStream = <span ="CodeKeyword">new</span> MemoryStream();                                <span ="CodeComment">// Define cryptographic stream (always use Write mode for encryption).</span>        CryptoStream cryptoStream = <span ="CodeKeyword">new</span> CryptoStream(memoryStream,                                                      encryptor,                                                     CryptoStreamMode.Write);        <span ="CodeComment">// Start encrypting.</span>        cryptoStream.Write(plainTextBytes, <span ="CodeNumericLiteral">0</span>, plainTextBytes.Length);                        <span ="CodeComment">// Finish encrypting.</span>        cryptoStream.FlushFinalBlock();        <span ="CodeComment">// Convert our encrypted data from a memory stream into a byte array.</span>        <span ="CodeKeyword">byte</span>&#091;&#093; cipherTextBytes = memoryStream.ToArray();                        <span ="CodeComment">// Close both streams.</span>        memoryStream.Close();        cryptoStream.Close();                <span ="CodeComment">// Convert encrypted data into a base64-encoded string.</span>        <span ="CodeKeyword">string</span> cipherText = Convert.ToBase64String(cipherTextBytes);                <span ="CodeComment">// Return encrypted string.</span>        <span ="CodeKeyword">return</span> cipherText;    }        <span ="CodeComment">/// &lt;summary&gt;    /// <span ="CodeComment">Decrypts specified ciphertext using Rijndael symmetric key algorithm.</span>    /// &lt;/summary&gt;    /// &lt;param name="cipherText"&gt;    /// <span ="CodeComment">Base64-formatted ciphertext value.</span>    /// &lt;/param&gt;    /// &lt;param name="passPhrase"&gt;    /// <span ="CodeComment">Passphrase from which a pseudo-random password will be derived. The</span>    /// <span ="CodeComment">derived password will be used to generate the encryption key.</span>    /// <span ="CodeComment">Passphrase can be any string. In this example we assume that this</span>    /// <span ="CodeComment">passphrase is an ASCII string.</span>    /// &lt;/param&gt;    /// &lt;param name="saltValue"&gt;    /// <span ="CodeComment">Salt value used along with passphrase to generate password. Salt can</span>    /// <span ="CodeComment">be any string. In this example we assume that salt is an ASCII string.</span>    /// &lt;/param&gt;    /// &lt;param name="hashAlgorithm"&gt;    /// <span ="CodeComment">Hash algorithm used to generate password. Allowed values are: "MD5" and</span>    /// <span ="CodeComment">"SHA1". SHA1 hashes are a bit slower, but more secure than MD5 hashes.</span>    /// &lt;/param&gt;    /// &lt;param name="passwordIterations"&gt;    /// <span ="CodeComment">Number of iterations used to generate password. One or two iterations</span>    /// <span ="CodeComment">should be enough.</span>    /// &lt;/param&gt;    /// &lt;param name="initVector"&gt;    /// <span ="CodeComment">Initialization vector (or IV). This value is required to encrypt the</span>    /// <span ="CodeComment">first block of plaintext data. For RijndaelManaged class IV must be</span>    /// <span ="CodeComment">exactly 16 ASCII characters long.</span>    /// &lt;/param&gt;    /// &lt;param name="keySize"&gt;    /// <span ="CodeComment">Size of encryption key in bits. Allowed values are: 128, 192, and 256.</span>    /// <span ="CodeComment">Longer keys are more secure than shorter keys.</span>    /// &lt;/param&gt;    /// &lt;returns&gt;    /// <span ="CodeComment">Decrypted string value.</span>    /// &lt;/returns&gt;    /// &lt;remarks&gt;    /// <span ="CodeComment">Most of the logic in this function is similar to the Encrypt</span>    /// <span ="CodeComment">logic. In order for decryption to work, all parameters of this function</span>    /// <span ="CodeComment">- except cipherText value - must match the corresponding parameters of</span>    /// <span ="CodeComment">the Encrypt function which was called to generate the</span>    /// <span ="CodeComment">ciphertext.</span>    /// &lt;/remarks&gt;</span>    <span ="CodeKeyword">public static string</span> Decrypt(<span ="CodeKeyword">string</span>   cipherText,                                 <span ="CodeKeyword">string</span>   passPhrase,                                 <span ="CodeKeyword">string</span>   saltValue,                                 <span ="CodeKeyword">string</span>   hashAlgorithm,                                 <span ="CodeKeyword">int</span>      passwordIterations,                                 <span ="CodeKeyword">string</span>   initVector,                                 <span ="CodeKeyword">int</span>      keySize)    {        <span ="CodeComment">// Convert strings defining encryption key characteristics into byte        // arrays. Let us assume that strings only contain ASCII codes.        // If strings include Unicode characters, use Unicode, UTF7, or UTF8        // encoding.</span>        <span ="CodeKeyword">byte</span>&#091;&#093; initVectorBytes = Encoding.ASCII.GetBytes(initVector);        <span ="CodeKeyword">byte</span>&#091;&#093; saltValueBytes  = Encoding.ASCII.GetBytes(saltValue);                <span ="CodeComment">// Convert our ciphertext into a byte array.</span>        <span ="CodeKeyword">byte</span>&#091;&#093; cipherTextBytes = Convert.FromBase64String(cipherText);                <span ="CodeComment">// First, we must create a password, from which the key will be         // derived. This password will be generated from the specified         // passphrase and salt value. The password will be created using        // the specified hash algorithm. Password creation can be done in        // several iterations.</span>        PasswordDeriveBytes password = <span ="CodeKeyword">new</span> PasswordDeriveBytes(                                                        passPhrase,                                                         saltValueBytes,                                                         hashAlgorithm,                                                         passwordIterations);                <span ="CodeComment">// Use the password to generate pseudo-random bytes for the encryption        // key. Specify the size of the key in bytes (instead of bits).</span>        <span ="CodeKeyword">byte</span>&#091;&#093; keyBytes = password.GetBytes(keySize / <span ="CodeNumericLiteral">8</span>);                <span ="CodeComment">// Create uninitialized Rijndael encryption object.</span>        RijndaelManaged    symmetricKey = <span ="CodeKeyword">new</span> RijndaelManaged();                <span ="CodeComment">// It is reasonable to set encryption mode to Cipher Block Chaining        // (CBC). Use default options for other symmetric key parameters.</span>        symmetricKey.Mode = CipherMode.CBC;                <span ="CodeComment">// Generate decryptor from the existing key bytes and initialization         // vector. Key size will be defined based on the number of the key         // bytes.</span>        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(                                                         keyBytes,                                                          initVectorBytes);                <span ="CodeComment">// Define memory stream which will be used to hold encrypted data.</span>        MemoryStream  memoryStream = <span ="CodeKeyword">new</span> MemoryStream(cipherTextBytes);                        <span ="CodeComment">// Define cryptographic stream (always use Read mode for encryption).</span>        CryptoStream  cryptoStream = <span ="CodeKeyword">new</span> CryptoStream(memoryStream,                                                       decryptor,                                                      CryptoStreamMode.Read);        <span ="CodeComment">// Since at this point we don't know what the size of decrypted data        // will be, allocate the buffer long enough to hold ciphertext;        // plaintext is never longer than ciphertext.</span>        <span ="CodeKeyword">byte</span>&#091;&#093; plainTextBytes = <span ="CodeKeyword">new</span> <span ="CodeKeyword">byte</span>&#091;cipherTextBytes.Length&#093;;                <span ="CodeComment">// Start decrypting.</span>        <span ="CodeKeyword">int</span> decryptedByteCount = cryptoStream.Read(plainTextBytes,                                                    <span ="CodeNumericLiteral">0</span>,                                                    plainTextBytes.Length);                        <span ="CodeComment">// Close both streams.</span>        memoryStream.Close();        cryptoStream.Close();                <span ="CodeComment">// Convert decrypted data into a string.         // Let us assume that the original plaintext string was UTF8-encoded.</span>        <span ="CodeKeyword">string</span> plainText = Encoding.UTF8.GetString(plainTextBytes,                                                    <span ="CodeNumericLiteral">0</span>,                                                    decryptedByteCount);                <span ="CodeComment">// Return decrypted string.</span>           <span ="CodeKeyword">return</span> plainText;    }}<span ="CodeComment">/// &lt;summary&gt;/// <span ="CodeComment">Illustrates the use of RijndaelSimple class to encrypt and decrypt data.</span>/// &lt;/summary&gt;</span><span ="CodeKeyword">public class</span> RijndaelSimpleTest{    <span ="CodeComment">/// &lt;summary&gt;    /// <span ="CodeComment">The main entry point for the application.</span>    /// &lt;/summary&gt;</span>    &#091;STAThread&#093;    <span ="CodeKeyword">static void</span> Main(<span ="CodeKeyword">string</span>&#091;&#093; args)    {        <span ="CodeKeyword">string</span>   plainText          = <span ="CodeLiteral">"Hello, World!"</span>;    <span ="CodeComment">// original plaintext</span>                <span ="CodeKeyword">string</span>   passPhrase         = <span ="CodeLiteral">"Pas5pr@se"</span>;        <span ="CodeComment">// can be any string</span>        <span ="CodeKeyword">string</span>   saltValue          = <span ="CodeLiteral">"s@1tValue"</span>;        <span ="CodeComment">// can be any string</span>        <span ="CodeKeyword">string</span>   hashAlgorithm      = <span ="CodeLiteral">"SHA1"</span>;             <span ="CodeComment">// can be "MD5"</span>        <span ="CodeKeyword">int</span>      passwordIterations = <span ="CodeNumericLiteral">2</span>;                  <span ="CodeComment">// can be any number</span>        <span ="CodeKeyword">string</span>   initVector         = <span ="CodeLiteral">"@1B2c3D4e5F6g7H8"</span>; <span ="CodeComment">// must be 16 bytes</span>        <span ="CodeKeyword">int</span>      keySize            = <span ="CodeNumericLiteral">256</span>;                <span ="CodeComment">// can be 192 or 128</span>                Console.WriteLine(String.Format(<span ="CodeLiteral">"Plaintext : {0}"</span>, plainText));        <span ="CodeKeyword">string</span>  cipherText = RijndaelSimple.Encrypt(plainText,                                                    passPhrase,                                                    saltValue,                                                    hashAlgorithm,                                                    passwordIterations,                                                    initVector,                                                    keySize);        Console.WriteLine(String.Format(<span ="CodeLiteral">"Encrypted : {0}"</span>, cipherText));                plainText          = RijndaelSimple.Decrypt(cipherText,                                                    passPhrase,                                                    saltValue,                                                    hashAlgorithm,                                                    passwordIterations,                                                    initVector,                                                    keySize);        Console.WriteLine(String.Format(<span ="CodeLiteral">"Decrypted : {0}"</span>, plainText));    }}<span ="CodeComment">//// END OF FILE///////////////////////////////////////////////////////////////////////////////</span></pre>]]>
   </description>
   <pubDate>Fri, 23 Mar 2012 09:30:45 +0000</pubDate>
   <guid isPermaLink="true">http://forum.datakent.com/forum_posts.asp?TID=2345&amp;PID=4964#4964</guid>
  </item> 
 </channel>
</rss>