forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VigenereEncoder.cs
71 lines (62 loc) · 2.42 KB
/
VigenereEncoder.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Text;
namespace Algorithms.Encoders
{
/// <summary>
/// Encodes using vigenere cypher.
/// </summary>
public class VigenereEncoder : IEncoder<string>
{
private readonly CaesarEncoder caesarEncoder = new CaesarEncoder();
/// <summary>
/// Encodes text using specified key,
/// time complexity: O(n),
/// space complexity: O(n),
/// where n - text length.
/// </summary>
/// <param name="text">Text to be encoded.</param>
/// <param name="key">Key that will be used to encode the text.</param>
/// <returns>Encoded text.</returns>
public string Encode(string text, string key) => Cipher(text, key, caesarEncoder.Encode);
/// <summary>
/// Decodes text that was encoded using specified key,
/// time complexity: O(n),
/// space complexity: O(n),
/// where n - text length.
/// </summary>
/// <param name="text">Text to be decoded.</param>
/// <param name="key">Key that was used to encode the text.</param>
/// <returns>Decoded text.</returns>
public string Decode(string text, string key) => Cipher(text, key, caesarEncoder.Decode);
private string Cipher(string text, string key, Func<string, int, string> symbolCipher)
{
key = AppendKey(key, text.Length);
var encodedTextBuilder = new StringBuilder(text.Length);
for (var i = 0; i < text.Length; i++)
{
if (!char.IsLetter(text[i]))
{
_ = encodedTextBuilder.Append(text[i]);
continue;
}
var letterZ = char.IsUpper(key[i]) ? 'Z' : 'z';
var encodedSymbol = symbolCipher(text[i].ToString(), letterZ - key[i]);
_ = encodedTextBuilder.Append(encodedSymbol);
}
return encodedTextBuilder.ToString();
}
private string AppendKey(string key, int length)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentOutOfRangeException($"{nameof(key)} must be non-empty string");
}
var keyBuilder = new StringBuilder(key, length);
while (keyBuilder.Length < length)
{
_ = keyBuilder.Append(key);
}
return keyBuilder.ToString();
}
}
}