// Introduction: /// This is a very interesting example! ////////////////// The upper level. Viisen does not allow the company's internal staff to fall in love, There is a policy, there is //// / policy, Ting and Dicky use the idle time through the mail method with the other party communication, the way the task gives Dicky to Dicky is also sent to the mail. In order to save the mail security / ///Break, the mail is transmitted by RC2 (symmetric encryption) encryption, and the key of RC2 is encrypted by RSA //// (asymmetric encrypted). Tsing, Dicky and Viisen each have a pair of public keys and keys //// //.TING and Dicky communication content For Viisen, the communication content of Dicky and Viisen //// is invisible for ting. /// using system; using system.text; using System.Security.Cryptography; namespace EzoneSecuritySystem {class EzoneSecurityDemo {// create three employee objects, namely Dicky, Ting, Viisen public static EzonePerson Dicky = new EzonePerson ( "Dicky"); public static EzonePerson Ting = new EzonePerson ( "Ting "); Public static ezonePerson Viisen = New EzonePerson (" Viisen "); [Stathread] public static static void main (string [] args) {console.writeLine (" =============== ===== Scene 1: Dicky gives I send mail messages yourself ==================== "); Scene_1 (); console.writeline (" ============== ============================================================================================================================================================================================================= ========== "); console.write (" Press anyg "to continue ...");
Console.in.readline (); console.writeline ("==================== Scene 2: Dicky Send mail messages to Ting ======== ============ "); Scene_2 (); console.writeline (" =========================================================================================================================================================================================================== ========================================================= "); Console.write ("Press anymore to continue ..."); console.in.readline (); console.writeline ("================ Scene 3: Dicky gives ting, Viisen sends an email message ================ "); Scene_3 (); console.writeline (" ============== ============================================================================================================================================================================================================= ========== "); console.write (" press any means to continue ... "); console.in.readline ();} ///
Public Static Void Scene_1 () {Console.WriteLine ("Dicky with its own public key encryption information"); ezoneMessage DickyMessage = Dicky.EncryptMessage ("Is I worked hard today?"); console.writeline (System.environment.newline) Console.writeline ("Dicky with its own private key decryption message"); Dicky.DecryptMessage (DickyMessage); console.writeline (System.Environment.newline); console.writeline ("Viisen tries to decrypt messages with your own private key" ); Viisen.DecryptMessage (DickyMessage); console.writeline (system.environment.newline);} ///
); Dicky.GetPublicKey (TING); system.environment.newline; console.writeline ("Dicky Ting's public key encryption information"); ezoneMessage TingMessage = Dicky.EncryptMessage ("i love you!") Console.Writeline (System.Environment.newline); Console.Writeline ("Dicky Gets Viisen's Public Key"); Dicky.GetPublicKey (Viisen); console.writeline (System.Environment.newline); console.writeline ("Dicky Use Viisen's public key encryption information "); ezoneMessage ViisenMessage = Dicky.EncryptMessage (" 2003 financial summary report is on your desk! "); Console.writeline (system.environment.newline; console.writeline "Ting uses your private key to decrypt information"); ting.decryptMessage (TingMessage); console.writeline (system.environment.newline); console.writeline ("Viisen's private key Decryption information"); Viisen.DecryptMessage ViiSenMessage; console.write Line (system.environment.newline);}} // mail object class ezoneMessage {public byte [] messagebody; // mail content (content via RC2 (symmetric encrypted) encrypted) public Byte [] rc2key; // rc2 Key (by RSA (asymmetric encrypted) encrypted) Public Byte [] RC2IV; // RC2 initialization vector} // Employee object class ezonePerson {// RSA (asymmetric encrypted object) Private RsacryptoServiceProvider ezonersa; // rc2 (Symmetry Confidential Object) Private RC2CryptoserviceProvider Ezonerc2; // Employee Name Private String Name; // Person Construction Method PUBLIC EZONEPERSON (String Name) {// Initialization Member Object this.ezonersa = New RsacryptoserviceProvider ();
THIS.EZONERC2 = New rc2cryptoserviceProvider (); this.name = name;} // Send public key public {// RSA's public key and key object Rsaparameters result = new rAparameters (); // Export EZONERSA The public key (FALSE means no private key) Result = this.ezonersa.exportParameters (false); Return Result;} // Get public key public void getpublicKey (ezonePerson Obj) {// Import the public key of the ezonePerson object. This.ezonersa. ImportParameters (Obj.SendPublicKey ());} // encrypted message public EzoneMessage EncryptMessage (string text) {EzoneMessage MessageObj = new EzoneMessage (); // converts from the message string into a byte array byte [] MessageBytes = System.Text.Encoding.utf8.getbytes (text); // Randomly created RC2 key this.ezonerc2.generateKey (); // randomly created RC2 initialization vector THIS.EZ Oneterc2.generateiv (); // encrypts RC2 key with RSA, and assigns a key to the RC2 of the message object (false means no filling, only Winxp or above * is supported) MessageObj.rc2key = this .Ezonersa.Encrypt (this.ezonerc2.key, false); // Give the vector value of the RC2 of the message MessageObj.rc2iv = this.ezonerc2.iv; // Create a encryption object ibryptotransform myencryptor = this.ezonerc2.createEncryptor () ; // a memory stream MemoryStream EzoneMemoryStream = new MemoryStream create (); // create an encrypted stream object CryptoStream MyEncryptoStream = new CryptoStream (EzoneMemoryStream, MyEncryptor, CryptoStreamMode.Write) on the basis of the memory stream;
// Write the mail content to the encrypted stream MYENCRYPTOSTREAM.WRIENCRYPTOSTREAM.WRITE (MessageBytes, 0, MessageBytes.Length); // Refresh the new Direct Buffer MYENCRYPTOSTREAM.FLUSHFINALBLOCK (); // Give MessageBody (Message Body) assignment ( after RC2 encrypted byte array) MessageObj.MessageBody = EzoneMemoryStream.ToArray (); EzoneMemoryStream.Close (); MyEncryptoStream.Close (); // returns EzoneMessage target return MessageObj;} // decrypt the message public void DecryptMessage (EzoneMessage obj ) {This.ezonerc2.iv = obj.rc2iV; try {// Decipse RC2 with RSA (FALSE indicates that there is no OAEP to fill, only WinXP or higher * is supported) this.ezonerc2.key = tris. Ezonersa.decrypt (Obj.rc2key, false);} catch (cryptographicException e) {console.writeline ("Decryption failed: E.Message); Return;} // Build a decryption target ICryptoTransform MyDecryptor = this.EzoneRC2.CreateDecryptor (); // create a memory stream, obj e-mail message to initialize memory stream MemoryStream EzoneMemoryStream = new MemoryStream (obj.MessageBody);! // memory stream on the basis of Create a decryption target CryptoStream MyDecryptoStream = new CryptoStream stream (EzoneMemoryStream, MyDecryptor, CryptoStreamMode.Read); // message content after storing the decrypted byte [] MessageText = new byte [obj.MessageBody.Length]; // decrypted data from the decrypted stream And write the decrypted data into the MessageText byte array of MyDecryptostream.Read (MessageText, 0, MessageText.length); ezoneMemoryStream.Close (); myDecryptostream.close ();
Console.writeline ("Decryption success:" system.text.Encoding.utf8.getstring (messagetext));}}} 2222222222222
(Yigang International -008) [Original] Use the DOTNET Password System to ensure data security
/// Author: Stardicky //// E-mail: Stardicky@hotmail.com //// qqnumber: 9531511 //// CompanyName: EZONE INTERNATIONAL / / / / / CLASS: HBS-0308 //// Title: Utilization DOTNET Password System Guaranteed Data Security / //// Note: DOTNET Cryptographic Disease Algorithm guarantees data security /// USING system; use system.io; use system.text; using system.security.cryptography; namespace EzoneInternationalSecurityCryptography {class EzoneSecurityCryptographyDemo {[STAThread] public static void Main (string [] args) {// the encrypted data (from the memory to a file) EzoneEncryptorDemo (); // decrypted data (from file to memory) EzoneDecryptorDemo ();} / //
MyServiceProvider.GenerateKey (); // The meaning of the second parameter is: the initialization vector of the symmetric algorithm (length is 64 bits, which is 8 bytes) // can be manually input, or the random generation method is: MyServiceProvider.Generateiv (); ICryptoTransform MyTransform = MyServiceProvider.CreateEncryptor (new byte [] {100,110,120,130,100,110,120,130}, new byte [] {100,110,120,130,100,110,120,130}); // CryptoStream role object is to convert an encrypted data stream to the stream CryptoStream MyCryptoStream = new CryptoStream (fs , MyTransform, CRYPTOSTREAMMODE.WRITE); // writes data in the byte array into the encrypted stream MYCRYPTOSTREAM.WRITE (YourInputStorage.Length); // Close the plus secret object mycryptostream.close ();} / //
ICryptoTransform MyTransform = MyServiceProvider.CreateDecryptor (new byte [] {100,110,120,130,100,110,120,130}, new byte [] {100,110,120,130,100,110,120,130}); CryptoStream MyCryptoStream = new CryptoStream (fs, MyTransform, CryptoStreamMode.Read); byte [] YourInputStorage = new byte [1000]; int len = MyCryptoStream.Read (YourInputStorage, 0, YourInputStorage.Length); Console.WriteLine ( "string you just entered is:"); Console.WriteLine (System.Text.Encoding.UTF8.GetString (YourInputStorage, 0, Len));}}} Author Blog:
http://blog.9cbs.net/stardicky/
related articles
How to Trace the Stack in DOTNET Tracking Billion International Point Pair Document Transfer Process Make a Windows Form Edition DOS Analyzer Asynchronous Call Web Services About the Event Driver of the XML Document Model
Comments on this article
9cbs User (2004-06-11) to REXSP: http://www.9cbs.net/develop/Article/26/26096.shtm
Nieyong (2004-05-17) is not necessarily. You can use MemoryStream to read the encrypted information to the memory, then extract the ciphertext from the memory to decrypt the plaintext.
For example, the following code: // Encrypted Public String Desencrypt (String Strtext, String Strencrkey) {byte [] bykey = null; Byte [] iv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAb, 0xcd, 0x90, 0xAb, 0xcd, 0xEf}; {byKey = System.Text.Encoding.UTF8.GetBytes (strEncrKey.Substring (0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte [] inputByteArray = Encoding.UTF8.GetBytes (strText); MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (ms, des.CreateEncryptor (byKey, IV), CryptoStreamMode.Write); cs.Write (inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock (); return Convert.ToBase64String (ms .Toarray ());} catch (system.exception error) {messagebox.show; return "error:" error.Message "/ r";}} // Decryption function public string deSDecrypt (String Strtext , String Sdecrkey) {byte [] bykey = null; Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAb, 0xcd, 0x90, 0xAb, 0xcd, 0xef}; Byte [] INPUTBYTEARRAY = New Byte [strText.length]; try { Bykey = system.text.Encoding.utf8.getbytes (sdecrkey.substring (0,8)); Descryptoservi ceProvider des = new DESCryptoServiceProvider (); inputByteArray = Convert.FromBase64String (strText); MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (ms, des.CreateDecryptor (byKey, IV), CryptoStreamMode.Write); cs.Write (inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock (); System.Text.Encoding encoding = new System.Text.UTF8Encoding (); return encoding.GetString (ms.ToArray ());} catch (System.Exception Error) {MessageBox.show (Error.Message); Return "error:" error.Message "/ r";}} // or another program code: use system; use system.security.cryptography; using system Using system.text;
Public class encryptstringdes {public static void main (string [] args) {if (args.length <1) {console.writeline ("usage: des_demo", args [0]);
Return;
}
// Encrypt the input parameters using the UTF8 function
Utf8encoding utf8encoding = new utf8encoding ();
Byte [] InputByteArray = UTF8Encoding.getbytes (args [0] .tochararRay ());
/ / Method 1: Call the default DES implementation method DES_CSP.
DES des = des.create ();
/ / Method 2: Directly use DES_CSP () entity
// DES_CSP DES = New DES_CSP ();
// Initialize the DES encryption key and a random, 8-bit initialization vector (IV)
Byte [] Key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAb, 0xcd, 0xef};
Byte [] iv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAb, 0xcd, 0xef};
des.key = key;
DES.IV = IV;
// Establish an encryption stream
SymmetricsTreamencryptor SSE = des.createEncryptor ();
// Get the output of the encryption process using the CryptOMEMORYSTREAM method
CryptomemoryStream CMS = New CryptomemoryStream ();
// Output encrypted data in the SymmetricsTreamEncryptor stream to CryptomemoryStream
SSE.SETSINK (CMS);
// Encrypted, output the result to the console
SSE.WRITE (InputByTearray);
SSE.CloseStream ();
// Get encrypted data
Byte [] encrypteddata = cms.data;
// Output encryption results
Console.writeline ("Encryption Results:");
For (int i = 0; i Console.Write ("{0: x2}", EncryptedData [i]); } Console.writeLine (); // Demonstrate how to encrypt, how to decrypt how to decrypt SymmetricsTreamDecryptor ssd = des.createdecryptor (); CMS = new cryptomorystream (); SSD.SetSink (CMS); ssd.write (encrypteddata); SSD.CloseStream (); Byte [] DecryptedData = CMS.DATA; Char [] decryptedchararray = utf8encoding.getchars (decrypteddata); Console.writeLine ("Decryption Data:"); Console.Write (DecryptedChararray); Console.writeLine (); } }