#include<stdio.h>
int main()
{ char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}}
printf("Encrypted message: %s", message);
1
PARUL MITTAL Information Network Security
return 0;}
For Decryption:-
#include<stdio.h>
int main()
{ char message[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1; }
message[i] = ch;
}} printf("Decrypted message: %s", message);
return 0; }
2
PARUL MITTAL Information Network Security
3
PARUL MITTAL Information Network Security
r2=i;
c2=j;
}
}
}
if(r1==r2)
{
if(c2==4)
printf("Ciphertext = %c%c \n", arr[r1][c1+1], arr[r2][0]);
else
printf("Ciphertext = %c%c \n", arr[r1][c1+1], arr[r2][c2+1]);
}
if(c1==c2)
{
if(r2==4) //for char in last row
printf("Ciphertext = %c%c \n", arr[r1+1][c1], arr[0][c2]);
else
printf("Ciphertext = %c%c \n", arr[r1+1][c1], arr[r2+1][c2]);
}
4
PARUL MITTAL Information Network Security
#include<stdio.h>
#include<math.h>
float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];
void encryption();
void decryption();
void getKeyMessage();
void inverse();
int main()
{
getKeyMessage();
encryption();
decryption();
}
void encryption()
{
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
}
void decryption()
{
int i, j, k;
inverse();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
printf("\nDecrypted string is: ");
for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));
printf("\n");
}
void getKeyMessage()
{
int i, j;
char msg[3];
printf("Enter 3x3 matrix for key (It should be inversible):\n");
5
PARUL MITTAL Information Network Security
6
PARUL MITTAL Information Network Security
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int count, j;
char message[50];
char key[20];
printf("\nEnter Message To Encode:\t");
fflush(stdin);
scanf("%[^\n]s", message);
printf("\nEnter Key:\t");
fflush(stdin);
scanf("%[^\n]s", key);
int message_length = strlen(message), key_length = strlen(key);
char temp_key[message_length], encrypted_message[message_length],
decrypted_message[message_length];
for(count = 0, j = 0; count < message_length; ++count, ++j)
{
if(j == key_length)
{
j = 0;
}
temp_key[count] = key[j];
}
temp_key[count] = '\0';
count = 0;
while(count < message_length)
{
encrypted_message[count] = ((message[count] + temp_key[count]) % 26) + 'A';
count = count + 1;
}
encrypted_message[count] = '\0';
count = 0;
while(count < message_length)
{
decrypted_message[count] = (((encrypted_message[count] - temp_key[count]) +
26) % 26) + 'A';
count = count + 1;
}
decrypted_message[count] = '\0';
printf("\nIntial String:\t%s", message);
printf("\nKey:\t%s", key);
printf("\nGenerated Key:\t%s", temp_key);
printf("\nEncrypted Message:\t%s", encrypted_message);
printf("\nDecrypted Message:\t%s", decrypted_message);return 0;}
7
PARUL MITTAL Information Network Security
#include<stdio.h>
#include<string.h>
void encryptMsg(char msg[], int key){
int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0;
char railMatrix[key][msgLen];
row = row + k;
}
printf("\nEncrypted Message: ");
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
if(railMatrix[i][j] != '\n')
printf("%c", railMatrix[i][j]);
}
void decryptMsg(char enMsg[], int key){
int msgLen = strlen(enMsg), i, j, k = -1, row = 0, col = 0, m = 0;
char railMatrix[key][msgLen];
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){
railMatrix[row][col++] = '*';
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
}
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
if(railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
printf("\nDecrypted Message: ");
for(i = 0; i < msgLen; ++i){
8
PARUL MITTAL Information Network Security
printf("%c", railMatrix[row][col++]);
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
}
}
int main(){
char msg[] = "Hello World";
char enMsg[] = "Horel ollWd";
int key = 3;
printf("Original Message: %s", msg);
encryptMsg(msg, key);
decryptMsg(enMsg, key);
return 0;
}
9
PARUL MITTAL Information Network Security
10
PARUL MITTAL Information Network Security
JOptionPane.showMessageDialog(null,"Decrypted Data"+"\n"+decryptedMessage);
}
catch(Exception e)
{
System.out.println(e);
}
}
void generateSymmetricKey() {
try {
Random r = new Random();
int num = r.nextInt(10000);
String knum = String.valueOf(num);
byte[] knumb = knum.getBytes();
skey=getRawKey(knumb);
skeyString = new String(skey);
System.out.println("DES Symmetric key = "+skeyString);
}
catch(Exception e)
{
System.out.println(e);
}
}
private static byte[] getRawKey(byte[] seed) throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("DES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(56, sr);
11
PARUL MITTAL Information Network Security
12
PARUL MITTAL Information Network Security
13
PARUL MITTAL Information Network Security
printf("\nWRONG INPUT\n");
getch();
exit(1);}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for (i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
getch();
}
int prime(long int pr) {
int i;
j=sqrt(pr);
for (i=2;i<=j;i++) {
if(pr%i==0)
return 0;
}
return 1;
}
14
PARUL MITTAL Information Network Security
void ce() {
int k;
k=0;
for (i=2;i<t;i++) {
if(t%i==0)
continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q) {
e[k]=i;
flag=cd(e[k]);
if(flag>0) {
d[k]=flag;
k++;}
if(k==99)
break;
}
}
}
long int cd(long int x) {
long int k=1;
while(1) {
k=k+t;
if(k%x==0)
return(k/x);
}}
void encrypt() {
long int pt,ct,key=e[0],k,len;
15
PARUL MITTAL Information Network Security
i=0;
len=strlen(msg);
while(i!=len) {
pt=m[i];
pt=pt-96;
k=1;
for (j=0;j<key;j++) {
k=k*pt;
k=k%n;}
temp[i]=k;
ct=k+96;
en[i]=ct;
i++;}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for (i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}
void decrypt() {
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1) {
ct=temp[i];
k=1;
for (j=0;j<key;j++) {
k=k*ct;
k=k%n;}
16
PARUL MITTAL Information Network Security
pt=k+96;
m[i]=pt;
i++;
}
m[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for (i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}
17
PARUL MITTAL Information Network Security
18
PARUL MITTAL Information Network Security
scanf("%d",&y);
b=power(g,y,n);
printf("key for the first person is :%lld\n",power(b,x,n));
printf("key for the second person is :%lld\n",power(a,y,n));
}
19
PARUL MITTAL Information Network Security
20
PARUL MITTAL Information Network Security
s = fabs(sin(1+i));
k[i] = (unsigned)( s * pwr );
}
return k;
}
unsigned rol( unsigned r, short N )
{
unsigned mask1 = (1<<N) -1;
return ((r>>(32-N)) & mask1) | ((r<<N) & ~mask1);
}unsigned *md5( const char *msg, int mlen)
{
static DigestArray h0 = { 0x67452301, 0xEFCDAB89,
0x98BADCFE, 0x10325476 };
static DgstFctn ff[] = { &func0, &func1, &func2, &func3};
static short M[] = { 1, 5, 3, 7 };
static short O[] = { 0, 1, 5, 0 };
static short rot0[] = { 7,12,17,22};
static short rot1[] = { 5, 9,14,20};
static short rot2[] = { 4,11,16,23};
static short rot3[] = { 6,10,15,21};
static short *rots[] = {rot0, rot1, rot2, rot3 };
static unsigned kspace[64];
static unsigned *k;
static DigestArray h;
DigestArray abcd;
DgstFctn fctn;
short m, o, g;
unsigned f;
21
PARUL MITTAL Information Network Security
short *rotn;
union
{
unsigned w[16];
char b[64];
}mm;
int os = 0;
int grp, grps, q, p;
unsigned char *msg2;
if (k==NULL) k= calctable(kspace);
for (q=0; q<4; q++) h[q] = h0[q]; // initialize
{
grps = 1 + (mlen+8)/64;
msg2 = malloc( 64*grps);
memcpy( msg2, msg, mlen);
msg2[mlen] = (unsigned char)0x80;
q = mlen + 1;
while (q < 64*grps){ msg2[q] = 0; q++ ; }
{
MD5union u;
u.w = 8*mlen;
q -= 8;
memcpy(msg2+q, &u.w, 4 );
}
}
for (grp=0; grp<grps; grp++)
{
22
PARUL MITTAL Information Network Security
23
PARUL MITTAL Information Network Security
24
PARUL MITTAL Information Network Security
25
PARUL MITTAL Information Network Security
System.out.println(""); }
catch (Exception e) {
System.out.println("Exception: " +e);
}
}
public static String bytesToHex(byte[] b)
{
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
StringBuffer buf = new StringBuffer();
for (int j=0; j<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]); }
return buf.toString();
}
}
26
PARUL MITTAL Information Network Security
27
PARUL MITTAL Information Network Security
28
PARUL MITTAL Information Network Security
29
PARUL MITTAL Information Network Security
30