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  
Old 07-18-2007, 12:59 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 245
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
  #2  
Old 07-27-2008, 06: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
  #3  
Old 02-05-2009, 05:04 AM
chitti chitti is offline
D-Web Trainee
 
Join Date: Feb 2009
Posts: 1
chitti is on a distinguished road
Default Re: Uses of a symmetric key algorithm (Rijndael) to encrypt and decrypt data.

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?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 10-01-2009, 02:27 AM
garuav123456 garuav123456 is offline
Banned
 
Join Date: Sep 2009
Posts: 31
garuav123456 is on a distinguished road
Default Re: Uses of a symmetric key algorithm (Rijndael) to encrypt and decrypt data.

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.
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 Off
Pingbacks are Off
Refbacks are Off

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


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


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0