LINUX, C # using the DES algorithm CBC mode for symmetric encryption, decryption

Source: Internet
Author: User
Tags openssl

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; } 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.