Task: The ISAAC cipher

C: 
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#ifdef _MSC_VER
  typedef unsigned __int32 uint32_t;
#else
  #include <stdint.h>
#endif


typedef  uint32_t  ub4;


ub4 randrsl[256], randcnt;


static    ub4 mm[256];
static    ub4 aa=0, bb=0, cc=0;

void isaac()
{
   register ub4 i,x,y;

   cc = cc + 1;    
   bb = bb + cc;   

   for (i=0; i<256; ++i)
   {
     x = mm[i];
     switch (i%4)
     {
     case 0: aa = aa^(aa<<13); break;
     case 1: aa = aa^(aa>>6); break;
     case 2: aa = aa^(aa<<2); break;
     case 3: aa = aa^(aa>>16); break;
     }
     aa              = mm[(i+128)%256] + aa;
     mm[i]      = y  = mm[(x>>2)%256] + aa + bb;
     randrsl[i] = bb = mm[(y>>10)%256] + x;
   }
   
   randcnt = 0;
}


#define mix(a,b,c,d,e,f,g,h) \
{ \
   a^=b<<11; d+=a; b+=c; \
   b^=c>>2;  e+=b; c+=d; \
   c^=d<<8;  f+=c; d+=e; \
   d^=e>>16; g+=d; e+=f; \
   e^=f<<10; h+=e; f+=g; \
   f^=g>>4;  a+=f; g+=h; \
   g^=h<<8;  b+=g; h+=a; \
   h^=a>>9;  c+=h; a+=b; \
}

void randinit(int flag)
{
   register int i;
   ub4 a,b,c,d,e,f,g,h;
   aa=bb=cc=0;
   a=b=c=d=e=f=g=h=0x9e3779b9;  

   for (i=0; i<4; ++i)          
   {
     mix(a,b,c,d,e,f,g,h);
   }

   for (i=0; i<256; i+=8)   
   {
     if (flag)                  
	 {
       a+=randrsl[i  ]; b+=randrsl[i+1]; c+=randrsl[i+2]; d+=randrsl[i+3];
       e+=randrsl[i+4]; f+=randrsl[i+5]; g+=randrsl[i+6]; h+=randrsl[i+7];
     }
     mix(a,b,c,d,e,f,g,h);
     mm[i  ]=a; mm[i+1]=b; mm[i+2]=c; mm[i+3]=d;
     mm[i+4]=e; mm[i+5]=f; mm[i+6]=g; mm[i+7]=h;
   }

   if (flag)
   {        
	 for (i=0; i<256; i+=8)
     {
       a+=mm[i  ]; b+=mm[i+1]; c+=mm[i+2]; d+=mm[i+3];
       e+=mm[i+4]; f+=mm[i+5]; g+=mm[i+6]; h+=mm[i+7];
       mix(a,b,c,d,e,f,g,h);
       mm[i  ]=a; mm[i+1]=b; mm[i+2]=c; mm[i+3]=d;
       mm[i+4]=e; mm[i+5]=f; mm[i+6]=g; mm[i+7]=h;
     }
   }

   isaac();            
   randcnt=0;        
}



ub4 iRandom()
{
	ub4 r = randrsl[randcnt];
	++randcnt;
	if (randcnt >255) {
		isaac();
		randcnt = 0;
	}
	return r;
}



char iRandA()
{	
	return iRandom() % 95 + 32;
}



void iSeed(char *seed, int flag)
{
	register ub4 i,m;
	for (i=0; i<256; i++) mm[i]=0;
	m = strlen(seed);
	for (i=0; i<256; i++)
	{
	
        if (i>m) randrsl[i]=0;  else randrsl[i] = seed[i];
	}
	
	randinit(flag);
}



#define MAXMSG 4096
#define MOD 95
#define START 32

enum ciphermode {
	mEncipher, mDecipher, mNone 
};



char v[MAXMSG];
char* Vernam(char *msg)
	{
		register ub4 i,l;
		l = strlen(msg);
		
		memset(v,'\0',l+1);
		
		for (i=0; i<l; i++) 
			v[i] = iRandA() ^ msg[i];
		return v;
	}

	

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;
	}
	

char c[MAXMSG];
char* CaesarStr(enum ciphermode m, char *msg, char modulo, char start)
	{
		register ub4 i,l;
		l = strlen(msg);
		
		memset(c,'\0',l+1);
		
		for (i=0; i<l; i++) 
			c[i] = Caesar(m, msg[i], iRandA(), modulo, start);
		return c;
	}

	
int main()
{
	register ub4 n,l;
	
	char *msg = "a Top Secret secret";
	char *key = "this is my secret key";
	
	char vctx[MAXMSG], vptx[MAXMSG];
	
	char cctx[MAXMSG], cptx[MAXMSG];
	l = strlen(msg);
	
	iSeed(key,1);
	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));
	
	printf("Message: %s\n",msg);
	printf("Key    : %s\n",key);
	printf("XOR    : ");
	
	for (n=0; n<l; n++) printf("%02X",vctx[n]);
	printf("\n");
	
	printf("XOR dcr: %s\n",vptx);
	
	printf("MOD    : ");
	
	for (n=0; n<l; n++) printf("%02X",cctx[n]);
	printf("\n");
	
	printf("MOD dcr: %s\n",cptx);
	return 0;
}


C++: #include <iomanip>
#include <iostream> 
#include <sstream>
using namespace std;

enum CipherMode {ENCRYPT, DECRYPT};


uint32_t randRsl[256];
uint32_t randCnt;


uint32_t mm[256]; 
uint32_t aa = 0, bb = 0, cc = 0;

void isaac()
{   
    ++cc;  
    bb += cc; 
   
    for (uint32_t i = 0; i < 256; ++i)
    {
        uint32_t x, y;

        x = mm[i];
        switch (i % 4)
        {
            case 0: 
                aa = aa ^ (aa << 13); 
                break;
            case 1:
                aa = aa ^ (aa >> 6); 
                break;
            case 2:
                aa = aa ^ (aa << 2);
                break;
            case 3:
                aa = aa ^ (aa >> 16);
                break;
        }
        aa = mm[(i + 128) % 256] + aa;
        y = mm[(x >> 2) % 256] + aa + bb;
        mm[i] = y;
        bb = mm[(y >> 10) % 256] + x;
        randRsl[i] = bb; 
    }
    randCnt = 0; 
}

void mix(uint32_t a[])
{  
    a[0] = a[0] ^ a[1] << 11; a[3] += a[0]; a[1] += a[2];
    a[1] = a[1] ^ a[2] >>  2; a[4] += a[1]; a[2] += a[3];
    a[2] = a[2] ^ a[3] <<  8; a[5] += a[2]; a[3] += a[4];
    a[3] = a[3] ^ a[4] >> 16; a[6] += a[3]; a[4] += a[5];
    a[4] = a[4] ^ a[5] << 10; a[7] += a[4]; a[5] += a[6];
    a[5] = a[5] ^ a[6] >>  4; a[0] += a[5]; a[6] += a[7];
    a[6] = a[6] ^ a[7] <<  8; a[1] += a[6]; a[7] += a[0];
    a[7] = a[7] ^ a[0] >>  9; a[2] += a[7]; a[0] += a[1];  
}

void randInit(bool flag)
{
    uint32_t a[8];
    aa = bb = cc = 0;
   
    a[0] = 2654435769UL; 
    for (uint32_t j = 1; j < 8; ++j)
        a[j] = a[0];
   
    for (uint32_t i = 0; i < 4; ++i) 
        mix(a);
    for (uint32_t i = 0; i < 256; i += 8) 
    {
        if (flag) 
            for (uint32_t j = 0; j < 8; ++j)
                a[j] += randRsl[i + j];
        mix(a);
        for (uint32_t j = 0; j < 8; ++j)
            mm[i + j] = a[j];
    }
   
    if (flag)
    {   
        for (uint32_t i = 0; i < 256; i += 8)
        {
            for (uint32_t j = 0; j < 8; ++j)
                a[j] += mm[i + j];
            mix(a);
            for (uint32_t j = 0; j < 8; ++j)
                mm[i + j] = a[j];
        }
    }   
    isaac(); 
    randCnt = 0; 
}



void seedIsaac(string seed, bool flag)
{
    uint32_t seedLength = seed.length();
    for (uint32_t i = 0; i < 256; i++)
        mm[i] = 0;
    for (uint32_t i = 0; i < 256; i++)
        
        randRsl[i] = i > seedLength ? 0 : seed[i];
    
    randInit(flag);
}


uint32_t getRandom32Bit()
{
    uint32_t result = randRsl[randCnt];
    ++randCnt;
    if (randCnt > 255)
    {
        isaac();
        randCnt = 0;
    }
    return result;
}


char getRandomChar()
{       
    return getRandom32Bit() % 95 + 32;
}


string ascii2hex(string source)
{
    uint32_t sourceLength = source.length();
    stringstream ss;
    for (uint32_t i = 0; i < sourceLength; i++) 
        ss << setfill ('0') << setw(2) << hex << (int) source[i];
    return ss.str(); 
}


string vernam(string msg)
{
    uint32_t msgLength = msg.length();
    string destination = msg;
    for (uint32_t i = 0; i < msgLength; i++) 
        destination[i] = getRandomChar() ^ msg[i];
    return destination;    
}
        

char caesar(CipherMode m, char ch, char shift, char modulo, char start)
{
    int n;
    if (m == DECRYPT)
        shift = -shift;
    n = (ch - start) + shift;
    n %= modulo;
    if (n < 0) 
        n += modulo;
    return start + n;
}
        

string vigenere(string msg, CipherMode m)
{
    uint32_t msgLength = msg.length();
    string destination = msg;
    
    for (uint32_t i = 0; i < msgLength; ++i) 
        destination[i] = caesar(m, msg[i], getRandomChar(), 95, ' ');
    return destination; 
}
        
int main()
{
    
    string msg = "a Top Secret secret";
    string key = "this is my secret key";
    string xorCipherText, modCipherText, xorPlainText, modPlainText;
    
    
    seedIsaac(key, true);
    
    
    xorCipherText = vernam(msg);
    
    modCipherText = vigenere(msg, ENCRYPT);
    
    seedIsaac(key, true);
    
    xorPlainText = vernam(xorCipherText);
    
    modPlainText = vigenere(modCipherText, DECRYPT);
    
    cout << "Message: " << msg << endl;
    cout << "Key    : " << key << endl;
    cout << "XOR    : " << ascii2hex(xorCipherText) << endl;
    cout << "MOD    : " << ascii2hex(modCipherText) << endl;
    cout << "XOR dcr: " << xorPlainText << endl;
    cout << "MOD dcr: " << modPlainText << endl;
}