Classical Cryptography in computer experiments

Source: Internet
Author: User
Tags lowercase printf strlen

Experiment one, traditional cipher algorithm

I. Objectives and tasks of the experiment

By programming to realize the substitution cipher algorithm and the substitution cipher algorithm, deepen the understanding to the classical cipher system, lays the foundation for the Deep Study cryptography

Second, the experimental environment

A PC running the Windows operating system that has a C language compilation environment.

Three, the principle of experiment

Classical cryptographic algorithms have historically been widely used, mostly simple, using manual and mechanical operation to achieve encryption and decryption. Its main application object is the text information, using the cipher algorithm realizes the text information encryption and the decryption. The following is a description of two typical classical cryptographic algorithms, To help readers create a preliminary impression of the cryptographic algorithm.

1. Alternate password

The principle of substitution cipher algorithm is to use substitution method to encrypt, that is, the characters in the clear text is replaced by other characters to form ciphertext. For example: PlainText letter a,b,c,d, with D,e,f,g to do the corresponding replacement after forming ciphertext.

Alternative passwords include multiple types, such as single-table alternative passwords, multiple-cipher alternative passwords, multiple-letter substitution passwords, and multiple-table replacement passwords ——.

1) below we first introduce a typical single-table substitution password, caesar (Caesar) password, also known as cyclic shift password. Its encryption method is to replace each letter in the clear text with this character in the alphabet followed by the K-letter. Its encryption process can be expressed as the following function:

E (M) = (m+k) mod n

Where: M is the number of clear letters in the alphabet, N is the number of letters in the alphabet, and K is the key; E (m) is the number of positions in the alphabet that the ciphertext parent corresponds to.

For example, for the plaintext letter H, where the number of positions in the alphabet is 7, set k=4, the cipher is calculated as L:

E (7) = (m+k) mod n = (7+4) mod = one = L

2) Here is a typical multi-table replacement password: Virginia password, which selects a phrase as the key, each letter of the key is used to determine a substitution table, each key letter is used to encrypt a clear letter. For example, the key letter is a, the plaintext letter is C, then the ciphertext mother is 0+2 (mod26) = 2, that is, C. Use the first key letter to encrypt until all key letters are exhausted and then start from scratch. In other words, the key is recycled.

Example: PlainText is attack begins at five, key is cipher,

Attack begins at five plaintext

+ cipher cipher ci pher key

----------------------------------------------

= CBIHGB DMVPRJ cb Upzv Ciphertext

Cbihgbdmvprjcbupzv after removing the space


2. Replacement password

The principle of permutation cipher algorithm is not to change the characters of Ming characters, but to change the order of Fu Daiming in the text, so as to realize the encryption of plaintext information. The replacement password is sometimes referred to as a transposition password.

Matrix Transposition method is a common method to realize permutation cipher. It arranges the letters in the plaintext in a matrix, and then re-combines the letters in the matrix according to the order provided by the key, thus forming the ciphertext. For example, the plaintext is attack begins at five, and the key is cipher, The plaintext is sorted into a matrix in the form of 6 columns per row, forming the following form:

A T t a C K

b e g i n s

A T f I v E

According to the order in which the letters in the key cipher appear in the alphabet, a permutation is given:

1 2 3 4 5 6

F= 1 4 5 3 2 6 1

According to the permutation above, the letters in the original matrix are arranged in the order of the 1th, 4th, 5th, 3rd, 2nd, and 6th columns, in the following form:

A A c T t K

b i n g e s

A I v f t E

To obtain ciphertext: Abatgftetcnvaiikse

The decryption process is based on the letter number of the key as the number of columns, the ciphertext in the order of columns, rows, and then based on the matrix permutation given by the key to produce a new matrix, thereby restoring the plaintext.

Iv. Steps of the experiment

(1) According to the experimental principle part of the introduction of alternative cryptography algorithm, to create their own plaintext information, and select a key k, write an Alternative password algorithm implementation program, the implementation of encryption and decryption operations.

(2) According to the experimental principle part of the replacement cipher algorithm introduction, self-created plaintext information, and select a key, write a replacement cipher algorithm implementation program, to achieve encryption and decryption operations.

Five: Experimental results:

1:l

2:abatgftetcnvaiikse

Vi. Experimental Study Questions

1: What is the principle of alternative passwords?

2: What is the principle of the replacement password?

The replacement password code is as follows:

#include <stdio.h> #include <math.h> #include <string.h> #define N int main () {int i=0,k,m,n,l;
	Char Str1[n],str2[n]; /*c=m+k ...
	K is key...*/printf ("Input plaintext m\n to be encrypted");
	Gets (STR1);/* Enter the plaintext m*/printf ("Input key k\n") to be encrypted;
	scanf ("%d", &k);/* Enter the key k*/M=strlen (STR1);/* Test the length of the plaintext */printf ("The length of the plaintext is%d\n", m);
	printf ("\ n *\n *\n *\n***\n *\n");
	printf ("Ciphertext (C) is:: \ n");
			for (i=0;i<m;i++)/*/Cryptographic procedure */{n= (int) str1[i];/* convert characters to ascii*/if (str1[i]== ")/* If a space appears in the string returns a space */{printf (" ");
			Str2[i]=str1[i];
			} else if (n>96&&n<123)/* Encrypts lowercase */{n= (n-97+k)%26;
			if (n<0) n=26+n;
			L= (char) (n+97);
			printf ("%c", L);
		Str2[i]=l;
			} else if (n>64&&n<91)/* Encrypts uppercase */{n= (n-65+k)%26;
			if (n<0) n=26+n;
			L= (char) (n+97);
			printf ("%c", L);
		Str2[i]=l;
	}} str2[i]= ' + ';
	/*--------------------------------*/printf ("\n\nthe C length is%d", strlen (STR2));
	printf ("\ n \ *\n *\n *\n***\n *\n"); printf ("When the CiphertexT is '%s ', \nthe password program is...::\n\n ", STR2);
	M=strlen (STR2);
		for (i=0;i<m;i++)///decryption process */{n= (int) str2[i];/* converts the character to ascii*/if (str2[i]== ')/* If it is a space, the return is also a space */{printf ("");
			} else if (n>96&&n<123)/* Decrypts lowercase */{n= (n-97-k)%26;
				if (n<0) n=26+n;
			L= (char) (n+97);
		printf ("%c", L);
			} else if (n>64&&n<91)/* Decrypts uppercase */{n= (n-65-k)%26;
			if (n<0) n=26+n;
			L= (char) (n+97);			
		printf ("%c", L);
	}} str1[i]= ' + ';
return 0; }


The replacement password code is as follows:

#include <stdio.h> #include <math.h> #include <string.h> #define N + int main () {/*------------------
	----------*/int i,j,lenk,lenm,m,n,temp;
	int t[n];
	Char K[n],m[n],c[n],temp1[n],temp2[n],temp3[n];
	/*----------------------------*/printf ("Input plaintext m\n");/* Enter clear text m*/gets (M);
	printf ("Enter key k\n");/* Enter key k*/gets (K);
	Lenk=strlen (K);
	Lenm=strlen (M);/* Set length */M=lenm/lenk;
		n=lenk;/* defines the length and width of the matrix */for (i=0;i<lenk;i++)/* To digitally sort the key */{temp=0;
			for (j=0;j<lenk;j++) {if ((int) k[i]< (int) k[j]) {temp+=1;
		}} t[i]=lenk-temp;
	printf ("%d", t[i]);/* Output the numeric order of each character */}/*--------------------------------*/printf ("\n\nfirst........\n\n");
			for (i=0;i<m;i++)/* The plaintext is arranged as a matrix */{for (j=0;j<n;j++) {Temp1[i*lenk+j]=m[i*lenk+j] according to the key;
		printf ("%c", Temp1[i*lenk+j]);
	} printf ("\ n");
	} temp1[lenm]= ' + '; /*--------------------------------*/printf ("\nsecond........\n\n"),/* for first substitution */for (i=0;i<lenk;i++) {for (j=0;j <m;j++) {Temp2[j*lenk+i]=temp1[j*lenk+T[I]-1];
	}} temp2[lenm]= ' + ';
		for (i=0;i<m;i++) {for (j=0;j<lenk;j++) {printf ("%c", Temp2[i*lenk+j]);
	} printf ("\ n"); }/*-------------------------------*/printf ("\nthird.......\n\n"),/* For a second replacement */for (i=0;i<lenk;i++) {for (j=0;j
		<m;j++) {temp3[j*lenk+i]=temp2[j*lenk+t[i]-1];
	}} temp3[lenm]= ' + ';
		for (i=0;i<m;i++) {for (j=0;j<lenk;j++) {printf ("%c", Temp3[i*lenk+j]);
	} printf ("\ n");
	}/*-----------------------------*/printf ("Output result \ n");
		for (j=0;j<lenk;j++) {for (i=0;i<m;i++) {printf ("%c", Temp3[i*lenk+j]); 
}} return 0; }



Related Article

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.