Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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 :
C#
public static byte[] Encrypt(byte[] bytes, int dwKeySize, string xmlString, bool reverse, bool fOAEP)
        {
            // TODO: Add Proper Exception Handlers
            RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
            rsaCryptoServiceProvider.FromXmlString(xmlString);
            int keySize = dwKeySize / 8;
            // The hash function in use by the .NET RSACryptoServiceProvider here is SHA1
            // int maxLength = ( keySize ) - 2 - ( 2 * SHA1.Create().ComputeHash( rawBytes ).Length );
            int maxLength = keySize - 42;
            int dataLength = bytes.Length;
            int iterations = dataLength / maxLength;
            //StringBuilder stringBuilder = new StringBuilder();
            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);
                // Be aware the RSACryptoServiceProvider reverses the order of encrypted bytes after encryption and before decryption.
                // If you do not require compatibility with Microsoft Cryptographic API (CAPI) and/or other vendors.
                // Comment out the next line and the corresponding one in the DecryptString function.
                if (reverse) Array.Reverse(encryptedBytes);
                // Why convert to base 64?
                // Because it is the largest power-of-two base printable using only ASCII characters
                //stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
                resault = AppendBytes(resault, encryptedBytes);
            }
            rsaCryptoServiceProvider.Clear();
            return resault; //Convert.FromBase64String(stringBuilder.ToString());
        }

        public static byte[] Decrypt(byte[] EncBytes, int dwKeySize, string xmlString, bool reverse, bool fOAEP)
        {
            string inputString = Convert.ToBase64String(EncBytes);
            // TODO: Add Proper Exception Handlers
            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));
                // Be aware the RSACryptoServiceProvider reverses the order of encrypted bytes after encryption and before decryption.
                // If you do not require compatibility with Microsoft Cryptographic API (CAPI) and/or other vendors.
                // Comment out the next line and the corresponding one in the EncryptString function.
                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;
        }
Posted

i write this, and tried to use this to split but still generate error :-s

C#
private static byte[] SubBytes(byte[] inputBytes, int startIndex, int count)
        {
            if (startIndex>inputBytes.Length) return new byte[0];

            int actualCount = -1;
            //Determinate available count
            if (inputBytes.Length < startIndex + count)
                actualCount = (startIndex + count) - inputBytes.Length;
            if (actualCount == -1 || actualCount > count)
                actualCount = count;
            //Iterate and get resault bytes
            byte[] resault = new byte[actualCount];
            int rIndex = 0;
            for (int i = startIndex; i < startIndex + actualCount; i++)
            {
                resault[rIndex] = inputBytes[i];
            }
        }

        private static byte[] SubBytes(byte[] inputBytes, int offset, int section, int size)
        {
            int ActualStartIndex=(section*size)+offset;
            return SubBytes(inputBytes, ActualStartIndex, size);
        }
 
Share this answer
 
first of all, i used 128 byte spliter, to split data, and it worked fine :|

second : some one posted here, i didnt still try it out, but i think that can be answer:http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a831d26e-e917-431b-ab50-34735f333cd3[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900