This is my C++ implementation of the encryption algorithm that is used by the Cyberoam Protocol to encrypt passwords. Note that the code is released under the GNU General Public License. Also, there is a reasonable amount of guesswork that I went through before arriving at the final algorithm. I have left out the include files.
/*
Copyright (C) 2002 Rahul Mittal <admin@rasteroid.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
char* Encrypt(char* sPlaintext, int nLength) {
assert (nLength <= 8); // Max length of plaintext password
char* sTimestamp = new char[10];
char* sCiphertext = new char[10 + 3*nLength]; // holds ciphertext
int n1 = 0, n2 = 0;
// Generate time stamp
sprintf(sTimestamp, "%010d", time()); // get and store timestamp
strncpy(sCiphertext, sTimestamp, 10);
// Generate ciphertext
for (int i = 0; i < nLength; i++) {
char c = sPlaintext[i];
short nScramble = ScrambleTimestamp(sTimestamp, n1, n2);
for (int j = 0; j < 10; j++)
sTimestamp[j] ^= c;
int n = (nScramble & 0xFF) ^ c ^ (nScramble >> 8);
sprintf(sCiphertext + 10 + (i*3), "%03d", n);
}
return sCiphertext;
}
short ScrambleTimestamp(char* sTimestamp, int& nCurrent, int& nPrevious) {
// nCurrent == BP+0x10
int nScramble = 0;
int nIter = 0; // BP+0x1E
int i;
int nTemp1; // BP+0x04
int nTemp2 = 0; // BP+0x12
int nFinal = 0; // BP+0x20
while (nIter < 5) {
i = nIter * 2;
nScramble = nTemp2 ^ ((sTimestamp[i] << 8) | sTimestamp[i+1]);
nTemp1 = (nCurrent + nIter) * 0x4E35;
nCurrent = nPrevious + (nScramble * 0x015A) + nTemp1; // BP+0x0A
nPrevious = nScramble * 0x015A;
nTemp2 = nScramble * 0x4E35 + 1;
nScramble = nCurrent ^ nTemp2;
nFinal ^= nScramble;
++nIter;
}
return static_cast<short>(nFinal & 0xFFFF);
}