Click here to Skip to main content
15,867,453 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Hit counter on a web page Pin
JacSophie9-Jan-10 8:24
JacSophie9-Jan-10 8:24 
GeneralRe: Hit counter on a web page Pin
HarrisonB9-Jan-10 8:56
HarrisonB9-Jan-10 8:56 
GeneralRe: Hit counter on a web page Pin
JacSophie9-Jan-10 12:56
JacSophie9-Jan-10 12:56 
QuestionImage from Database Fields Pin
eddieangel8-Jan-10 6:04
eddieangel8-Jan-10 6:04 
AnswerRe: Image from Database Fields Pin
Ennis Ray Lynch, Jr.8-Jan-10 10:28
Ennis Ray Lynch, Jr.8-Jan-10 10:28 
Questioncss problem Pin
Spurple8-Jan-10 4:24
Spurple8-Jan-10 4:24 
AnswerRe: css problem Pin
Abhijit Jana8-Jan-10 5:03
professionalAbhijit Jana8-Jan-10 5:03 
Questionstring Encryption and decryption problem Pin
Sudhanshu Mani Tripathi8-Jan-10 3:43
Sudhanshu Mani Tripathi8-Jan-10 3:43 
I am using below code to encrypt and decrypt querystring value .it works fine
but it show nullrefrence exception when
some works not performed in mysite for more than 15-20 minute.
it shows savedKey and savedIV must be non-null." error
I am not analyzing why this error occur .




private static byte[] savedKey = null;
private static byte[] savedIV = null;


public static byte[] key
{
get { return savedKey; }
set { savedKey = value; }
}

public static byte[] IV
{
get { return savedIV; }
set { savedIV = value; }
}

private static void rdGenerateSecretKey(RijndaelManaged rdProvider)
{
if (savedKey == null)
{
rdProvider.KeySize = 256;
rdProvider.GenerateKey();
savedKey = rdProvider.Key;
}

}

private static void rdGenerateSecreInitVector(RijndaelManaged rdProvider)
{
if (savedIV == null)
{
rdProvider.GenerateIV();
savedIV = rdProvider.IV;
}

}

public static string Encrypt(string originalStr)
{
Random RandNum = new Random();
int RandomNumber = RandNum.Next(1000000, 9999999);
originalStr = String.Format("{0}&Random={1}", originalStr, RandomNumber.ToString());
//encode data string to be stored in memory
byte[] originalStrAsBytes = Encoding.ASCII.GetBytes(originalStr);
byte[] originalBytes = { };

// create memory stream to contain output.
using (MemoryStream memStream = new MemoryStream(originalStrAsBytes.Length))
{
using (RijndaelManaged rijndel = new RijndaelManaged())
{

// generate secret ket and save in to vector.
rdGenerateSecretKey(rijndel);
rdGenerateSecreInitVector(rijndel);

if (savedKey == null || savedIV == null)
{
throw (new NullReferenceException("savedKey and savedIV must be non-null."));
}
// Create Encrypt and Stream Objects.
using (ICryptoTransform rdTransform=rijndel.CreateEncryptor((byte[]) savedKey.Clone(),(byte[]) savedIV.Clone()))
{

using (CryptoStream cryptostream =new CryptoStream(memStream,rdTransform,CryptoStreamMode.Write))
{

// write crypto stream data in memeory stream
cryptostream.Write(originalStrAsBytes,0,originalStrAsBytes.Length);
cryptostream.FlushFinalBlock();
originalBytes=memStream.ToArray();
}
}

}
}

// convert encrypted string
string encryptedStr=Convert.ToBase64String(originalBytes);
return(encryptedStr);

}


public static string Decrypt(string encryptedStr)
{
// unconvert encrypted string.
encryptedStr = encryptedStr.Replace(" ", "+");

byte[] encryptedAsBytes = Convert.FromBase64String(encryptedStr);
byte[] intialText = new byte[encryptedAsBytes.Length];

using (RijndaelManaged rijndael = new RijndaelManaged())
{
using (MemoryStream memStream = new MemoryStream(encryptedAsBytes))
{

if (savedKey == null || savedIV == null)
{

throw (new NullReferenceException("savedKey and savedIV must be non-null. "));
}

// Create Decrypt and Stream Objects.
using (ICryptoTransform rdTransform = rijndael.CreateDecryptor((byte[])savedKey.Clone(), (byte[])savedIV.Clone()))
{

using (CryptoStream cryptostream = new CryptoStream(memStream, rdTransform, CryptoStreamMode.Read))
{

// Read decryte string as bytes
cryptostream.Read(intialText, 0, intialText.Length);
}
}
}

// convert byte[] to string

string decryptedStr = Encoding.ASCII.GetString(intialText);
return (decryptedStr);
}

}

public static System.Collections.Hashtable FillQs(HttpRequest request)
{

char[] SepMain = { '&' };
char[] SepSub = { '=' };
string Test = string.Empty;
System.Collections.Hashtable hasTableQueryStringId = new System.Collections.Hashtable();

if (request.QueryString["Q"] != null)
{
string[] Param = Decrypt(request.QueryString["Q"].ToString()).Split(SepMain, StringSplitOptions.RemoveEmptyEntries);


foreach (string str in Param)
{
string[] ParamKey = str.Split(SepSub);

//request.QueryString.Set(request.QueryString[ParamKey[0].ToString()], ParamKey[1].ToString());
//request.QueryString[ParamKey[0].ToString()] = ParamKey[1].ToString();
hasTableQueryStringId.Add(ParamKey[0].ToString(), ParamKey[1].ToString());
}
}
else
{
hasTableQueryStringId.Add("NoAttachments", "NoAttachments");
}
return hasTableQueryStringId;

}
AnswerRe: string Encryption and decryption problem Pin
Covean8-Jan-10 4:07
Covean8-Jan-10 4:07 
QuestionDeploy Steps Pin
arkiboys8-Jan-10 2:58
arkiboys8-Jan-10 2:58 
AnswerRe: Deploy Steps Pin
Abhijit Jana8-Jan-10 3:40
professionalAbhijit Jana8-Jan-10 3:40 
QuestionHow to convert word document pdf in asp.net ?? Pin
JC.KaNNaN8-Jan-10 2:40
JC.KaNNaN8-Jan-10 2:40 
AnswerRe: How to convert word document pdf in asp.net ?? Pin
annathor8-Jan-10 3:08
annathor8-Jan-10 3:08 
AnswerRe: How to convert word document pdf in asp.net ?? Pin
HimaBindu Vejella8-Jan-10 3:12
HimaBindu Vejella8-Jan-10 3:12 
GeneralRe: How to convert word document pdf in asp.net ?? Pin
JC.KaNNaN9-Jan-10 6:22
JC.KaNNaN9-Jan-10 6:22 
QuestionDynamically getting values from database to javascript array Pin
dsaikrishna8-Jan-10 1:10
dsaikrishna8-Jan-10 1:10 
AnswerRe: Dynamically getting values from database to javascript array Pin
Not Active8-Jan-10 2:29
mentorNot Active8-Jan-10 2:29 
AnswerOnly cause Mark Said you can't Pin
Ennis Ray Lynch, Jr.8-Jan-10 10:30
Ennis Ray Lynch, Jr.8-Jan-10 10:30 
QuestionA site with multiple languages... Pin
awedaonline8-Jan-10 0:43
awedaonline8-Jan-10 0:43 
AnswerRe: A site with multiple languages... Pin
Abhijit Jana8-Jan-10 1:31
professionalAbhijit Jana8-Jan-10 1:31 
GeneralRe: A site with multiple languages... Pin
awedaonline8-Jan-10 4:28
awedaonline8-Jan-10 4:28 
GeneralRe: A site with multiple languages... Pin
Abhijit Jana8-Jan-10 5:04
professionalAbhijit Jana8-Jan-10 5:04 
QuestionForeign key relationship in same table Pin
Amit Patel19858-Jan-10 0:43
Amit Patel19858-Jan-10 0:43 
AnswerRe: Foreign key relationship in same table Pin
awedaonline8-Jan-10 0:52
awedaonline8-Jan-10 0:52 
GeneralRe: Foreign key relationship in same table Pin
Amit Patel19858-Jan-10 1:19
Amit Patel19858-Jan-10 1:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.