The implementation of a 128-bit efficient text Encryption Algorithm

Source: Internet
Author: User

1

2. Source:

<A fast and secure encryption algorithm for message communication> (ictes 2007)

The main advantage is fast

In my own test, the encryption throughput is 67618 Bytes/sec, which is much higher than des blowfish at GHz in inter t6600.

The disadvantage is that only the 32-ASCII code is supported. It is obvious that the decryption is garbled after the carriage return is encrypted.

Basic Principles:

3. Encrypted class

# Pragma once # include <string> # include <cstring> using namespace STD; Class matrix_encreption {char asc_matrix [16] [95]; // ASCII code matrix bool is_setkey; // whether the key is set to Char key [16]; // key 128-bit char plain_text [16]; // The processed plaintext block contains 128-bit char lever_one_cipher [16]; // The first-level ciphertext char cipher_text [16]; // The 128-bit public ciphertext block processed: matrix_encreption (void );~ Matrix_encreption (void); void set_key (const char *); // The key must be a 128-bit 16-byte string void show_key () const; void init_matrix () throw (const char *); // initialization matrix. You must call void show_matrix () const after setting the key; // void set_plain (); // set the plaintext void set_plain (const char *); const char * get_plain (); // void set_cipher (); // set the ciphertext void set_cipher (const char *); const char * get_cipher (); // void init_buf (); // clear the Buf plaintext and ciphertext void substitution_mapping (); // Replace the voing void translation (INT ); // translate void transposing (INT); // transpose void encreption (); void de_substitution_mapping (); // Replace the voing void de_translation (INT ); // translate void de_transposing (INT); // transpose void decreption (); void right_round_shift (char * P, int size, int N); // The right shift parameter of the Loop: number of capacity shifts in the target array void left_round_shift (char * P, int size, int N); // shifts left in a loop };

# Include "stdafx. H "# include" matrix_encreption.h "# include <iostream> # include <cstring> # include <string> # include <sstream> using namespace STD; matrix_encreption: matrix_encreption (void) {is_setkey = false;} matrix_encreption ::~ Signature (void) {} void matrix_encreption: set_key (const char * Str) {memcpy (Key, STR, 16); is_setkey = true; init_matrix ();} void matrix_encreption :: show_key () const {If (! Is_setkey) {cout <"key is not set" <Endl; return;} cout <"Key is:"; for (INT I = 0; I <16; I ++) {cout <key [I];} cout <Endl;} void matrix_encreption: init_matrix () Throw (const char *) {If (! Is_setkey) {Throw "Please set key first";} // fill in for with ASCII code (INT I = 0; I <16; I ++) {for (Int J = 0; j <95; j ++) {asc_matrix [I] [J] = 32 + J + '\ 0 ';}} // shift for (INT I = 0; I <15; I ++) {right_round_shift (asc_matrix [I], 95, key [I] + key [I + 1]);} right_round_shift (asc_matrix [15], 95, key [0] + key [15]);} void matrix_encreption :: show_matrix () const {for (INT I = 0; I <16; I ++) {for (Int J = 0; j <95; j ++) {cout <asc_matrix [I] [J];} cout <Endl ;} }/* Void matrix_encreption: set_plain () {cout <"Enter plaintext:" <Endl; CIN> plain_buf;} */void matrix_encreption :: set_plain (const char * Str) {// cout <"entering plaintext:" <STR <Endl; memcpy (plain_text, STR, 16 );} const char * matrix_encreption: get_plain () {return lever_one_cipher;} void matrix_encreption: set_cipher (const char * Str) {// cout <"entering ciphertext" <STR <Endl; memcpy (cipher_text, STR, 16);} const char * matrix _ Encreption: get_cipher () {return lever_one_cipher;} void matrix_encreption: substitution_mapping () // replace {ing {char temp [16]; for (INT I = 0; I <16; I ++) {temp [I] = asc_matrix [I] [plain_text [I]-32];} memcpy (lever_one_cipher, temp, 16);} void matrix_encreption :: translation (INT round) // convert round 0 ~ 7 {string temp_str (asc_matrix [round]); string k_ts_n = temp_str.substr (0, 16); For (INT I = 0; I <16; I ++) {lever_one_cipher [I] ^ = k_ts_n [I]; // exclusive or} void matrix_encreption: transposing (INT round) // shift {char left [8]; // left 8 a2char right [8]; // right 8 a3char all [16]; // All char k_tp_n0 = asc_matrix [round] [0]; char k_tp_n1 = asc_matrix [round] [1]; char k_tp_n2 = asc_matrix [round] [2]; char k_tp_n3 = asc_matrix [round] [3]; m Emcpy (all, lever_one_cipher, 16); right_round_shift (all, 16, k_tp_n0); // right shift memcpy (left, all, 8); memcpy (right, all + 8 ); right_round_shift (left, 8, k_tp_n1); // left and right shift left_round_shift (right, 8, k_tp_n2); // right shift left shift memcpy (all, left, 8 ); memcpy (all + 8, right, 8); right_round_shift (all, 16, k_tp_n3); // move memcpy right together (lever_one_cipher, all, 16);} void matrix_encreption :: encreption () // 128-bit encryption {// subing substitution_mapping (); // translation and transpose for (INT Ro Und = 0; Round <8; ++ round) {translation (round); transposing (round) ;}} void matrix_encreption: de_substitution_mapping () // reverse replace ing {for (INT I = 0; I <16; I ++) {char c = lever_one_cipher [I]; for (Int J = 0; j <95; j ++) {If (asc_matrix [I] [J] = c) {lever_one_cipher [I] = J + 32 + '\ 0 '; break ;}}} void matrix_encreption: de_transposing (INT round) // reversely sets Step1 {char left [8]; // left 8 a2char right [8]; // right 8 a3char all [16]; // All char k_tp_n 0 = asc_matrix [round] [0]; char k_tp_n1 = asc_matrix [round] [1]; char k_tp_n2 = asc_matrix [round] [2]; char k_tp_n3 = asc_matrix [round] [3]; memcpy (all, lever_one_cipher, 16); left_round_shift (all, 16, k_tp_n3); memcpy (left, all, 8 ); memcpy (right, all + 8); left_round_shift (left, 8, k_tp_n1); right_round_shift (right, 8, k_tp_n2); memcpy (all, left, 8 ); memcpy (all + 8, right, 8); left_round_shift (all, 16, k_tp_n0); memcpy (lever_one _ Cipher, all, 16);}/* void matrix_encreption: de_translation (INT round) // The reverse translation seems to be the same {string temp_str (asc_matrix [round]); string k_ts_n = temp_str.substr (0, 16); // The first 0 ~ 15For (INT I = 0; I <16; I ++) {lever_one_cipher [I] ^ = k_ts_n [I]; // exclusive or} */void matrix_encreption :: decreption () {memcpy (lever_one_cipher, cipher_text, 16); For (INT round = 7; Round>-1; -- round) {de_transposing (round); translation (round ); // not changed} de_substitution_mapping ();} void matrix_encreption: right_round_shift (char * P, int size, int N) // The right shift parameter of the Loop: target array capacity shift count {int K = n % size; // valid shift count char * temp = new char [size]; for (INT I = 0; I <size; I ++) {if (I + k <size) {temp [I + k] = P [I];} else {temp [I + k-size] = P [I] ;}} for (INT I = 0; I <size; I ++) {P [I] = temp [I]; // cout <p [I];} // cout <Endl; Delete temp;} void matrix_encreption :: left_round_shift (char * P, int size, int N) // shifts left of the loop {int K = n % size; right_round_shift (p, size, size-k );} /* void matrix_encreption: init_buf () {// cout <"Clear plaintext ciphertext in memory" <Endl; // plain_buf = ""; // cipher_buf = "";}*/

Use encryption

# Include "stdafx. H "# include" matrix_encreption.h "# include <iostream> # include <fstream> # include <time. h> using namespace STD; int _ tmain (INT argc, _ tchar * argv []) {/* Timing module */clock_t start, finish; double totaltime; start = clock ();/* Start Program */matrix_encreption m; M. set_key ("123 ujhgfbvfdcxsa"); // it must be a 16-bit ifstream F1 ("plain.txt", IOS: Binary | IOs: In); If (! F1) {cout <"plain.txt failed" <Endl; System ("pause"); return 1 ;}ofstream F2 ("cipher.txt", IOS: Binary | IOs :: out); If (! F2) {cout <"failed to open cipher.txt" <Endl; System ("pause"); return 1 ;}char buf1 [16]; while (F1) {memset (buf1, '', 16); // fill f1.read (buf1, 16) with spaces when less than 16; If (f1.gcount () = 0) break; M. set_plain (buf1); M. encreption (); f2.write (M. get_cipher (), 16);} f1.close (); f2.close (); ifstream F3 ("cipher.txt", IOS: Binary | IOs: In); If (! F3) {cout <"failed to open cipher.txt" <Endl; System ("pause"); return 1 ;}ofstream F4 ("plain_out.txt", IOS: Binary | IOs :: out); If (! F4) {cout <"plain_out.txt failed" <Endl; System ("pause"); return 1 ;}char buf2 [16]; while (F3) {memset (buf2, '', 16); // fill f3.read (buf2, 16) with spaces below 16; int A = f3.gcount (); If (f3.gcount () = 0) break; M. set_cipher (buf2); M. decreption (); f4.write (M. get_plain (), 16);} f3.close (); f4.close ();/* Timing module */finish = clock (); totaltime = (double) (finish-start) /clocks_per_sec; cout <"\ n the running time of this program is" <totaltime <"seconds! "<Endl; System (" pause "); 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.