Some classical cryptography algorithms

Source: Internet
Author: User
Tags decrypt

Classical cryptography can be divided into alternative passwords (also known as shift passwords) and replacement password (also known as transposition of the password) two, which instead of the typical password has Caesar password, affine transformation, replacement password has a single table replacement and multiple table replacement.

The following are the implementations of several common classical cryptography algorithms.

1.Caesar Password

void Encrypt (char* text,int K,char cipher[1024]) {
	int a[26];int a[26];
	int m;
	for (int i=97;i<123;i++) {
		a[i-97]=i;
		a[i-97]=i-32;
	}

	int Len=strlen (text);
	for (int j=0;j<len;j++) {	
		int t=text[j];
		if (96<t&&t<123) {
			m= (text[j]+k-97)%26;
			CIPHER[J]=A[M];
		}
		else if (64<t&&t<91) {
			m= (text[j]+k-65)%26;
			CIPHER[J]=A[M];
		}
		else
			cipher[j]= ';
	}	
}


void Decrypt (char* cipher,int K,char text[1024]) {
	int a[26];int a[26];
	int m;
	for (int i=97;i<123;i++) {
		a[i-97]=i;
		a[i-97]=i-32;
	}
	int Len=strlen (cipher);
	for (int i=0;i<len;i++) {
		int t=cipher[i];
		if (96<t&&t<123) {
			m= (cipher[i]-k-71)%26;
			TEXT[I]=A[M];
		}
		else if (64<t&&t<91) {
			m= (cipher[i]-k-39)%26;
			TEXT[I]=A[M];
		}
		else
			text[i]= ';
	}
}

2. Single Table replacement

#include "Ceasar.h" #include "stdafx.h" void Transkey (char* key,char table[27]) {char tem[100]={0};
	Char table1[30]={0};

	for (int i=97;i<123;i++) table1[i-97]=i;
	int Len1=strlen (key);
	strcpy (Tem,key);

	strcpy (TEM+LEN1,TABLE1);
	int Len=strlen (TEM);
	int K=1;int flag;
	if (Isupper (tem[0])) tem[0]+=32;
	TABLE[0]=TEM[0];
		for (int i=1;i<strlen (TEM) &&k<26;i++) {if (!isalpha (tem[i)) continue;
		if (Isupper (Tem[i])) tem[i]+=32;
		flag=0;
			for (int j=0;j<k;j++) {if (Table[j]==tem[i]) {flag=1;break;	
	} if (!flag) table[k++]=tem[i];
	} void Encrypt (char* text,char* Key,char cipher[1024]) {char table[27]={0};

	Transkey (key,table);
	int Len=strlen (text);
		for (int i=0;i<len;i++) {if (!isalpha (Text[i])) cipher[i]=text[i];
		else if (Islower (Text[i])) cipher[i]=table[text[i]-97];
	else cipher[i]=table[text[i]-65]-32;
	} void Ttable (char table[27]) {char ip[30]={0};
	for (int i=97;i<122;i++) {ip[table[i-97]-' a ']=i; } memcpy (table,ip,26);
	} void Decrypt (char* cipher,char* Key,char text[1024]) {char table[27]={0};
	Transkey (key,table);

	Ttable (table);
	int Len=strlen (cipher);
		for (int i=0;i<len;i++) {if (!isalpha (Cipher[i])) text[i]=cipher[i];
		else if (Islower (Cipher[i])) text[i]=table[cipher[i]-97];
	else text[i]=table[cipher[i]-65]-32;


 }
}
3. Affine transformation

#include "Ceasar.h"
#include "stdafx.h"


void Encrypt (char* text,int k1,int K2,char cipher[1024]) {
	int Len=strlen (text);
	int e=0;
	for (int j=0;j<len;j++) {	
			if (!isalpha (text[j))) {
				cipher[j]=text[j];
			}	
			else if (Islower (Text[j])) {
				e=text[j]-' a ';
				CIPHER[J] = (e*k1+k2)% + ' a ';
			}
			else{
				e=text[j]-' A ';
				CIPHER[J] = (e*k1+k2)% + ' A ';

}} int rec (int k,int m) {
	int i=1;
	int j=0;
	while (1) {for
		(j=0;k*j<m*i+1;j++) {}
		if (k*j==m*i+1) break
			;
		else
			i++;
	}
	return j;
}
void Decrypt (char* cipher,int k1,int K2,char text[1024]) {
	int len=strlen (cipher);
	int  e=0;
	int K3=rec (k1,26);
	for (int i=0;i<len;i++) {
		if (!isalpha (cipher[i))) {
			text[i]=cipher[i];
		}
		else if (Islower (Cipher[i])) {
				e=cipher[i]-' a ';
				Text[i] = (k3* (e-k2+26))% + ' a ';
		}
		else{   
			e = cipher[i]-' A ';
			Text[i]= ((e-k2+26) *k3)% + ' A ';}
		}






4. Virginia Code

#include "Ceasar.h"
#include "stdafx.h"


void Encrypt (char* text,char* Key,char cipher[1024]) {
	int len= strlen (text);
	int Lenkey=strlen (key);
	for (int j=0;j<lenkey;j++)	//keyсд
		if (Isupper (key[j))
			key[j]+=32;
	for (int i=0;i<len;i++) {
		if (!isalpha (Text[i]))
			cipher[i]=text[i];
		else if (Isupper (Text[i]))
			cipher[i]= (text[i]-' A ' +key[i%lenkey]-' a ')%26+ ' a ';
		else
			cipher[i]= (text[i]-' A ' +key[i%lenkey]-' a ')%26+ ' a ';
	}
}


void Decrypt (char* cipher,char* Key,char text[1024]) {
	int len=strlen (cipher);
	int Lenkey=strlen (key);
	for (int j=0;j<lenkey;j++)	//keyсд
		if (Isupper (key[j))
			key[j]+=32;
	for (int i=0;i<len;i++) {
		if (!isalpha (Cipher[i]))
			text[i]=cipher[i];
		else if (Isupper (Cipher[i]))
			text[i]= (26+cipher[i]-' A '-(key[i%lenkey]-' a '))%26+ ' a ';
		else
			text[i]= (26+cipher[i]-' A '-(key[i%lenkey]-' a '))%26+ ' a ';
	}







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.