A class that can be used to encryptdecrypt (original)

xiaoxiao2021-03-06  48

Can be used to add / decrypt database users, passwords, etc.

Using system; using system.text; using system.security.cryptography;

Namespace Common {? ///

Summary description of /// securityservice. ? ///

Public Class SecurityService

? {

?? static protected byte [] Bytekey? = {125, 14, 45, 67, 112, 79, 77, 99, 37, 104, 13, 9, 118, 51, 87, 108};

?? static protected byte [] Byteiv? = {86, 19, 79, 15, 72, 58, 117, 45};

?? static public string symmetricencrypt (string splaintext)

?? {

??? byte [] byteplaintext;

??? MemoryStream EncryptedStream;

??? Icryptotransform Encryptor;

????? Cryptostream thecryptostream;

????? if (splaintext == "")? Return ""

??? byteplaintext = Encoding.ascii.getbytes (splaintext);

??? EncryptedStream = new memoryStream (splaint.length);

??? encryptor = getencryptor ();

??? Thecryptostream = New CryptostReam (EncryptedStream, Encryptor, Cryptostreammode.write);

??? Thecryptostream.write (byteplaintext, 0, byteplaintext.length);

???????.Flushptostream.flushfinalblock ();

??? thecryptostream.close ();

??? Return Convert.TOBASE64STRING (EncryptedStream.toArray ());

??} // end function

?? static public string SymmetricDecrypt (String sEncryptedText) ?? {??? Byte [] byteEncrypted; ??? MemoryStream PlaintextStream; ??? ICryptoTransform Decryptor; ??? CryptoStream TheCryptoStream;

??? if (sencryptedtext == "") "rRN" ";

??? byteEncrypted = Convert.FromBase64String (sEncryptedText.Trim ()); ??? PlaintextStream = new MemoryStream (sEncryptedText.Length); ??? Decryptor = GetDecryptor (); ??? TheCryptoStream = new CryptoStream (PlaintextStream, Decryptor, CryptostReammode.write;

??? TheCryptoStream.Write (byteEncrypted, 0, byteEncrypted.Length); ??? TheCryptoStream.FlushFinalBlock (); ??? TheCryptoStream.Close (); ??? return Encoding.ASCII.GetString (PlaintextStream.ToArray ()) ;

??} // end function

?? static private ICryptoTransform GetEncryptor () ?? {???? RC2CryptoServiceProvider CryptoProvider = new RC2CryptoServiceProvider (); ????????????? CryptoProvider.Mode = CipherMode.CBC; ???? return CryptoProvider .Createncryptor (bytekey, byteiv);

??} // end function

? ?? static private ICryptoTransform GetDecryptor () ?? {??? RC2CryptoServiceProvider CryptoProvider = new RC2CryptoServiceProvider (); ??? CryptoProvider.Mode = CipherMode.CBC; ???????????? return CryptoProvider.CreateDecryptor (Bytekey, Byteiv);

??} // end function?}}

转载请注明原文地址:https://www.9cbs.com/read-117402.html

New Post(0)