Comparing sensitive data, confidential files or internal emails?

Most legal and privacy policies prohibit uploading sensitive data online. Diffchecker Desktop ensures your confidential information never leaves your computer. Work offline and compare documents securely.

C#Diff

Created Diff never expires
6 removals
64 lines
5 additions
63 lines
using System;
using System;
using System.Security.Cryptography;
using System.Security.Cryptography;
using System.Text;
using System.Text;


class SSN
class SSN
{
{
public string EncryptString(string plainString, string keyString, string encryptionIV)
public string EncryptString(string plainString, string keyString, string encryptionIV)
{
{
byte[] key = Encoding.UTF8.GetBytes(keyString);
byte[] key = Encoding.UTF8.GetBytes(keyString.Substring(0, 16));
byte[] iv = Encoding.UTF8.GetBytes(encryptionIV);
byte[] iv = Encoding.UTF8.GetBytes(encryptionIV.Substring(0, 16));


using (Aes aesAlg = Aes.Create())
using (Aes aesAlg = Aes.Create())
{
{
aesAlg.KeySize = 128;
aesAlg.Key = key;
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.IV = iv;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.None;
aesAlg.Padding = PaddingMode.None;


ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);


byte[] plainBytes = Encoding.UTF8.GetBytes(plainString);
byte[] plainBytes = Encoding.UTF8.GetBytes(plainString);


int blockSize = 16;
int blockSize = 16;
int paddingNeeded = blockSize - (plainBytes.Length % blockSize);
int paddingNeeded = blockSize - (plainBytes.Length % blockSize);


byte[] paddedBytes = new byte[plainBytes.Length + paddingNeeded];
byte[] paddedBytes = new byte[plainBytes.Length + paddingNeeded];
Array.Copy(plainBytes, paddedBytes, plainBytes.Length);
Array.Copy(plainBytes, paddedBytes, plainBytes.Length);
for (int i = plainBytes.Length; i < paddedBytes.Length; i++)
for (int i = plainBytes.Length; i < paddedBytes.Length; i++)
{
{
paddedBytes[i] = (byte)paddingNeeded;
paddedBytes[i] = (byte)paddingNeeded;
}
}


byte[] encryptedBytes = encryptor.TransformFinalBlock(paddedBytes, 0, paddedBytes.Length);
byte[] encryptedBytes = encryptor.TransformFinalBlock(paddedBytes, 0, paddedBytes.Length);


return Convert.ToBase64String(encryptedBytes);
return Convert.ToBase64String(encryptedBytes);
}
}
}
}


public string DecryptString(string encryptedString, string keyString, string encryptionIV)
public string DecryptString(string encryptedString, string keyString, string encryptionIV)
{
{
byte[] key = Encoding.UTF8.GetBytes(keyString);
byte[] key = Encoding.UTF8.GetBytes(keyString.Substring(0, 16));
byte[] iv = Encoding.UTF8.GetBytes(encryptionIV);
byte[] iv = Encoding.UTF8.GetBytes(encryptionIV.Substring(0, 16));


using (Aes aesAlg = Aes.Create())
using (Aes aesAlg = Aes.Create())
{
{
aesAlg.KeySize = 128;
aesAlg.Key = key;
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.IV = iv;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.None;
aesAlg.Padding = PaddingMode.None;


ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);


byte[] encryptedBytes = Convert.FromBase64String(encryptedString);
byte[] encryptedBytes = Convert.FromBase64String(encryptedString);
byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);


int paddingByte = decryptedBytes[decryptedBytes.Length - 1];
int paddingByte = decryptedBytes[decryptedBytes.Length - 1];
int unpaddedLength = decryptedBytes.Length - paddingByte;
int unpaddedLength = decryptedBytes.Length - paddingByte;


return Encoding.UTF8.GetString(decryptedBytes, 0, unpaddedLength);
return Encoding.UTF8.GetString(decryptedBytes, 0, unpaddedLength);
}
}
}
}
}
}