Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c# - RijndaelManaged can not decrypt

The code can be found at: http://pastebin.com/3Yg5bHra

My problem is, that when I decrypt then nothing gets returned at all. Nothing gets decrypted.

It goes wrong somewhere around line 111-114. The cryptoStream (csDecrypt) is empty, eventhough I put data into the memorystream (msDecrypt)

EDIT Nudier came up with a solution

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
//Function for encrypting propose
static string SymmetricEncryption(string str, byte[] key, byte[] IV)
{

MemoryStream ms = new MemoryStream();

try
{

//---creates a new instance of the RijndaelManaged class---
RijndaelManaged RMCrypto = new RijndaelManaged();

//---creates a new instance of the CryptoStream class---
CryptoStream cryptStream =new CryptoStream(ms, RMCrypto.CreateEncryptor(key, IV),

CryptoStreamMode.Write);

StreamWriter sWriter = new StreamWriter(cryptStream);

//---encrypting the string---
sWriter.Write(str);

sWriter.Close();

cryptStream.Close();

//---return the encrypted data as a string---
return System.Convert.ToBase64String(ms.ToArray());

}
catch (Exception ex)

{
Console.WriteLine(ex.ToString());

return (String.Empty);
}

}


//Function for Decrypting propose
static string SymmetricDecryption(string str, byte[] key, byte[] IV)
{

try

{

//---converts the encrypted string into a byte array---
byte[] b = System.Convert.FromBase64String(str);

//---converts the byte array into a memory stream for decryption---
MemoryStream ms = new MemoryStream(b);

//---creates a new instance of the RijndaelManaged class---
RijndaelManaged RMCrypto = new RijndaelManaged();

//---creates a new instance of the CryptoStream class---
CryptoStream cryptStream = new CryptoStream(ms, RMCrypto.CreateDecryptor(key, IV),

CryptoStreamMode.Read);

//---decrypting the stream---
StreamReader sReader = new StreamReader(cryptStream);

//---converts the decrypted stream into a string---
String s = sReader.ReadToEnd();

sReader.Close();

return s;

}
catch (Exception ex)
{

Console.WriteLine(ex.ToString());

return String.Empty;

}

}



//Main function execute the functions
RijndaelManaged RMCrypto = new RijndaelManaged();

//---generate key---
RMCrypto.GenerateKey();

byte[] key = RMCrypto.Key;

Console.WriteLine(“Key : “ + System.Convert.ToBase64String(key));

//---generate IV---
RMCrypto.GenerateIV();

byte[] IV = RMCrypto.IV;
Console.WriteLine(“IV : “ + System.Convert.ToBase64String(IV));

//---encrypt the string---
string cipherText = SymmetricEncryption(“This is a test string.”, key, IV);

Console.WriteLine(“Ciphertext: “ + cipherText);

//---decrypt the string---
Console.WriteLine(“Original string: “ + SymmetricDecryption(cipherText, key, IV));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...