Jon*_*Jon 5 c# arrays split bytearray c#-3.0
我正在做 RSA 加密,我必须将我的长字符串分成小字节 [] 并加密它们。然后我组合数组并转换为字符串并写入安全文件。
然后加密创建字节[128]
我使用以下内容进行组合:
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;
}
Run Code Online (Sandbox Code Playgroud)
当我解密时,我将字符串转换为 byte[] 数组,现在需要拆分它以解码块,然后转换为字符串。
有任何想法吗?
谢谢
编辑:
我想我现在可以进行拆分,但是解密失败。这是因为RSA密钥等吗?在 TimePointA 它加密它,然后在 TimePointB 它试图解密但它失败了。公钥不同,所以不确定这是否是问题所在。
解密时,您可以为解密缓冲区创建一个数组并重用它:
此外,通常使用 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
}
Run Code Online (Sandbox Code Playgroud)
编辑 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21704 次 |
| 最近记录: |