Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
test save
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.Security.Cryptography; using System.Diagnostics; namespace Rextester { public class Program { public static void Main(string[] args) { string key = "59b50e345cab8b6d421b161918ea3fbd7e5921eea7d43d1ac54fa92cca452bb5"; string iv = "00000000000000000000000000000000"; string message = "3b16601d0a7e283c1f24d30ec214676885096cb0bbf3998012a2be87c5a58d89"; string encryptedReferenceOFB = "578946591ce2d787cbe41bec77a58dac66e6007fb722b1af847ecc3bf4212cea"; string encryptedReferenceECB = "d0aa1d38f2338de417a919cc5380c81d9a5f90b8cd505a4839eaa30ab524fd82"; var encryptedString = AesCsp.EncryptHexStrings(message, key, iv); var decryptedString = AesCsp.DecryptHexStrings(encryptedString, key, iv); Debug.Assert(message.ToUpper() == decryptedString.ToUpper()); Debug.Assert(encryptedString.ToUpper() == encryptedReferenceECB.ToUpper()); Console.WriteLine(encryptedString); Console.WriteLine(encryptedReferenceECB); } } static class AesCsp { public static byte[] /*output*/ EncryptBytes(byte[] input, byte[] aesKey, byte[] aesIV) { Aes aes = new AesCryptoServiceProvider(); aes.Padding = PaddingMode.None; aes.Mode = CipherMode.ECB; var enc = aes.CreateEncryptor(aesKey, aesIV); return enc.TransformFinalBlock (input, 0, input.Length); } public static byte[] DecryptBytes(byte[] encryptedOutput, byte[] aesKey, byte[] aesIV) { Aes aes = new AesCryptoServiceProvider(); aes.Padding = PaddingMode.None; var dec = aes.CreateDecryptor(aesKey, aesIV); return dec.TransformFinalBlock(encryptedOutput, 0, encryptedOutput.Length); } public static string EncryptHexStrings(string input, byte[] aesKey, byte[] aesIV) { byte[] bytes = HexStringToByteArray(input); byte[] encBytes = EncryptBytes (bytes,aesKey,aesIV); return ByteArrayToHexString(encBytes); } public static string DecryptHexStrings(string encryptedOutput, byte[] aesKey, byte[] aesIV) { byte[] bytes = HexStringToByteArray(encryptedOutput); byte[] decBytes = DecryptBytes(bytes,aesKey,aesIV); return ByteArrayToHexString(decBytes); } public static string EncryptHexStrings(string input, string aesKey, string aesIV) { byte[] key = HexStringToByteArray(aesKey); byte[] iv = HexStringToByteArray(aesIV); return EncryptHexStrings(input, key, iv); } public static string DecryptHexStrings(string encryptedOutput, string aesKey, string aesIV) { byte[] key = HexStringToByteArray(aesKey); byte[] iv = HexStringToByteArray(aesIV); return DecryptHexStrings(encryptedOutput,key,iv); } public static byte[] HexStringToByteArray(string s) { byte[] ret = new byte[s.Length / 2]; for (int i=0; i<s.Length; i+=2) { ret[i/2] = Convert.ToByte (s.Substring (i,2), 16); } return ret; } public static string ByteArrayToHexString(byte[] bytes) { var sb = new StringBuilder(); foreach (byte b in bytes) sb.AppendFormat("{0:X2}", b); return sb.ToString(); } } }
run
|
edit
|
history
|
help
0
hay
Dependency Injection
Merge Sample
GP-TIME
aaa
latest Iequatable
base interior 0.1
IF... ELSE
Strings Mix Complete
Fusion