Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use JS code?
#4
Macro C# AES encryption
Code:
Copy      Help
CsScript x
x.SetOptions("references=System.Web.Extensions.dll")
x.AddCode("")

str Key = "8UHjPgXZzXCGkhxV2QCnooyJexUzvJrO"
str Text = "Here is some text to encrypt!"

str Encrypted = x.Call("Aes.Encrypt" Text Key)
str Decrypted = x.Call("Aes.Decrypt" Encrypted Key)
out Encrypted
out Decrypted


#ret
//C# code
;using System;
;using System.Text;
;using System.Security.Cryptography;
;using System.Web.Script.Serialization;
;using System.Collections.Generic;
;
;;;;;/**
;;;;;;* A class to encrypt and decrypt strings using
;;;;;;* the cipher AES-256-CBC used in Laravel.
;;;;;;*/
;;;;;public class Aes
;;;;;{
;;;;;;;;;private static readonly Encoding encoding = Encoding.UTF8;
;
,,;public static string Encrypt(string plainText, string key)
,,;{
,,,;try
,,,;{
,,,,;RijndaelManaged aes = new RijndaelManaged();
,,,,;aes.KeySize = 256;
,,,,;aes.BlockSize = 128;
,,,,;aes.Padding = PaddingMode.PKCS7;
,,,,;aes.Mode = CipherMode.CBC;
;
,,,,;aes.Key = encoding.GetBytes(key);
,,,,;aes.GenerateIV();
;
,,,,;ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
,,,,;byte[] buffer = encoding.GetBytes(plainText);
;
,,,,;string encryptedText = Convert.ToBase64String(AESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
;
,,,,;String mac = "";
;
,,,,;mac = BitConverter.ToString(HmacSHA256(Convert.ToBase64String(aes.IV) + encryptedText, key)).Replace("-", "").ToLower();
;
,,,,;var keyValues = new Dictionary<string, object>
,,,,;{
,,,,,;{ "iv", Convert.ToBase64String(aes.IV) },
,,,,,;{ "value", encryptedText },
,,,,,;{ "mac", mac },
,,,,;};
;
,,,,;JavaScriptSerializer serializer = new JavaScriptSerializer();
;
,,,,;return Convert.ToBase64String(encoding.GetBytes(serializer.Serialize(keyValues)));
,,,;}
,,,;catch (Exception e)
,,,;{
,,,,;throw new Exception("Error encrypting: " + e.Message);
,,,;}
,,;}
;
,,;public static string Decrypt(string plainText, string key)
,,;{
,,,;try
,,,;{
,,,,;RijndaelManaged aes = new RijndaelManaged();
,,,,;aes.KeySize = 256;
,,,,;aes.BlockSize = 128;
,,,,;aes.Padding = PaddingMode.PKCS7;
,,,,;aes.Mode = CipherMode.CBC;
,,,,;aes.Key = encoding.GetBytes(key);
;
,,,,;// Base 64 decode
,,,,;byte[] base64Decoded = Convert.FromBase64String(plainText);
,,,,;string base64DecodedStr = encoding.GetString(base64Decoded);
;
,,,,;// JSON Decode base64Str
,,,,;JavaScriptSerializer ser = new JavaScriptSerializer();
,,,,;var payload = ser.Deserialize<Dictionary<string, string>>(base64DecodedStr);
;
,,,,;aes.IV = Convert.FromBase64String(payload["iv"]);
;
,,,,;ICryptoTransform AESDecrypt = aes.CreateDecryptor(aes.Key, aes.IV);
,,,,;byte[] buffer = Convert.FromBase64String(payload["value"]);
;
,,,,;return encoding.GetString(AESDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
,,,;}
,,,;catch (Exception e)
,,,;{
;;;;;;;;;;;;;;;;;throw new Exception("Error decrypting: " + e.Message);
,,,;}
,,;}
;
,,;static byte[] HmacSHA256(String data, String key)
,,;{
,,,;using (HMACSHA256 hmac = new HMACSHA256(encoding.GetBytes(key)))
,,,;{
,,,,;return hmac.ComputeHash(encoding.GetBytes(data));
,,,;}
,,;}
;;;;;}


Messages In This Thread
How to use JS code? - by BK - 03-19-2019, 03:05 AM
RE: How to use JS code? - by Gintaras - 03-19-2019, 06:03 AM
RE: How to use JS code? - by BK - 03-19-2019, 11:50 AM
RE: How to use JS code? - by Gintaras - 03-20-2019, 06:14 AM
RE: How to use JS code? - by BK - 03-21-2019, 09:43 AM
RE: How to use JS code? - by win - 05-03-2019, 12:14 AM
RE: How to use JS code? - by Gintaras - 05-03-2019, 05:56 PM
RE: How to use JS code? - by win - 05-03-2019, 10:11 PM
RE: How to use JS code? - by win - 05-04-2019, 09:43 PM
RE: How to use JS code? - by Kevin - 05-04-2019, 11:50 PM
RE: How to use JS code? - by win - 05-05-2019, 12:22 AM
RE: How to use JS code? - by Kevin - 05-05-2019, 12:42 AM
RE: How to use JS code? - by win - 05-05-2019, 12:46 AM
RE: How to use JS code? - by Kevin - 05-05-2019, 12:59 AM
RE: How to use JS code? - by win - 05-05-2019, 01:02 AM
RE: How to use JS code? - by Kevin - 05-05-2019, 01:10 AM
RE: How to use JS code? - by win - 05-05-2019, 01:19 AM
RE: How to use JS code? - by Gintaras - 05-06-2019, 06:43 AM
RE: How to use JS code? - by win - 05-06-2019, 07:27 AM
RE: How to use JS code? - by r0n - 08-10-2020, 08:29 PM
RE: How to use JS code? - by TeddyBear - 08-13-2020, 01:36 AM
RE: How to use JS code? - by r0n - 08-13-2020, 02:34 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)