Anonymous user
The ISAAC cipher: Difference between revisions
→{{header|C}}: Implemented task's complete solution (ie. added MOD section)
(→{{header|C}}: Optimized Vernam section a little, eliminating unnecessary variables etc.) |
(→{{header|C}}: Implemented task's complete solution (ie. added MOD section)) |
||
Line 38:
=={{header|C}}==
At the top is Bob Jenkins' reference code for ISAAC. Below and in main() is the task's
<lang C>
/* Known to compile and work with tcc in win32 & gcc on Linux (with warnings)
Line 176:
// maximum length of message
#define MAXMSG
#define MOD 95
#define START 32
// cipher modes for Caesar
enum ciphermode {
mEncipher, mDecipher, mNone
};
// XOR cipher on random stream. Output: ASCII string
Line 191 ⟶ 198:
return v;
}
// Caesar-shift a printable character
char Caesar(enum ciphermode m, char ch, char shift, char modulo, char start)
{
register int n;
if (m == mDecipher) shift = -shift;
n = (ch-start) + shift;
n = n % modulo;
if (n<0) n += modulo;
return start+n;
}
// Caesar-shift a string on a pseudo-random stream
char* CaesarStr(enum ciphermode m, char *msg, char modulo, char start)
{
register ub4 i,l;
l = strlen(msg);
// zeroise c
memset(c,'\0',l+1);
// Caesar-shift message
for (i=0; i<l; i++)
c[i] = Caesar(m, msg[i], iRandA(), modulo, start);
return c;
}
int main()
{
Line 200 ⟶ 233:
char *key = "this is my secret key";
// Vernam ciphertext & plaintext
char vctx[MAXMSG], vptx[MAXMSG];
// Caesar ciphertext & plaintext
▲ char vptx[MAXMSG];
char cctx[MAXMSG], cptx[MAXMSG];
l = strlen(msg);
//
iSeed(key,1);
// Vernam XOR encrypt▼
strcpy(vctx, Vernam(msg));
//
strcpy(cctx, CaesarStr(mEncipher, msg, MOD, START));
iSeed(key,1);
strcpy(vptx, Vernam(vctx));
//
strcpy(cptx, CaesarStr(mDecipher,cctx, MOD, START));
// Program output
printf("Message: %s\n",msg);
printf("Key : %s\n",key);
Line 219 ⟶ 256:
// Output Vernam decrypted plaintext
printf("XOR dcr: %s\n",vptx);
// Caesar
printf("MOD : ");
// Output Caesar ciphertext as a string of hex digits
for (n=0; n<l; n++) printf("%02X",cctx[n]);
printf("\n");
// Output Caesar decrypted plaintext
printf("MOD dcr: %s\n",cptx);
return 0;
}
</lang>
Sample output:
Line 229 ⟶ 272:
XOR : 1C0636190B1260233B35125F1E1D0E2F4C5422
XOR dcr: a Top Secret secret
MOD : 734270227D36772A783B4F2A5F206266236978
MOD dcr: a Top Secret secret
</pre>
|