C # Made an encrypteddecryption class

xiaoxiao2021-03-06  17

Everyone is interested in discussing the WebService data interaction security issues, the following code, can be used in any managed approach in the DOTNET environment, and there are two instances in practical applications. Among them, there is a URL: http://www.ttttssmp/webservice/thrdataservice.asmx, interested in seeing SOAP information. Of course, you have to look at the encrypted decryption process, there is no way! Otherwise, I am too faceless, yes!

The things written in the first two years have now been organized! Previous companies need to do WebService, and encrypt the WebService's SOAPHEADER, so I wrote this stuff! Using this class, you need key management! In order to ensure that data is automatically encrypted, one of the disadvantages of encryption is to affect the operational efficiency of the program, so my idea is to encrypt only the user's login information (username, password)! Data is transmitted with a clear text, and the user information verification is not passed, and data transmission is not performed.

In the network communication, the way the key is used is not an impeccable. If the hacker can capture the key encrypted, user authentication information, then make a simulation request, send a request to the webservice server, or you can get the request data! So, I use IP or domain binding way! After all, WebService is not directly provided by end users! So, after adding these means, even if there is a bad attemparent, you will get a touch of service through the services provided by WebService through illegal ways!

There is also a safe suggestion, which is a regular replacement key. In this example, I use symmetrical encryption, the encryption party, and the secret key! Regular replacement keys allow security to improve!

Everyone must have a better way, or suggest that you can leave a message to discuss! To increase!

code show as below:

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

Summary description of Namespace Sedo {///

/// sedo. /// SEDO implements a component encapsulated four symmetric encryption methods (Des, RC2, RIJNDAEL, TRIPLEDES) ///// Precautions: /// 1: TripleDes and Rijndael encryption / decryption objects use 16 Or 24 Byte's key /// 2: Rijndael can only use 16-bit initialization vectors IV /// 3: DES and RC2 use 8 byte Byte's key and IV //// 4: data that requires encryption / decryption How to use the method for encoding / decoding, determined by the user of the calling assembly yourself /// 5: Key and initialization vector IV is defined by the user yourself /// Programmer: Wang Haibo 2003-05-19 hwnghb@21cn.com /// // Defines the enumeration of the encryption type public enum encryptionalgorithm {des = 1, RC2, RIJNDAEL, TrIPLEDES};

// definition of class encryption internal class EncryptTransformer {private EncryptionAlgorithm algorithmID; private byte [] initVec; private byte [] encKey; internal EncryptTransformer (EncryptionAlgorithm algId) {// Save the algorithm being used algorithmID = algId;.}

INTERNAL ICRYPTORANSFORM GETCRYPTOSERVICEPROVIDER (Byte [] BYTESKEY) {// When the data key Key or initialization vector IV is empty, the encrypted object will automatically generate the key Key, or the initialization vector IV switch (algorithmid) {Case EncryptionAlithm.des: {Des des = new descryptoServiceProvider (); des.mode = ciphermode.cbc;

// see if a key at.}} else {des.key;} else {des.key;}}} else {des.key;}}}}}}} // See if the client provides;} // see} // See if the client provides ( null == initVec) {// Have the algorithm create one initVec = des.IV;} else {// No, give it to the algorithm des.IV = initVec;} return des.CreateEncryptor ();} case EncryptionAlgorithm.TripleDes : {TripleDES des3 = new TripleDESCryptoServiceProvider (); des3.Mode = CipherMode.CBC; // See if a key was provided if (null == bytesKey) {encKey = des3.Key;} else {des3.Key = bytesKey; encKey = des3.key;} // See if the client provides {// yes, have the alg create one initVec = des3.iv;} else {// no, give it to the alg DES3.IV = INITVEC;} Return des3.createEncryptor ();} case encryptionalgorithm.rc2 : {Rc2 rc2 = new rc2cryptoserviceProvider (); rc2.mode = ciphermode.cbc; // test to see letter a key taste.key;} else = rc2.key;} else {rc2.key;} else {rc2.key = byteskey ENCKEY = rc2.key;} // see if the client provides an iv if (null == initVec) {// yes, have the alg;} else {// NO, Give It To RC2.IV = INITVEC;} Return rc2.createEncryptor ();} case encryptionalgorithm.rijndael: {rijndael rijndael = new rijndaelManaged (); rijndael.mode =

Ciphermode.cbc; // test to see if a key was proVided if (null == byteskey) {eNCKEY = rijndael.key;} else {rijndael.key;} else {rijndael.key = rijndael.key;} // See meansclect Provided an iv if (null == initVec) {// Yes, have the alg create one initVec = rijndael.iv;} else {// no, give it to the alg. rijndael.iv = initVec;} Return Rijndael.createEncryptor (); "ALGORITHM ID '" AlgorithMID "' NOT Supported.");}}} // Encrypted Offset Vector INTERNAL BYTE []} {get {return initVec;} set {INITVEC = Value;}} // Encrypted key infernal byte [] key {get {return enckey;}}}

}

// Define Decryption Class Internal Class Decrypttransformer {Private EncryptionAlGorithm Algorithmid; Private Byte [] InitVec; Private Byte [] EncKey;

INTERNAL DECRYPTTRANSFORMER (Encryptionalgorithm Decryptid) {algorithmid = DecryptID;

// Encrypted Offset Vector Internal Byte [] IV {Get {Return INTVEC;} set {initVec = value;}} // Encrypted key infernal byte [] key {get {return enckey;} set {eNckey = value }}

INTERNAL ICRYPTORANSFORM GETCRYPTOSERVICEPROVIDER (Byte [] BYTESKEY) {// When the data key Key or initialization vector IV is empty, the encrypted object will automatically generate the key Key, or the initialization vector IV switch (algorithmid) {Case EncryptionAlithm.des: {dES des = new DESCryptoServiceProvider (); des.Mode = CipherMode.CBC; des.Key = bytesKey; des.IV = initVec; return des.CreateDecryptor ();} case EncryptionAlgorithm.TripleDes: {TripleDES des3 = new TripleDESCryptoServiceProvider () ; des3.Mode = CipherMode.CBC; return des3.CreateDecryptor (bytesKey, initVec);} case EncryptionAlgorithm.Rc2: {RC2 rc2 = new RC2CryptoServiceProvider (); rc2.Mode = CipherMode.CBC; return rc2.CreateDecryptor (bytesKey, initVec );} case EncryptionAlgorithm.Rijndael: {Rijndael rijndael = new RijndaelManaged (); rijndael.Mode = CipherMode.CBC; return rijndael.CreateDecryptor (bytesKey, initVec);} default: {throw new CryptographicException ( "Algorithm ID '" algorithmID " 'Not supported.");}}} // end GetCryptoServiceProvider} // define encryptor class public class Encryptor {private EncryptTransformer transformer; private byte [] initVec; private byte [] encKey; public Encryptor (EncryptionAlgorithm algId) {transformer = New EncryPtTransformer (algid);

Public Byte [] encrypt (byte [] bytesdata, byte [] byteskey, byte [] bytes) {// Set stream object to save encrypted data byte stream. MemoryStream MemstreamEncryptedData = new memorystream (); transformer.iv = bytesiv; transformer.Key = bytesKey; ICryptoTransform transform = transformer.GetCryptoServiceProvider (bytesKey); CryptoStream encStream = new CryptoStream (memStreamEncryptedData, transform, CryptoStreamMode.Write); try {// the encrypted data written to the stream object encStream.Write (bytesData, 0, Bytesdata.length;} catch new exception ("Errors appear when data encryption! Error prompt: / n" ex. measureage);} // Setup encrypted key and initial vector IV attribute Enckey = Transformer.Key; INITVEC = Transformer.IV;

EncStream.flushfinalBlock (); EncStream.close ();

// send the data back. Return memstreamencryptedData.toArray ();

Public Byte [] iv {get {return initVec;} set {initvec = value;}}

Public byte [] key {get {return} set {eNCKEY = value;}}}

// Define Decryptors Public Class Decryptor {Private Decrypttransformer Transformer; Private Byte [] InitVec; Private Byte [] eNCKey;

Public Decryptor (Encryptionalgorithm Algid) {Transformer = New Decrypttransformer (ALGID);

Public Byte [] Decrypt (byte [] bytesdata, byte [] byteskey, byte [] bytes) {// Set stream object is used to save the decryption data byte stream. MemoryStream MemstreamDecryptedData = new memorystream ();

// Pass in the initialization vector. Transformer.IV = bytesiV; transformer.key = byteskey;

ICryptoTransform transform = transformer.GetCryptoServiceProvider (bytesKey); CryptoStream decStream = new CryptoStream (memStreamDecryptedData, transform, CryptoStreamMode.Write); try {decStream.Write (bytesData, 0, bytesData.Length);} catch (Exception ex) {throw new Exception ("In the data decryption, an error occurred! Error prompt: / n" ex.Message);} DECSTREAM.FLUSHFINALBLOCK (); DECSTREAM.CLOSE (); // Return Decourse data. Return memstreamDecryptedData.toArray ();} public Byte [] iv {get {return initVec;} set {initVec = value;}}

Public byte [] key {get {return} set {eNCKEY = value;}}}

// Class Description: file encryption / decryption class public class SecurityFile {private DecryptTransformer Dec_Transformer; // converter decryption private EncryptTransformer Enc_Transformer; // converter encrypted private byte [] initVec; private byte [] encKey;

Public securityFile (Encryptionalgorithm algid) {dec_transformer = new decrypttransformer (algid); enc_transformer = new encrypttransformer (algid);}

// Encrypted Offset Vector Internal Byte [] IV {Get {Return INTVEC;} set {initVec = value;}} // Encrypted key infernal byte [] key {get {return enckey;} set {eNckey = value }}

// Function Description: encrypted files public void EncryptFile (string inFileName, string outFileName, byte [] bytesKey, byte [] bytesIV) {try {FileStream fin = new FileStream (inFileName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream (outFileName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength (0); // Create variables to help with read and write byte [] bin = new byte [100];. // This is intermediate storage for THE ENCRYPTION. Long rdlen = 0; // this is the total number of bytes Written. Long Totlen = FIN.LENGTH; // this is the total length of the input file. int LEN; // this is the number of bytes to . be written at a time Enc_Transformer.IV = bytesIV; Enc_Transformer.Key = bytesKey; ICryptoTransform transform = Enc_Transformer.GetCryptoServiceProvider (bytesKey); CryptoStream encStream = new CryptoStream (fout, transform, CryptoStreamMode.Write); // Read from the input file , THE (RDLEN

// Function Description: decrypt files public void DecryptFile (string inFileName, string outFileName, byte [] bytesKey, byte [] bytesIV) {try {FileStream fin = new FileStream (inFileName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream (outFileName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength (0); // Create variables to help with read and write byte [] bin = new byte [100];. // This is intermediate storage for THE ENCRYPTION. Long rdlen = 0; // this is the total number of bytes Written. Long Totlen = FIN.LENGTH; // this is the total length of the input file. int LEN; // this is the number of bytes to be written at a time Dec_Transformer.IV = bytesIV;. Dec_Transformer.Key = bytesKey; ICryptoTransform transform = Dec_Transformer.GetCryptoServiceProvider (bytesKey); CryptoStream encStream = new CryptoStream (fout, transform, CryptoStreamMode.Write); // Read from the input file , THE (RDLEN

}

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

New Post(0)