C # Split byte [] array

I do RSA encryption and I have to split my long string into small bytes [] and encrypt them. Then I combine the arrays and convert to a string and write to a protected file.

Encryption then creates a byte [128]

I use this 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 a string, convert it to a byte [] array, and now I need to split it to decode the pieces, and then convert it to a string.

Any ideas?

thanks

EDIT:

I think that now a split works for me, but decryption fails. Is it because of RSA keys, etc.? In TimePointA, he encrypts it, then in TimePointB he tries to decrypt it and it fails. Public keys are different, so they are not sure if this is a problem.

+3
5

:

, 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
}

2: , , Convert.ToBase64String() Convert.FromBase64String() .

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;
}
+3

, :

        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
        }
+3

? .

0

" "?

, .

- .

0

why not use a framework instead of doing byte stuff yourself?

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

0
source

Source: https://habr.com/ru/post/1713214/


All Articles