-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy path3DESClass.cs
79 lines (66 loc) · 2.86 KB
/
3DESClass.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace Priore.Cryptography
{
public class TripleDESClass
{
public static string EncryptText(string input, string password)
{
// Get the bytes of the string
byte[] bytesToBeEncrypted = UTF8Encoding.UTF8.GetBytes(input);
byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes).Take(24).ToArray();
byte[] bytesEncrypted = TripleDES_Encrypt(bytesToBeEncrypted, passwordBytes);
return Convert.ToBase64String(bytesEncrypted);
}
public static byte[] TripleDES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
{
tdes.KeySize = 192;
tdes.BlockSize = 64;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
tdes.Key = passwordBytes;
using (ICryptoTransform encrypto = tdes.CreateEncryptor())
{
encryptedBytes = encrypto.TransformFinalBlock(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
}
}
return encryptedBytes;
}
public static string DecryptText(string input, string password)
{
// Get the bytes of the string
byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes).Take(24).ToArray();
byte[] bytesDecrypted = TripleDES_Decrypt(bytesToBeDecrypted, passwordBytes);
return UTF8Encoding.UTF8.GetString(bytesDecrypted);
}
public static byte[] TripleDES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
{
tdes.KeySize = 192;
tdes.BlockSize = 64;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
tdes.Key = passwordBytes;
using (ICryptoTransform decrypto = tdes.CreateDecryptor())
{
decryptedBytes = decrypto.TransformFinalBlock(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
}
}
return decryptedBytes;
}
}
}