IT Community - Software Programming, Web Development and Technical Support

Uses of a symmetric key algorithm (Rijndael) to encrypt and decrypt data.

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Other Web Programming Languages

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-17-2007, 11:59 PM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Default Uses of a symmetric key algorithm (Rijndael) to encrypt and decrypt data.

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));
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-27-2008, 05:48 AM
ruka13b ruka13b is offline
D-Web Trainee
 
Join Date: Jul 2008
Posts: 2
ruka13b is on a distinguished road
Default Re: Uses of a symmetric key algorithm (Rijndael) to encrypt and decrypt data.

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 ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ConvexHull Algorithm C# Polygon Drawing kingmaker C# Programming 1 02-27-2008 08:40 PM
what algorithm used for garpage collection? leoraja8 Java Programming 2 07-20-2007 06:43 AM
What mean by scheduling algorithm? prasath Operating Systems 1 07-19-2007 11:10 AM
encrypt and decrypt query string hanusoftware VB.NET Programming 0 06-07-2007 04:55 AM
Yahoo Algorithm Joe Yahoo 0 02-23-2007 05:48 AM


All times are GMT -7. The time now is 10:30 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0