Reputation: 814
I know It's a probably common question but I cannot find answer anywhere. So I have byte array key and byte array value and I need to produce new 8 byte array that has been encrypted with DES in C#
Upvotes: 4
Views: 2689
Reputation: 814
This is what I was looking for :D:D...thank you :D
private static byte[] Encrypt(byte[] value, byte[] key)
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider
{
Mode = CipherMode.ECB,
Padding = PaddingMode.None
};
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);
cryptoStream.Write(value, 0, value.Length);
cryptoStream.Close();
return memoryStream.ToArray();
}
Upvotes: 1
Reputation: 2566
Here is your sample code. Remember to pad trailig zeros with random data, remember the bytes written, and DES parametrs: Key, IV.
Best wishes ;)
using System.Security.Cryptography;
using System.IO;
namespace hash
{
public static class Program
{
static void Main(string[] args)
{
byte[] data = new byte[10000];
DES des = DES.Create();
int bytesWritten = 0;
data = Encode(data, des, out bytesWritten);
}
private static byte[] Encode(byte[] data, DES des, out int bytesWritten)
{
using (var input = new MemoryStream(data))
using (var output = new MemoryStream())
using (var csp = new DESCryptoServiceProvider())
using (var encStream = new CryptoStream(output, csp.CreateEncryptor(des.Key, des.IV), CryptoStreamMode.Write))
{
int length = 0;
byte[] buffer = new byte[256];
bytesWritten = 0;
while ((length = input.Read(buffer, 0, 256)) > 0)
{
if (length < 256)
{
byte[] pad = new byte[256];
using (var rng = RNGCryptoServiceProvider.Create())
{
rng.GetBytes(pad);
for (int i = 0; i < 256 - length; i++)
{
buffer[length + i] = pad[i];
}
}
encStream.Write(buffer, 0, length);
bytesWritten += length;
break;
}
encStream.Write(buffer, 0, 256);
bytesWritten += length;
}
return output.ToArray();
}
}
}
}
Upvotes: 4