-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Security.Cryptography; | ||
using System.Globalization; | ||
|
||
namespace App.Opayo | ||
{ | ||
public static class Crypt | ||
{ | ||
public static string Encrypt(string input, string password) | ||
{ | ||
return ByteArrayToHexString(AesEncrypt(Encoding.UTF8.GetBytes(input), password)); | ||
} | ||
|
||
public static string Decrypt(string input, string password) | ||
{ | ||
return Encoding.UTF8.GetString(AESdecrypt(ConvertHexStringToByteArray(input.Replace("@", "")), password)); | ||
} | ||
|
||
private static byte[] ConvertHexStringToByteArray(string hexString) | ||
{ | ||
if (hexString.Length % 2 != 0) | ||
{ | ||
throw new ArgumentException("Binary key must have even number of digits"); | ||
} | ||
|
||
byte[] data = new byte[hexString.Length / 2]; | ||
|
||
for (int index = 0; index < data.Length; index++) | ||
{ | ||
string byteValue = hexString.Substring(index * 2, 2); | ||
data[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
private static string ByteArrayToHexString(byte[] value) | ||
{ | ||
return BitConverter.ToString(value).Replace("-", ""); | ||
} | ||
|
||
private static byte[] AesEncrypt(byte[] input, string key) | ||
{ | ||
using MemoryStream ms = new MemoryStream(); | ||
var aes = GetAesManagedEncryption(key); | ||
CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write); | ||
cs.Write(input, 0, input.Length); | ||
cs.FlushFinalBlock(); | ||
return ms.ToArray(); | ||
} | ||
|
||
private static byte[] AESdecrypt(byte[] input, string key) | ||
{ | ||
using MemoryStream ms = new MemoryStream(); | ||
var aes = GetAesManagedEncryption(key); | ||
CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write); | ||
cs.Write(input, 0, input.Length); | ||
cs.FlushFinalBlock(); | ||
return ms.ToArray(); | ||
} | ||
|
||
private static AesManaged GetAesManagedEncryption(string key) | ||
{ | ||
return new AesManaged | ||
{ | ||
Padding = PaddingMode.PKCS7, | ||
Mode = CipherMode.CBC, | ||
KeySize = 128, | ||
BlockSize = 128, | ||
Key = Encoding.UTF8.GetBytes(key), | ||
IV = Encoding.UTF8.GetBytes(key) | ||
}; | ||
} | ||
} | ||
} |