Alex Dsouza

Alex Dsouza

  • NA
  • 112
  • 5k

Please check rsa file encryption algorithm code..and update answer.

Feb 25 2021 4:11 PM
https://www.youtube.com/watch?v=HWNcfxUadUs I am trying to create this project given in link and above posted code of rsa file encryption algorithm please check code i have done using rsa crypto provider...is it correct way or not??Please check... yes or no..If no then what is the correct code? in asp.net c# webforms.
 
 
//RSAencrypt function
public bool RSAencryptfile(string filename)
{
FileStream f;
try
{
f = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
}
catch (Exception e)
{
throw e;
//return false;
}
Random r = new Random();
Thread.Sleep(10 * r.Next(3, 8));
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
rsa.FromXmlString("<RSAKeyValue><Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");
// Import key
byte[] data = new byte[16];
ArrayList con = new ArrayList();
while (f.Read(data, 0, 16) != 0)
{
byte[] result = rsa.Encrypt(data, false);
//Thread.Sleep(10 * r.Next(2, 7));
con.Add(result);
}
// encryption
f.Seek(0, SeekOrigin.Begin);
foreach (byte[] d in con)
{
f.Write(d, 0, d.Length);
}
f.Close();
return true;
}
//RSAdecrypt function
public bool RSAdecryptFile(string filename, string key)
{
FileStream f;
try
{
f = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
}
catch
{
return false;
}
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
try
{
rsa.FromXmlString(key);
}
catch
{
return false;
}
byte[] data = new byte[128];
ArrayList con = new ArrayList();
while (f.Read(data, 0, 128) != 0)
{
byte[] result;
try
{
result = rsa.Decrypt(data, false);
}
catch (Exception e)
{
throw e;
//return false;
}
con.Add(result);
}
// decryption
f.SetLength(con.Count * 16);
f.Seek(0, SeekOrigin.Begin);
foreach (byte[] d in con)
{
f.Write(d, 0, d.Length);
}
f.Close();
return true;
}
//In aspx.cs
string privatekey = "<RSAKeyValue><Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus><Exponent>AQAB</Exponent><P>/aULPE6jd5IkwtWXmReyMUhmI/nfwfkQSyl7tsg2PKdpcxk4mpPZUdEQhHQLvE84w2DhTyYkPHCtq/mMKE3MHw==</P><Q>3WV46X9Arg2l9cxb67KVlNVXyCqc/w+LWt/tbhLJvV2xCF/0rWKPsBJ9MC6cquaqNPxWWEav8RAVbmmGrJt51Q==</Q><DP>8TuZFgBMpBoQcGUoS2goB4st6aVq1FcG0hVgHhUI0GMAfYFNPmbDV3cY2IBt8Oj/uYJYhyhlaj5YTqmGTYbATQ==</DP><DQ>FIoVbZQgrAUYIHWVEYi/187zFd7eMct/Yi7kGBImJStMATrluDAspGkStCWe4zwDDmdam1XzfKnBUzz3AYxrAQ==</DQ><InverseQ>QPU3Tmt8nznSgYZ+5jUo9E0SfjiTu435ihANiHqqjasaUNvOHKumqzuBZ8NRtkUhS6dsOEb8A2ODvy7KswUxyA==</InverseQ><D>cgoRoAUpSVfHMdYXW9nA3dfX75dIamZnwPtFHq80ttagbIe4ToYYCcyUz5NElhiNQSESgS5uCgNWqWXt5PnPu4XmCXx6utco1UVH8HGLahzbAnSy6Cj3iUIQ7Gj+9gQ7PkC434HTtHazmxVgIR5l56ZjoQ8yGNCPZnsdYEmhJWk=</D></RSAKeyValue>";

Answers (6)