Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CODE:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int S[256];
int i=0;
for(i=0;i<256;i++)
S[i]=i;
int j=0,len;
cout<<"Enter the key length"<<endl;
cin>>len;
int key[len];
cout<<"Enter the key values"<<endl;
int temp;
for(i=0;i<len;i++)
{
cin>>temp;
key[i]=temp;
}
for(i=0;i<256;i++)
{
j=(j+S[i]+key[i%len])%256;
swap(S[i],S[j]);
}
int len2;
cout<<"Enter the text length"<<endl;
cin>>len2;
int text[len2];
cout<<"Enter the text values"<<endl;
for(i=0;i<len2;i++)
{
cin>>temp;
text[i]=temp;
}
i=0;
j=0;
int res[len2];
for(int p=0;p<len2;p++)
{
i=(i+1)%256;
j=(j+S[i])%256;
swap(S[i],S[j]);
res[p]=S[(S[i]+S[j])%256];
cout<<res[p]<<" ";
res[p]=res[p]^text[p];
}
cout<<endl;
for(i=0;i<len2;i++)
cout<<res[i]<<" ";
cout<<endl;
}
OUTPUT:
2. Write Program for encryption and decryption using RC5
CODE:
#include <stdio.h>
#include <time.h>
#define KEYSIZE 16
typedef unsigned int WORD;
typedef enum { ShiftLeft, ShiftRight } ShiftDir;
typedef enum { KeyWords = KEYSIZE / 4, NumRounds = 15, TableSize = 32 } bogus;
WORD Table[ TableSize ];
WORD L[KeyWords];
WORD ROT(const WORD x, const WORD y, const ShiftDir dir)
{
const unsigned int ShiftAmt = (y & (unsigned int)0x1f);
const unsigned int ShiftBack = 0x20 - ShiftAmt;
unsigned int result;
if (dir == ShiftLeft)
result = (x << ShiftAmt) | (x >> ShiftBack);
else
result = (x >> ShiftAmt) | (x << ShiftBack);
return result;
}
void SetKey( unsigned char KeyChar )
{
static unsigned int KeyCntr;
static unsigned int Shift;
A = 0;
B = 0;
i = 0;
j = 0;
if (sizeof(WORD)!=4)
printf("RC5 error: WORD has %d bytes.\n",sizeof(WORD));
printf("RC5-32/12/16 encryption:\n");
for (i = 1; i<6; i++)
{
PlainText1[0] = CryptoText[0];
PlainText1[1] = CryptoText[1];
for (j=0; j < KEYSIZE; j++)
SetKey((unsigned char)keystr[j]);
setup();
encrypt(PlainText1,CryptoText);
decrypt(CryptoText,PlainText2);