i used one of article in here to generate my code, i replace input/output string with bytes, and also perform some changes
Public Key RSA Encryption in C# .NET[
^]
i put it in test project, and upload it here:
https://skydrive.live.com/embed?cid=DD24375C87B5BE07&resid=DD24375C87B5BE07%21586&authkey=ABdnCLJ8M3UTcAc[
^]
i done both method, using string , which it work also as shown in it's own article program, the other as a method that i pass to serialize which fail :-/
also since this article method, split and concat the values, so i can say he think about larg data, which Asymmetric key algorithm cant handle...
but i'm wonder why i failed?
i remove base 65'es in out put, and write a small byte[] append-er algorithm , so i directly pass my value to output, but next i face error in decrypter, i dunno how .Net handle this encoding, and i'm looking for rich help from you :|
you can launch the encrypter and see resault,... 'false' mean run ...
------------------------------------------------------
it's what i tried second time :
public static byte[] Encrypt(byte[] bytes, int dwKeySize, string xmlString, bool reverse, bool fOAEP)
{
RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
rsaCryptoServiceProvider.FromXmlString(xmlString);
int keySize = dwKeySize / 8;
int maxLength = keySize - 42;
int dataLength = bytes.Length;
int iterations = dataLength / maxLength;
byte[] resault = new byte[0];
for (int i = 0; i <= iterations; i++)
{
byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];
Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length);
byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, fOAEP);
if (reverse) Array.Reverse(encryptedBytes);
resault = AppendBytes(resault, encryptedBytes);
}
rsaCryptoServiceProvider.Clear();
return resault;
}
public static byte[] Decrypt(byte[] EncBytes, int dwKeySize, string xmlString, bool reverse, bool fOAEP)
{
string inputString = Convert.ToBase64String(EncBytes);
RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
rsaCryptoServiceProvider.FromXmlString(xmlString);
int base64BlockSize = ((dwKeySize / 8) % 3 != 0) ? (((dwKeySize / 8) / 3) * 4) + 4 : ((dwKeySize / 8) / 3) * 4;
int iterations = inputString.Length / base64BlockSize;
ArrayList arrayList = new ArrayList();
for (int i = 0; i < iterations; i++)
{
byte[] encryptedBytes = Convert.FromBase64String(inputString.Substring(base64BlockSize * i, base64BlockSize));
if (reverse) Array.Reverse(encryptedBytes);
arrayList.AddRange(rsaCryptoServiceProvider.Decrypt(encryptedBytes, fOAEP));
}
rsaCryptoServiceProvider.Clear();
return arrayList.ToArray(Type.GetType("System.Byte")) as byte[];
}
private static byte[] AppendBytes(byte[] a, byte[] b)
{
int totalLenght = a.Length + b.Length;
byte[] resault = new byte[totalLenght];
int offset = 0;
for (int i = 0; i < a.Length; i++)
{
resault[offset] = a[i];
offset++;
}
for (int i = 0; i < b.Length; i++)
{
resault[offset] = b[i];
offset++;
}
return resault;
}