Using system.text; using system.sem.io; using system.security.cryptography;
Class class1 {static void main () {console.writeline ("encrypt string ..."); txtKey = "tkggrmbervc ="; // Setup encryption key btnkeygen (); console.writeline ("Encrypt Key: {0}" TXTKEY); txtiv = "kl7zgtm1dvq ="; // Setting encrypted IV btnivgen (); console.writeline ("Encrypt IV: {0}", txtiv); console.writeline ();
String txtencrypted = encryptstring ("test"); console.writeline ("Encrypt String: {0}", TXTENCRYPTED;
String txtORIGINAL = DecryptString (txtencrypted); console.writeline ("Decrypt String: {0}", txtoriginal);
Private static symmetricalgorithm mcsp; private static string txtick;
Private static void btnkeygen () {mcsp = setnc ();
Byte [] Byt2 = Convert.FromBase64String (txtKey);
Mcsp.key = byt2;}
Private static void btnivgen () {byte [] Byt2 = convert.frombase64string (txtiv);
Mcsp.IV = byt2;}
Private static string encryptstring (string value) {icryptotransform ct; memorystream ms; cryptostream cs; byte [] byt;
Ct = mcsp.createencryptor (mcsp.key, mcsp.iv);
Byt = encoding.utf8.getbytes (value);
Ms = new memoryStream (); cs = new cryptostream (ms, ct, cryptostreammode.write); cs.write (byt, 0, byt.length); cs.flushfinalBlock (); cs.close ();
Return Convert.TOBASE64STRING (ms.toarray ());}
Private static string decryptstring (string value) {icryptotransform ct; memorystream ms; cryptostream cs; byte [] byt;
CT = mcsp.createdecryptor (mcsp.key, mcsp.iv);
BYT = Convert.FromBase64String (Value);
Ms = new memoryStream (); cs = new cryptostream (ms, ct, cryptostreammode.write); cs.write (byt, 0, byt.length); cs.flushfinalBlock (); cs.close ();
Return encoding.utf8.getstring (ms.toarray ());}
Private static symmetricalgorithm setnc () {return new descryptoserviceProvider ();}}