-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptKey.cs
110 lines (101 loc) · 3.68 KB
/
CryptKey.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// cCrypt.cs
using System;
using System.IO;
using System.Security.Cryptography;
namespace DotStd
{
/// <summary>
/// Base for helper mechanism to store key for crypt/decrypt of some string.
/// </summary>
public abstract class CryptKey
{
protected byte[] _Key; // kLen bytes. pad to size that fits _Algo.KeySize/8
protected byte[] _IV; // kLen bytes. pad to size that fits _Algo.BlockSize/8
protected SymmetricAlgorithm _Algo; // what crypto algorithm to use ?
protected CryptKey(byte[] key, byte[] iv, SymmetricAlgorithm algo)
{
_Key = key;
_IV = iv;
_Algo = algo;
}
/// <summary>
/// Encrypt a string and return the base64 of the crypt.
/// pad out to proper size for algorithm?
/// </summary>
/// <param name="value">base64</param>
/// <returns></returns>
public string EncryptStr(string value)
{
// Can throw. "System.Security.Cryptography.CryptographicException: 'Specified key is not a valid size for this algorithm.'"
// or Specified initialization vector (IV) does not match the block size for this algorithm. (Parameter 'rgbIV')
if (string.IsNullOrEmpty(value))
return "";
using (var ms = new MemoryStream())
{
var cs = new CryptoStream(ms, _Algo.CreateEncryptor(_Key, _IV), CryptoStreamMode.Write);
using (var sw = new StreamWriter(cs))
{
sw.Write(value);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
//convert back to a string Base64
return Convert.ToBase64String(ms.GetBuffer(), (int)0, (int)ms.Length);
}
}
}
/// <summary>
/// Decrypt From Base64 string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public string DecryptStr(string value)
{
if (string.IsNullOrEmpty(value))
return "";
try
{
//convert from Base64 string to byte array
byte[] buffer = Convert.FromBase64String(value);
var ms = new MemoryStream(buffer);
var cs = new CryptoStream(ms, _Algo.CreateDecryptor(_Key, _IV), CryptoStreamMode.Read);
using (var sr = new StreamReader(cs))
{
return sr.ReadToEnd();
}
}
catch (Exception)
{
// OK invalid junk codes just return blank.
return ""; // invalid junk might not decode ?
}
}
}
/// <summary>
/// Standard DES encryption.
/// 8 bytes randomly selected for both the Key and the Initialization Vector.
/// the IV is used to encrypt the first block of text so that any repetitive patterns are not apparent
/// </summary>
public class CryptD : CryptKey
{
public const int kLen = 8;
public CryptD(byte[] k, byte[] iv) : base(k, iv, DES.Create())
{
//Assert.IsTrue(k.Length == kLen);
//Assert.IsTrue(i.Length == kLen);
}
}
/// <summary>
/// TRIPLE DES encryption, decryption.
/// 24 byte or 192 bit key and IV for TripleDES
/// </summary>
public class CryptD3 : CryptKey
{
public const int kLen = 24;
public CryptD3(byte[] k, byte[] iv) : base(k, iv, TripleDES.Create())
{
//Assert.IsTrue(k.Length == kLen);
//Assert.IsTrue(i.Length == kLen);
}
}
}