C#拆分字节[]数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1163921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Split byte[] array
提问by Jon
I am doing RSA encryption and I have to split my long string into small byte[] and encrypt them. I then combine the arrays and convert to string and write to a secure file.
我正在做 RSA 加密,我必须将我的长字符串分成小字节 [] 并加密它们。然后我组合数组并转换为字符串并写入安全文件。
Then encryption creates byte[128]
然后加密创建字节[128]
I use this the following to combine:
我使用以下内容进行组合:
public static byte[] Combine(params byte[][] arrays)
{
byte[] ret = new byte[arrays.Sum(x => x.Length)];
int offset = 0;
foreach (byte[] data in arrays)
{
Buffer.BlockCopy(data, 0, ret, offset, data.Length);
offset += data.Length;
}
return ret;
}
When I decrypt I take the string, convert it to a byte[] array and now need to split it to decode the chunks and then convert to string.
当我解密时,我将字符串转换为 byte[] 数组,现在需要拆分它以解码块,然后转换为字符串。
Any ideas?
有任何想法吗?
Thanks
谢谢
EDIT:
编辑:
I think I have the split working now however the decryption fails. Is this because of RSA keys etc? At TimePointA it encrypts it, then at TimePointB it tries to decrypt and it fails. The public keys are different so not sure if that is the issue.
我想我现在可以进行拆分,但是解密失败。这是因为RSA密钥等吗?在 TimePointA 它加密它,然后在 TimePointB 它试图解密但它失败了。公钥不同,所以不确定这是否是问题所在。
采纳答案by Sam Harwell
When you decrypt, you can create one array for your decrypt buffer and reuse it:
解密时,您可以为解密缓冲区创建一个数组并重用它:
Also, normally RSA gets used to encrypt a symmetric key for something like AES, and the symmetric algorithm is used to encrypt the actual data. This is enormouslyfaster for anything longer than 1 cipher block. To decrypt the data, you decrypt the symmetric key with RSA, followed by decrypting the data with that key.
此外,通常 RSA 用于加密诸如 AES 之类的对称密钥,而对称算法用于加密实际数据。这是巨大的更快任何超过1个密码块长。要解密数据,您需要使用 RSA 解密对称密钥,然后使用该密钥解密数据。
byte[] buffer = new byte[BlockLength];
// ASSUMES SOURCE IS padded to BlockLength
for (int i = 0; i < source.Length; i += BlockLength)
{
Buffer.BlockCopy(source, i, buffer, 0, BlockLength);
// ... decode buffer and copy the result somewhere else
}
Edit 2: If you are storing the data as strings and not as raw bytes, use Convert.ToBase64String()
and Convert.FromBase64String()
as the safest conversion solution.
编辑 2:如果您将数据存储为字符串而不是原始字节,请使用Convert.ToBase64String()
和Convert.FromBase64String()
作为最安全的转换解决方案。
Edit 3: From his edit:
编辑 3:来自他的编辑:
private static List<byte[]> splitByteArray(string longString)
{
byte[] source = Convert.FromBase64String(longString);
List<byte[]> result = new List<byte[]>();
for (int i = 0; i < source.Length; i += 128)
{
byte[] buffer = new byte[128];
Buffer.BlockCopy(source, i, buffer, 0, 128);
result.Add(buffer);
}
return result;
}
回答by Matt Howells
Why do you need to break the string into variable length chunks? Fixed-length chunks, or no chunks at all, would simplify this a lot.
为什么需要将字符串分成可变长度的块?固定长度的块,或者根本没有块,会简化很多。
回答by Matt Jacobsen
I'd say something like this would do it:
我会说这样的事情会做到:
byte[] text = Encoding.UTF8.GetBytes(longString);
int len = 128;
for (int i = 0; i < text.Length; )
{
int j = 0;
byte[] chunk = new byte[len];
while (++j < chunk.Length && i < text.Length)
{
chunk[j] = text[i++];
}
Convert(chunk); //do something with the chunk
}
回答by Will
"the public keys are different"?
“公钥不同”?
You encrypt with a private key, and decrypt with the public key that corresponds to the private key.
您使用私钥加密,并使用与私钥对应的公钥解密。
Anything else will give you gibberish.
其他任何事情都会让你胡言乱语。
回答by Sander
why not use a framework instead of doing the byte-stuff yourself?
为什么不使用框架而不是自己做字节的东西?