-
Notifications
You must be signed in to change notification settings - Fork 1
/
EncryptionHandler.cs
86 lines (75 loc) · 3.29 KB
/
EncryptionHandler.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
// '======================================
// ENCRYPTION HANDLER
// ONE DESKTOP COMPONENTS
// J.J.HIRNIAK
// COPYRIGHT (C) 2015 J.J.HIRNIAK
// '======================================
using System;
using System.IO;
using System.Security.Cryptography;
namespace Handlers {
public class EncryptionHandler {
public static string STR_ES(string Password, string Input) {
var wrapper = new Simple3Des(Password);
string cipherText = wrapper.EncryptData(Input);
return cipherText;
}
public static string STR_DS(string Password, string Input) {
var wrapper = new Simple3Des(Password);
try {
string plainText = wrapper.DecryptData(Input);
return plainText;
}
catch (CryptographicException ex) {
ex.Message.ToString();
return Input;
}
}
}
public sealed class Simple3Des {
private TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();
private byte[] TruncateHash(string key, int length) {
var sha1 = new SHA1CryptoServiceProvider();
// Hash the key.
var keyBytes = System.Text.Encoding.Unicode.GetBytes(key);
var hash = sha1.ComputeHash(keyBytes);
var oldHash = hash;
hash = new byte[length];
// Truncate or pad the hash.
if (oldHash != null)
Array.Copy(oldHash, hash, Math.Min(length, oldHash.Length));
return hash;
}
public Simple3Des(string key) {
// Initialize the crypto provider.
TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);
}
public string EncryptData(string plaintext) {
// Convert the plaintext string to a byte array.
var plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);
// Create the stream.
var ms = new MemoryStream();
// Create the encoder to write to the stream.
var encStream = new CryptoStream(ms, TripleDes.CreateEncryptor(), CryptoStreamMode.Write);
// Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
encStream.FlushFinalBlock();
// Convert the encrypted stream to a printable string.
return Convert.ToBase64String(ms.ToArray());
}
public string DecryptData(string encryptedtext) {
// Convert the encrypted text string to a byte array.
var encryptedBytes = Convert.FromBase64String(encryptedtext);
// Create the stream.
var ms = new MemoryStream();
// Create the decoder to write to the stream.
var decStream = new CryptoStream(ms, TripleDes.CreateDecryptor(), CryptoStreamMode.Write);
// Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();
// Convert the plaintext stream to a string.
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}
}
}