Last active
June 28, 2022 20:17
-
-
Save muraray/9a95d5db6dc1d42c6a407bf7fe851ce5 to your computer and use it in GitHub Desktop.
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
/// <summary> | |
/// ''' Class to Encrypt and Decrypt From APS previous Version. | |
/// </summary> | |
public sealed class EncryptDecrypt | |
{ | |
private EncryptDecrypt() | |
{ | |
} | |
// Encrypts specified plaintext using Rijndael symmetric key algorithm | |
// and returns a base64-encoded result. | |
/// Passphrase from which a pseudo-random password will be derived. The | |
/// ''' derived password will be used to generate the encryption key. | |
/// ''' Passphrase can be any string. In this example we assume that this | |
/// ''' passphrase is an ASCII string. | |
const string passPhrase = "no-need-of-this"; | |
/// Salt value used along with passphrase to generate password. Salt can | |
/// ''' be any string. In this example we assume that salt is an ASCII string. | |
const string saltValue = "no-need-of-this"; | |
const string hashAlgorithm = "SHA1"; | |
/// Number of iterations used to generate password. One or two iterations | |
/// ''' should be enough. | |
const int passwordIterations = 2; | |
/// Initialization vector (or IV). This value is required to encrypt the | |
/// ''' first block of plaintext data. For RijndaelManaged class IV must be | |
/// ''' exactly 16 ASCII characters long. | |
private static byte[] initVector = Encoding.UTF8.GetBytes("give a 16 random numeric characters"); | |
/// Size of encryption key in bits. Allowed values are: 128, 192, and 256. | |
/// ''' Longer keys are more secure than shorter keys. | |
const int keySize = 256; | |
/// The number of pseudo-random key bytes to generate. | |
private static byte[] keyBytes = Encoding.UTF8.GetBytes("give a 16 random numeric characters"); | |
/// <summary> | |
/// Encrypts a plain text so that only the key and iv used to encrypt the file can decrypt it. | |
/// </summary> | |
/// <param name="originalText"></param> | |
/// <returns></returns> | |
public static string Encrypt(string originalText) | |
{ | |
byte[] encrypted = EncryptStringToBytes(originalText, keyBytes, initVector); | |
var encryptedForJavascript = Convert.ToBase64String(Encoding.UTF8.GetBytes(Convert.ToBase64String(encrypted))); | |
return string.Format(encryptedForJavascript); | |
} | |
public static string Decrypt(string cipherText) | |
{ | |
var encrypted = Convert.FromBase64String(Encoding.UTF8.GetString(Convert.FromBase64String(cipherText))); | |
var decriptedFromJavascript = DecryptStringFromBytes(encrypted, keyBytes, initVector); | |
return string.Format(decriptedFromJavascript); | |
} | |
private static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV) | |
{ | |
// Check arguments. | |
if (plainText == null || plainText.Length <= 0) | |
throw new ArgumentNullException("plainText"); | |
if (Key == null || Key.Length <= 0) | |
throw new ArgumentNullException("Key"); | |
if (IV == null || IV.Length <= 0) | |
throw new ArgumentNullException("IV"); | |
byte[] encrypted; | |
// Create an RijndaelManaged object | |
// with the specified key and IV. | |
using (RijndaelManaged rijAlg = new RijndaelManaged()) | |
{ | |
rijAlg.Key = Key; | |
rijAlg.IV = IV; | |
// Create an encryptor to perform the stream transform. | |
// Generate encryptor from the existing key bytes and initialization | |
// vector. Key size will be defined based on the number of the key bytes. | |
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); | |
// Create the streams used for encryption. | |
// Define memory stream which will be used to hold encrypted data. | |
using (MemoryStream msEncrypt = new MemoryStream()) | |
{ | |
// Define cryptographic stream (always use Write mode for encryption). | |
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) | |
{ | |
// Start encrypting. | |
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) | |
{ | |
// Write all data to the stream. | |
// Finish encrypting. | |
swEncrypt.Write(plainText); | |
} | |
// Convert our encrypted data from a memory stream into a byte array. | |
encrypted = msEncrypt.ToArray(); | |
} | |
} | |
} | |
// Return the encrypted bytes from the memory stream. | |
return encrypted; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="cipherText"></param> | |
/// <param name="Key"></param> | |
/// <param name="IV"></param> | |
/// <returns></returns> | |
/// <exception cref="ArgumentNullException"></exception> | |
private static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV) | |
{ | |
// Check arguments. | |
if (cipherText == null || cipherText.Length <= 0) | |
throw new ArgumentNullException("cipherText"); | |
if (Key == null || Key.Length <= 0) | |
throw new ArgumentNullException("Key"); | |
if (IV == null || IV.Length <= 0) | |
throw new ArgumentNullException("IV"); | |
// Declare the string used to hold | |
// the decrypted text. | |
string plaintext = null; | |
// Create an RijndaelManaged object | |
// with the specified key and IV. | |
using (RijndaelManaged rijAlg = new RijndaelManaged()) | |
{ | |
rijAlg.Key = Key; | |
rijAlg.IV = IV; | |
// Create a decryptor to perform the stream transform. | |
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); | |
// Create the streams used for decryption. | |
using (MemoryStream msDecrypt = new MemoryStream(cipherText)) | |
{ | |
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) | |
{ | |
using (StreamReader srDecrypt = new StreamReader(csDecrypt)) | |
{ | |
// Read the decrypted bytes from the decrypting stream | |
// and place them in a string. | |
plaintext = srDecrypt.ReadToEnd(); | |
} | |
} | |
} | |
} | |
return plaintext; | |
} | |
} |
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
import CryptoJS from 'crypto-js/crypto-js' | |
/** | |
* AES encryption: string key iv returns base64 | |
*/ | |
export function Encrypt(word, keyStr, ivStr) { | |
let key = CryptoJS.enc.Utf8.parse("should be same as - how you have given in server side a 16 random numeric characters") | |
let iv = CryptoJS.enc.Utf8.parse("should be same as - how you have given in server side a 16 random numeric characters") | |
if (keyStr) { | |
key = CryptoJS.enc.Utf8.parse(keyStr); | |
iv = CryptoJS.enc.Utf8.parse(ivStr); | |
} | |
let srcs = CryptoJS.enc.Utf8.parse(word); | |
var encrypted = CryptoJS.AES.encrypt(srcs, key, { | |
keySize: 128 / 8, | |
iv: iv, | |
mode: CryptoJS.mode.CBC, | |
padding: CryptoJS.pad.Pkcs7 | |
}); | |
return btoa(CryptoJS.enc.Base64.stringify(encrypted.ciphertext)); | |
} | |
/** | |
* AES decryption: string key iv returns base64 | |
* | |
*/ | |
export function Decrypt(word, keyStr, ivStr) { | |
let key = CryptoJS.enc.Utf8.parse("should be same as - how you have given in server side a 16 random numeric characters") | |
let iv = CryptoJS.enc.Utf8.parse("should be same as - how you have given in server side a 16 random numeric characters") | |
if (keyStr) { | |
key = CryptoJS.enc.Utf8.parse(keyStr); | |
iv = CryptoJS.enc.Utf8.parse(ivStr); | |
} | |
let base64 = CryptoJS.enc.Base64.parse(atob(word)); | |
let src = CryptoJS.enc.Base64.stringify(base64); | |
var decrypt = CryptoJS.AES.decrypt(src, key, { | |
keySize: 128 / 8, | |
iv: iv, | |
mode: CryptoJS.mode.CBC, | |
padding: CryptoJS.pad.Pkcs7 | |
}); | |
var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); | |
return decryptedStr.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment