-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge18.cs
executable file
·48 lines (40 loc) · 1.52 KB
/
Challenge18.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace CryptoPalsChallenge
{
public static class Challenge18
{
public static byte[] CtrEncodeDecode(byte[] input, byte[] keyBytes, long nonce = 0)
{
const int blockSize = 16;
byte[] iv = new byte[blockSize];
byte[] result = new byte[input.Length];
for (int i = 0; i < input.Length; i += blockSize)
{
byte[] counter = Utility.Concat(
BitConverter.GetBytes(nonce),
BitConverter.GetBytes(i / blockSize));
byte[] encryptedCounter = Utility.Encrypt(
counter,
keyBytes,
iv,
CipherMode.CBC);
for (int j = 0; j < blockSize && i + j < input.Length; j++)
{
result[i + j] = (byte)(input[i + j] ^ encryptedCounter[j]);
}
}
return result;
}
public static void Run()
{
string s = "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==";
byte[] cipherText = Convert.FromBase64String(s);
byte[] keyBytes = Encoding.ASCII.GetBytes("YELLOW SUBMARINE");
byte[] result = CtrEncodeDecode(cipherText, keyBytes);
Console.WriteLine(Encoding.ASCII.GetString(result));
}
}
}