exploit the possibilities

playfair.c

playfair.c
Posted Dec 21, 1999

playfair.c

tags | encryption
MD5 | 74b4df11620bdd5bff9e3d77a74a807a

playfair.c

Change Mirror Download
/* Playfair encryption and decryption */

/*
All non-alphabetics in the input text are silently ignored, as is
the final odd character if any. Converting the keyword to the
keysquare is done in the traditional manner of dropping repeats and
completing the square with unused letters in alphabetical order,
despite its relative insecurity. Hey, this is a toy cipher!
Anyway, you can always give a keyword of 26 letters in your desired
order. The direction of en/decryption is seet by dir: >0 means
encrypt, <=0 means decrypt. The input text is always *pt, the
output is *ct. The code has been written for transparency more
than efficiency. I repeat, this is a toy cipher.

Copyright (c) 1994 by PC Leyland, pcl@ox.ac.uk

Permission is granted to make any use of this code as you feel
fit,as long as you give me credit for writing it and do not blame
me for any bugs you may introduce. I take no responsibility
whatsoever for any bugs I may have introduced.

*/

void playfair (int dir, char *key, char *pt, char *ct)
{
char *alph = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
char keycopy[25], pt0, pt1;
char *ptcopy = ct;
int i, j, k, lk, lp, rpt0, rpt1, cpt0, cpt1;
int alphabet [25], row[25], col[25];

for (i = 0; i < 25; i++) alphabet[i] = -1;

lk = strlen (key);
j = 0;
for (i = 0; i < lk; i++)
{
k = toupper (key[i]);
if (!isalpha (k)) continue;
if (k == 'J') k = 'I';
k = strchr (alph, k) - alph;
if (alphabet[k] != -1) continue; /* Repeat char */
alphabet[k] = j;
keycopy[j++] = alph[k];
}
for (i = 0; i < 25; i++)
{
if (alphabet[i] == -1)
{
alphabet[i] = j;
keycopy[j++] = alph[i];
}
}
for (i = 0; i < 25; i++)
{
row[i] = alphabet[i] / 5;
col[i] = alphabet[i] % 5;
}
/*
* Copy plain text, stripping out non-alphabetics and converting to
* uppercase. Use ct as the buffer, but will use ptcopy as the name.
*/
j = 0;
lp = strlen (pt);

for (i = 0; i < lp; i++)
if (isalpha (pt[i]))
ptcopy [j++] = toupper (pt[i]);
lp = j;

if (lp & 1)
{ /* Odd length plaintext. Warn and truncate */
fprintf (stdout,
"Odd number of characters in pt -- ignoring last.\n");
lp--;
}

ptcopy[lp] = '\0';

for (i = 0; i < lp; i += 2)
{
pt0 = strchr (alph, ptcopy[i]) - alph;
pt1 = strchr (alph, ptcopy[i+1]) - alph;
cpt0 = col[pt0];
cpt1 = col[pt1];
rpt0 = row[pt0];
rpt1 = row[pt1];

if (pt0 == pt1)
{
if (dir > 0)
{
ct[i] = ct[i+1] = keycopy[(cpt0 + 5 * rpt0 + 1) % 25];
}
else
{
ct[i] = ct[i+1] = keycopy[(cpt0 + 5 * rpt0 + 24) % 25];
}
}
else
if (rpt0 == rpt1)
{
if (dir > 0)
{
ct[i] = keycopy[(cpt0+1) % 5 + 5 * rpt0];
ct[i+1] = keycopy[(cpt1+1) % 5 + 5 * rpt1];
}
else
{
ct[i] = keycopy[(cpt0+4) % 5 + 5 * rpt0];
ct[i+1] = keycopy[(cpt1+4) % 5 + 5 * rpt1];
}
}
else
if (cpt0 == cpt1)
{
if (dir > 0)
{
ct[i] = keycopy[cpt0 + 5 * ((rpt0 + 1) % 5)];
ct[i+1] = keycopy[cpt1 + 5 * ((rpt1 + 1) % 5)];
}
else
{
ct[i] = keycopy[cpt0 + 5 * ((rpt0 + 4) % 5)];
ct[i+1] = keycopy[cpt1 + 5 * ((rpt1 + 4) % 5)];
}
}
else
{
ct[i] = keycopy[cpt1 + 5 * rpt0];
ct[i+1] = keycopy [cpt0 + 5 * rpt1];
}
}
}

Login or Register to add favorites

File Archive:

January 2022

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Jan 1st
    2 Files
  • 2
    Jan 2nd
    0 Files
  • 3
    Jan 3rd
    20 Files
  • 4
    Jan 4th
    4 Files
  • 5
    Jan 5th
    37 Files
  • 6
    Jan 6th
    20 Files
  • 7
    Jan 7th
    4 Files
  • 8
    Jan 8th
    0 Files
  • 9
    Jan 9th
    0 Files
  • 10
    Jan 10th
    18 Files
  • 11
    Jan 11th
    8 Files
  • 12
    Jan 12th
    19 Files
  • 13
    Jan 13th
    31 Files
  • 14
    Jan 14th
    2 Files
  • 15
    Jan 15th
    2 Files
  • 16
    Jan 16th
    2 Files
  • 17
    Jan 17th
    18 Files
  • 18
    Jan 18th
    13 Files
  • 19
    Jan 19th
    15 Files
  • 20
    Jan 20th
    29 Files
  • 21
    Jan 21st
    12 Files
  • 22
    Jan 22nd
    0 Files
  • 23
    Jan 23rd
    0 Files
  • 24
    Jan 24th
    17 Files
  • 25
    Jan 25th
    34 Files
  • 26
    Jan 26th
    23 Files
  • 27
    Jan 27th
    24 Files
  • 28
    Jan 28th
    14 Files
  • 29
    Jan 29th
    0 Files
  • 30
    Jan 30th
    0 Files
  • 31
    Jan 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2020 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close