简体   繁体   English

C#Split byte []数组

[英]C# Split byte[] array

I am doing RSA encryption and I have to split my long string into small byte[] and encrypt them. 我正在进行RSA加密,我必须将长字符串拆分为小字节[]并加密它们。 I then combine the arrays and convert to string and write to a secure file. 然后我组合数组并转换为字符串并写入安全文件。

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? 这是因为RSA密钥等吗? At TimePointA it encrypts it, then at TimePointB it tries to decrypt and it fails. 在TimePointA它加密它,然后在TimePointB它尝试解密它失败。 The public keys are different so not sure if that is the issue. 公钥是不同的,所以不确定是否是问题。

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. 此外,通常RSA用于加密AES等对称密钥,对称算法用于加密实际数据。 This is enormously faster for anything longer than 1 cipher block. 这是巨大的更快任何超过1个密码块长。 To decrypt the data, you decrypt the symmetric key with RSA, followed by decrypting the data with that key. 要解密数据,可以使用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;
}

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
        }

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. 固定长度的块,或根本没有块,将大大简化这一点。

"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. 别的什么都会给你带来胡言乱语。

why not use a framework instead of doing the byte-stuff yourself? 为什么不使用框架而不是自己做字节?

http://www.codinghorror.com/blog/archives/001275.html http://www.codinghorror.com/blog/archives/001275.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM