本文介绍了加密大字符串成功,加密文件失败-RSA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了本文中的一篇文章来生成我的代码,我将输入/输出字符串替换为字节,并且还进行了一些更改 [ ^ ]

我把它放在测试项目中,并上传到这里:
https://skydrive.live.com/embed?cid=DD24375C87B5BE07&resid= DD24375C87B5BE07%21586& authkey = ABdnCLJ8M3UTcAc [ ^ ]

我使用string完成了这两种方法,它也可以按照自己的文章程序所示工作,另一种方法是我传递给序列化的方法,但失败了:-/

同样由于本文的方法,将值拆分并合并,所以我可以说他想到了不对称密钥算法无法处理的大数据...

但我不知道为什么我失败了?

我删除了输出的基础65''es,并编写了一个小字节的[] appender算法,因此我直接将我的值传递给输出,但是接下来我在解密器中遇到错误,我不知道.Net如何处理这种编码,并且我正在寻找您的丰富帮助:|

您可以启动加密程序并查看重新启动信息,..."false"表示运行...


-------------------------------------------------- ----
这是我第二次尝试的内容:

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)
        {
            // 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;
        }

推荐答案

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);
        }



这篇关于加密大字符串成功,加密文件失败-RSA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:05