OpenSSL is a cryptographic library that encapsulates multiple algorithms, and we use the DES algorithm CBC mode here.
first, prepare OpenSSL Linux and C # library
OpenSSL Project Address:
https://www.openssl.org/
Use a self-band library under C #
Second, the Operation function in C # encryption and decryption:
Using System.Text;
Using System.IO;
Using System.Security.Cryptography;
DES DES_CBC = new DESCryptoServiceProvider ();
Public byte[] Key_b;
Public byte[] Iv_b; <summary> initialization </summary>///<param name= "key" > Key </param>///<param name= "vi" > vector < /param> public void init (string key, String vi) {Key_b = ASCIIEncoding.UTF8.GetBytes (key); Key Iv_b = ASCIIEncoding.UTF8.GetBytes (vi); Initialize Vector DES_CBC.
Mode = CIPHERMODE.CBC; DES_CBC.
Padding = Paddingmode.zeros; ///<summary>des cryptographic CBC mode 0 padded </summary>///<param name= "Input" > Clear text </param> public byte[] Ecrypt
(byte[] input)
{MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, DES_CBC.
CreateEncryptor (Key_b, Iv_b), cryptostreammode.write); Cs. Write (input, 0, input.
Length); Cs.
Close (); Byte[] ciphertext = Ms.
ToArray ();
return ciphertext; ///<summary>des decryption CBC mode 0 completion </summary>///<param name= "Input" > MingText </param> public byte[] Decrypt (byte[] input) {MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, DES_CBC.
CreateDecryptor (Key_b, Iv_b), cryptostreammode.write); Cs. Write (input, 0, input.
Length); Cs.
FlushFinalBlock (); Byte[] ciphertext = Ms.
ToArray ();
return ciphertext;
}
Compress the decompression under Linux:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/ Des.h>//DES-CBC Encryption Method * 8-bit key, encrypted content 8-bit padded, padded to fill 0 int main (int argc, char *argv[]) {unsigned char buff[20]={"123412
341234 "};
unsigned char encry_buff[20];
int packet_len = strlen (buff);
int i;
int Encry_len;
memset (encry_buff,0,20);
Key Des_cblock key_des = "pk$ @gtjt";
Encryption vector Des_cblock Ivec = "thvn#&@@";
Decryption vector des_cblock Ivec_de = "thvn#&@@";
Des_key_schedule schedule;
Des_set_key_unchecked ((const_des_cblock*) key_des, &schedule);
Encry_len = (strlen (buff) +7)/8*8;//length unsigned char *output= (char*) malloc (Encry_len);
Encrypted memset (output) (Output,0,strlen);
Des_ncbc_encrypt (buff, output, strlen (buff), &schedule, &ivec, Des_encrypt);
Decryption memset (buff,0,20);
Des_ncbc_encrypt (output, buff, strlen (output), &schedule, &ivec_de, Des_decrypt);
Free (output);
return 0; }