what you don't know can hurt you

playfair.c

playfair.c
Posted Dec 21, 1999

playfair.c

tags | encryption
SHA-256 | a0fc0209ba6c9370d5c48b0230994962ac4e3dd27ec55f45a08cd8daa39c2fdc

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:

May 2022

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

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close