[Information Security] Cracking Vignere Code

This is the first homework for Information Security Course. The Vignere Code is the following:

ktbueluegvitnthuexmonveggmrcgxptlyhhjaogchoemqchpdnetxupbqntietiabpsmaoncnwvoutiugtagmmqsxtvxaoniiogtagmbpsmtuvvihpstpdvcrxhokvhxotawswquunewcgxptlcrxtevtubvewcnwwsxfsnptswtagakvoyyak

Cracking this code needs three steps: 1. determine the key length 2. find out the letters of the key 3. cracking the ciphertext using the key.

1. determine the key length -- calculating  IC value for each possible key length. If the value is approximate to 0.0687, then it is likely to be the correct length.

// obtain average IC
#include <stdio.h>
int main()
{
	int i=0, j=1, p, q, m;
	float sum=0, len, sum1[11];
	int letter[27];
	char code[100][100];
	char a;
//len is the length of the key	
	len = 3;	
//read in the code
	while(1)
	{
		scanf("%c", &a);
		if(a=='\n')
			break;
		i++;
		if(i==len+1)
		{
			i=1;
			j++;
		}
		code[i][j] = a;
	}
	m=i;
//calculate each row's IC
	for(q=1;q<=10;q++)
	{
		sum=0;
		for(i=1;i<=26;i++)	letter[i]=0;
		for(i=1;i<j;i++)
		{
			for(p=0;p<=25;p++)
			{
				if(code[q][i]==97+p)
					letter[p+1]++;
			}
		}
		for(i=1;i<=26;i++)
			sum=sum+letter[i]*(letter[i]-1);
		if(q<=m)
			sum1[q]=sum/(j*(j-1));
		else
			sum1[q]=sum/((j-1)*(j-2));
	}
	sum = 0;
//output
	for(i=1;i<=m;i++)
	{
		sum=sum+sum1[i];
		printf("%f \n", sum1[i]);
	}
	printf("the average IC is: %f", sum/len);
	return 0;
}


2. find out the letters of the key -- calculate M value for each letter on every position of the key. If it is approximate to 0.0387, then it is likely to appear in a certain place of the key.

//find out the letter
#include <stdio.h>
int main()
{
	int i=0, j=1, p=1, q, m, length, move, t;
	float sum=0, tol, len, sum1[11], stand[27]={0,0.082,0.015,0.028,0.043,0.127,0.022,0.02,0.061,0.07,0.002,0.008,0.04,0.024,0.067,0.075,0.019,0.001,0.06,0.063,0.091,0.028,0.01,0.024,0.002,0.02,0.001};
//the standard frequency of letter A-Z
	int letter[27];
	char code[100][100];
	char a;
//len is the length of the key	
	len = 3;
	move = 19;	
//read in the code
	while(1)
	{
		scanf("%c", &a);
		if(a=='\n')
			break;
		i++;
		if(i==len+1)
		{
			i=1;
			j++;
		}
		code[i][j] = a;
	}
	m=i;
	length = j;
//calculate each row's IC
	for(q=1;q<=len;q++)
	{
		sum=0;
		tol=0;
		for(i=1;i<=26;i++)	letter[i]=0;
		for(i=1;i<length;i++)
		{
			for(p=0;p<=25;p++)
			{
				if(code[q][i]==97+p)
					letter[p+1]++;
			}
		}
		if(q<=m){
			for(i=1;i<=26;i++)
			{
				t=(i+move)%26;
				if(t==0)
					t = 26;
				tol=tol+stand[i]*letter[t]/length;
			}	
		}
		else{
			for(i=1;i<=26;i++)
			{
				t=(i+move)%26;
				if(t==0)
					t = 26;
				tol=tol+stand[i]*letter[t]/(length-1);
			}
		}	
		printf("%f\n", tol);
	}	
	return 0;
}

 3. cracking the ciphertext using the key -- this step is relatively easy for it only needs to translate the ciphertext referring to the Vignere Table

// [4] translate the code into plaintext
#include <stdio.h>
int main()
{
	int l=0, i, j, t, key[3],k;
	char code[200];
	char std[27];
	key[0]=19;
	key[1]=2;
	key[2]=0;
	while(1)
	{
		l++;
		scanf("%c", &code[l]);
		if(code[l]>'z' || code[l]<'a')
			{
				l--;
				break;
			}
	}
	for(i=1;i<=26;i++)
		std[i]=96+i;
	for(j=1;j<=l;j++)
	{
		i=j%3;
		k=key[i];			
		t=(code[j]-96-k+26)%26;
		if(t==0)
			t=26;
		printf("%c", std[t]);
	}
	return 0;     
}

the final plaintext turns out to be:

It is essential toseek out enemy agents who have come to conduct espionage against you and tobribe them to serve. You give them instructions and comfort them. Thus doubleagents are recruited and used Sun Tzu the Art of War.

One character of this course is that you need to learn something else yourself after class so as to accomplish it. I search for methods in order to crack this kind of code. Every time I learn something new, I am always glad, however simple.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值