-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringEncryptor.cs
More file actions
60 lines (51 loc) · 2.04 KB
/
StringEncryptor.cs
File metadata and controls
60 lines (51 loc) · 2.04 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace CSEmployeeAttendance25
{
public static class StringEncryptor
{
private static readonly string EncryptionKey = "YourSecureKey123456"; // 🔑 Change this key!
public static string EncryptString(string text)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(EncryptionKey.PadRight(32, '0')); // Ensure 32-byte key
using (Aes aes = Aes.Create())
{
aes.Key = keyBytes;
aes.GenerateIV();
byte[] iv = aes.IV;
using (MemoryStream ms = new MemoryStream())
{
ms.Write(iv, 0, iv.Length); // Prepend IV to the ciphertext
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(text);
}
return Convert.ToBase64String(ms.ToArray());
}
}
}
public static string DecryptString(string encryptedText)
{
byte[] fullCipher = Convert.FromBase64String(encryptedText);
byte[] keyBytes = Encoding.UTF8.GetBytes(EncryptionKey.PadRight(32, '0')); // Ensure 32-byte key
using (Aes aes = Aes.Create())
{
aes.Key = keyBytes;
byte[] iv = new byte[aes.IV.Length];
Array.Copy(fullCipher, iv, iv.Length);
using (MemoryStream ms = new MemoryStream(fullCipher, iv.Length, fullCipher.Length - iv.Length))
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
using (StreamReader sr = new StreamReader(cs))
{
return sr.ReadToEnd();
}
}
}
}
}