Please login to save your progress.
Symmetric Cryptography
Exercise: Using 16 bits blocks
Please update this code to use 16 bits blocks.
x
1
using System;
2
using System.Text;
3
using System.Linq;
4
5
public class Program
6
{
7
public const int BLOCK_SIZE = 4;
8
9
public static void Main()
10
{
11
var text =
12
"Symmetric algorithms are pretty fast";
13
14
var result = GetBlocks(text);
15
16
var output = String.Join(" ", result);
17
18
Console.Write(output);
19
20
}
21
22
public static uint[] GetBlocks(string text)
23
{
24
var bytes = Encoding.ASCII.GetBytes(text);
25
26
var blocksCount = (int)Math.Ceiling(bytes.Count() / (double)BLOCK_SIZE);
27
28
var result = new uint[blocksCount];
29
30
for (int i = 0; i < blocksCount; i++)
31
{
32
result[i] = ToUInt32(bytes.Skip(i * BLOCK_SIZE).Take(BLOCK_SIZE).ToArray());
33
}
34
35
return result;
36
}
37
38
public static UInt32 ToUInt32(byte[] bytes)
39
{
40
UInt32 result = 0;
41
42
for (int i = 0; i < 4; i++)
43
{
44
result += (UInt32) Math.Pow(256, 3 - i)*bytes[i];
45
}
46
47
return result;
48
}
49
}
Page 4 of 11