|
This code is not optimal (cuz of int/uint), but it works:
bool Crypt(string input, string key, out string output)
{
char[] cha = input.ToCharArray();
char[] sbox;
char[] sbox2;
sbox = new char[257];
sbox2 = new char[257];
UInt32 i, j, t, x;
i = j = t = x = 0;
char temp, k;
temp = '\0';
k = '\0';
for (int ii = 0; ii < 257; ++ii)
{
sbox[ii] = '\0';
sbox2[ii] = '\0';
}
for (i = 0; i < 256; ++i)
{
sbox[i] = Convert.ToChar(i);
}
j = 0;
for (i = 0; i < 256; ++i)
{
if (j == key.Length)
j = 0;
sbox2[i] = key[Convert.ToInt32(j)];
j++;
}
j = 0;
for (i = 0; i < 256; ++i)
{
j = (j + sbox[i] + sbox2[i]) % 256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
}
i = j = 0;
for (x = 0; x < input.Length; ++x)
{
i = (i + 1) % 256;
j = (j + sbox[i]) % 256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
t = Convert.ToUInt32((sbox[i] + sbox[j]) % 256);
k = sbox[t];
cha[Convert.ToInt32(x)] = Convert.ToChar(input[Convert.ToInt32(x)] ^ k);
}
output = new string(cha);
return true;
}
|
|
|
|
|
hi i have generated public key and private key using elliptical curve cryptography in c#. but by using those keys i need to encrypt data and need to decrypt it. any body knows encryption and decryption process by using elliptical curve cryptography keys. please give me an idea or c# code and i need to submit my university project on 3rd october 2011. so its bit urgent. please help me..
|
|
|
|
|
hi
i need an implementation of RSA algorithm performing encryption/decryption in C/C++. Please send it to my email :anuradha13@gmail.com if anybody can.
thanks a lot
Anuradha
|
|
|
|
|
is RSA the best choice for encryption . can
shaheena
|
|
|
|
|
HI all........
Anybody is having the source code for Elliptical curve cryptography program in vc++?
|
|
|
|
|
Hi!
Im looking for implementation of DSS digital signature algorithm in c/c++ without using any gnu or math library.I'll appreciate if anyone can help me with that.Thanx.
|
|
|
|
|
Dear All!
i need an implementation of RSA algorithm performing encryption/decryption in C/C++(without using any gnu math precision library).i would be your all time friend.!Take care.Please send it to my email :syedmfaisal@hotmail.com
~Faisal
|
|
|
|
|
This code is crippled with a serious flaw. XORing is a nilpotent operation, which means anything XORed with itself is 0. Unfortunately, nulls terminate strings in C, and can thus truncate anything encrypted with this algorithm.
To prove this to yourself, try encrypting "George Bush is a Texan" and you will get "George" on both input and output -- everything else is lost after the null in position 6.
The fix is simple. Just don't use strings for encryption or decryption. Confine yourself to buffers and buffer-related functions (memcpy, etc) and everything will be fine. Nasty problems like this are exactly why cryptographic code needs to be peer-reviewed.
|
|
|
|
|
It seems that the code sometimes will encrypt/decrypt correctly, and sometimes not. I've been fiddeling around with this example a lot, but can still not make it consequent in it's behavior.
This can sure be related to the usage of strings.
Here below is my testing sample. It will encrypt the string "ABCDEFG..." below and the write it to a binary file. Then it reads the file back and decrypts it and displays the the decrypted string in the console window.
Problem: If you just delete the character "A" in the string(and recompile), the string will get corrupted somehow and not displayed in its fully length.
I'm a newbie so I would apriciate all help there is, to make this code to work properly. Any clue on a workaround/inprovement?
-----------------------------------------------------------
#include <iostream.h>
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <fstream.h>
void Crypt(TCHAR *inp, DWORD inplen, TCHAR* key = "", DWORD keylen = 0)
{
//we will consider size of sbox 256 bytes
//(extra byte are only to prevent any mishep just in case)
TCHAR Sbox[257], Sbox2[257];
unsigned long i, j, t, x;
//this unsecured key is to be used only when there is no input key from user
static const TCHAR OurUnSecuredKey[] = "www.systweak.com" ;
unsigned static const int OurKeyLen = _tcslen( OurUnSecuredKey );
TCHAR temp , k = 0;
i = j = t = x = 0;
temp = 0;
//always initialize the arrays with zero
ZeroMemory(Sbox, sizeof(Sbox));
ZeroMemory(Sbox2, sizeof(Sbox2));
//initialize sbox i
for(i = 0; i < 256U; i++)
{
Sbox[i] = (TCHAR)i;
}
j = 0;
//whether user has sent any inpur key
if(keylen)
{
//initialize the sbox2 with user key
for(i = 0; i < 256U ; i++)
{
if(j == keylen)
{
j = 0;
}
Sbox2[i] = key[j++];
}
}
else
{
//initialize the sbox2 with our key
for(i = 0; i < 256U ; i++)
{
if(j == OurKeyLen)
{
j = 0;
}
Sbox2[i] = OurUnSecuredKey[j++];
}
}
j = 0 ; //Initialize j
//scramble sbox1 with sbox2
for(i = 0; i < 256; i++)
{
j = (j + (unsigned long) Sbox[i] + (unsigned long) Sbox2[i]) % 256U ;
temp = Sbox[i];
Sbox[i] = Sbox[j];
Sbox[j] = temp;
}
i = j = 0;
for(x = 0; x < inplen; x++)
{
//increment i
i = (i + 1U) % 256U;
//increment j
j = (j + (unsigned long) Sbox[i]) % 256U;
//Scramble SBox #1 further so encryption routine will
//will repeat itself at great interval
temp = Sbox[i];
Sbox[i] = Sbox[j] ;
Sbox[j] = temp;
//Get ready to create pseudo random byte for encryption key
t = ((unsigned long) Sbox[i] + (unsigned long) Sbox[j]) % 256U ;
//get the random byte
k = Sbox[t];
//xor with the data and done
inp[x] = (inp[x] ^ k);
}
// return 0;
}
int main()
{
TCHAR text[500];
// To encrypt a text
TCHAR HelloCrypt[] = "ABCDEFGHIJKLMNOPQRSTUVWXY123456789016063c01f1c77d601cec1ee16";
// We do not enter the key so we will be using default key provide by the crypt routine
Crypt(HelloCrypt, _tcslen(HelloCrypt));
remove( "example.bin" ); // If file already exists, delete it
ofstream a_file( "example.bin" ); // Open/Create file for writing
int setmode( int nMode = filebuf::binary ); // Set filemode to binary
a_file<<HelloCrypt;
a_file.close(); // Close file
ifstream b_file( "example.bin" ); // Open file for reading
// int setmode( int nMode = filebuf::binary );
b_file>>text; // Store filecontents in string "text"
// cout<<HelloCrypt<<"\n";
// Now to decrypt
Crypt(text, _tcslen(text)); // Decrypt the contents
cout<<text<<"\n"; // Print contents on screen
////////////////////////////////////////////////////////////////////////////////
// If we have to supply a key we have to use crypt as follows:
// TCHAR Data[] = "abcdefghijklmnopqrstuvwxyz"
// TCHAR Key[] = "Strong key";
// Encrypt:
// Crypt(Data, _tcslen(Data), Key, _tcslen(Key));
// Decrypt
// Crypt(Data, _tcslen(Data), Key, _tcslen(Key));
/////////////////////////////////////////////////////////////////////////////
return 0;
}
|
|
|
|
|
The problem is when loading and saving.
Here is a perfect(!) working version:
#include <iostream.h>
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <fstream.h>
static FILE *File;
unsigned static int A;
// Crypt Function /////////////////////////////////////////////////////////////////////////////////////////////////
void Crypt( TCHAR *inp, DWORD inplen, TCHAR* key = "", DWORD keylen = 0 )
{
//we will consider size of sbox 256 bytes
//(extra byte are only to prevent any mishep just in case)
TCHAR Sbox[257], Sbox2[257];
unsigned long i, j, t, x;
//this unsecured key is to be used only when there is no input key from user
static const TCHAR OurUnSecuredKey[] = "www.systweak.com" ;
unsigned static const int OurKeyLen = _tcslen( OurUnSecuredKey );
TCHAR temp , k = 0;
i = j = t = x = 0;
temp = 0;
//always initialize the arrays with zero
ZeroMemory( Sbox, sizeof( Sbox ) );
ZeroMemory( Sbox2, sizeof( Sbox2 ) );
//initialize sbox i
for( i = 0; i < 256U; i++ )
{
Sbox[i] = ( TCHAR )i;
}
j = 0;
//whether user has sent any inpur key
if( keylen )
{
//initialize the sbox2 with user key
for( i = 0; i < 256U ; i++ )
{
if( j == keylen )
{
j = 0;
}
Sbox2[i] = key[j++];
}
}
else
{
//initialize the sbox2 with our key
for( i = 0; i < 256U ; i++ )
{
if( j == OurKeyLen )
{
j = 0;
}
Sbox2[i] = OurUnSecuredKey[j++];
}
}
j = 0 ; //Initialize j
//scramble sbox1 with sbox2
for( i = 0; i < 256; i++ )
{
j = ( j + ( unsigned long ) Sbox[i] + ( unsigned long ) Sbox2[i] ) % 256U ;
temp = Sbox[i];
Sbox[i] = Sbox[j];
Sbox[j] = temp;
}
i = j = 0;
for( x = 0; x < inplen; x++ )
{
//increment i
i = (i + 1U) % 256U;
//increment j
j = (j + ( unsigned long ) Sbox[i] ) % 256U;
//Scramble SBox #1 further so encryption routine will
//will repeat itself at great interval
temp = Sbox[i];
Sbox[i] = Sbox[j] ;
Sbox[j] = temp;
//Get ready to create pseudo random byte for encryption key
t = ( ( unsigned long ) Sbox[i] + ( unsigned long ) Sbox[j] ) % 256U ;
//get the random byte
k = Sbox[t];
//xor with the data and done
inp[x] = ( inp[x] ^ k );
}
}
// Actual Program /////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
// To encrypt a text
TCHAR HelloCrypt[] = "ABCDEFGHIJKLMNOPQRSTUVWXY1234567890";
// We do not enter the key so we will be using default key provide by the crypt routine
Crypt(HelloCrypt, _tcslen(HelloCrypt));
// Saving encrypted string to file //////////////
if( NULL!=( File=fopen( "example.bin" , "wb") ) ) // Check to see if the existing file is write protected
{
File=fopen( "example.bin" , "wb" ); // Save binary contents of HelloCrypt
for( A = 0; A < _tcslen( HelloCrypt ); A++ )
{
fseek( File, A, SEEK_SET );
fwrite( &HelloCrypt[A], 1, 1, File );
}
printf( "Information saved! SUCCESS...\n" );
fclose( File );
printf( "%s\n\n", HelloCrypt ); // Print contents on screen
}
else printf( "Unable to create file!\n\n" );
// Reading crypted file and decrypt content /////
if( NULL!=( File=fopen( "example.bin" , "r") ) ) // Check to see if the file exists
{
File=fopen( "example.bin" , "r" ); // Read binary contents from file to HelloCrypt
for( A = 0; A < _tcslen( HelloCrypt ); A++ )
{
fseek( File, A, SEEK_SET );
fread( &HelloCrypt[A], 1, 1, File );
}
printf( "Information read! SUCCESS...\n" );
fclose( File );
Crypt( HelloCrypt, _tcslen( HelloCrypt ) ); // Decrypt content in file
printf( "%s\n\n", HelloCrypt ); // Print contents on screen
}
else printf( "File Access Failure!\n\n" );
////////////////////////////////////////////////////////////////////////////////
// If we have to supply a key we have to use crypt as follows:
// TCHAR Data[] = "abcdefghijklmnopqrstuvwxyz"
// TCHAR Key[] = "Strong key";
// Encrypt:
// Crypt(Data, _tcslen(Data), Key, _tcslen(Key));
// Decrypt
// Crypt(Data, _tcslen(Data), Key, _tcslen(Key));
/////////////////////////////////////////////////////////////////////////////
return 0;
}
|
|
|
|
|
Hello Peter,
Thank you very much for the corrected code !!!
Best regards
Fiffi
|
|
|
|
|
Cheers!
Over one year ago now... I've totally forgot about this posting.
Regards, Peter
|
|
|
|
|
The final part of the algorithm does an XOR that could give out \x0000 (Unicode NULL) for certain combination of characters, hence the string length of the encrypted value gets shorted and reverse process doesn't work at times. (Due to _tcslen use)
However I have over come this by tweaking the algorithm a little bit so that never a character gets encrypted to \x0000. (if final encrypted char is null then insert the original char, reverse process also works for this) I have tested this. This may weaken the encryption strength but you have a encrypted key that could be decrypted to the correct value.
Anonymous
|
|
|
|
|
Can you share your code snippet with us that shows the changes you made?
|
|
|
|
|
Here it is, the part I changed:
//xor with the data and done
//inp[x] = ( inp[x] ^ k );
if ((inp[x] ^ k) == _T('\x0000'))
{inp[x] = inp[x] ;}
else
{inp[x] = (inp[x] ^ k);}
////// XOR is done only if the result is not a NULL \\\\\\\
Let me know if you can make it better, or find another way of doing it.
Shankar
|
|
|
|
|
Thanks a lot. This solves the problem.
|
|
|
|
|
Looking at the code, it doesn't look like there is anything random about it, but in the message it says that "...encrypt we always pick a random byte from the key and encrypt the text with that..."
Wouldn't that make it impossible to come back to the source string, unless you pick the same random key?
Looking over hte code, it looks like it doesn't do that, but I still notice different encryptions for the same string, could anybody enlighten me?
|
|
|
|
|
Note that he is talking about 'psuedo-random' byte . It is always psuedo-random and not really random.
With my tests on this code it always produces the same encryption for the same string for the same key.
Anand
|
|
|
|
|
newbie here, beware
thanks for the code, how can i convert from a CString?
tried
CString tm;
tm = "Test String";
LPTSTR lpsz = new TCHAR[tm.GetLength()+1];
_tcscpy(lpsz, tm);
TCHAR HelloCrypt[] = lpsz;
i get, "cannot convert char * to char []".
i've tried everything i can think of, any helps really appreciated
shotgun
|
|
|
|
|
I've been looking for some code to deal specifically with strings, something a little above basic XOR, the only purpose to provide fast and small routines ( not particulary strong ) to hinder reverse engineering. All attempts by me so far have failed... they have all had bugs in them, don't suppose anyone has any ideas....
|
|
|
|
|
Change the following code from this ......
//Encrypt
Crypt(Data, _tcslen(Data), Key, _tcslen(Data));
//Decrypt
Crypt(Data, _tcslen(Data), Key, _tcslen(Data));
to this ......
//Encrypt
Crypt(Data, _tcslen(Data), Key, _tcslen(Key));
//Decrypt
Crypt(Data, _tcslen(Data), Key, _tcslen(Key));
|
|
|
|
|
The RC4 algorithm was designed in RSA Labratories by Ron Rivest in 1987. Rodney Thayer and Kalle Kaukonen documented it (read: reverse engineered it) and published it as an Internet Draft (http://www3.pkiclue.com/publications/draft-kaukonen-cipher-arcfour-01.txt); however, I'm not sure they were the first to do it.
It is important to make clear that RC4 already has several known weaknesses, and should therefore not be used today; this includes the implementation presented in this article. There are several better alternatives available, such as the new encryption standard AES (Rijndael), Twofish or Blowfish. Always pick an algorithm that doesn't have known weaknesses (that can't be avoided), and one that has undergone intensive peer review. And if security is of great importance, consult an expert.
|
|
|
|
|
Ok so where can we find such an implementation?
cheers,
swine
Check out Aephid Photokeeper, the powerful digital
photo album solution at www.aephid.com.
|
|
|
|
|
If you would like to check out a Library for this type of stuff, check this site out Crypto++ 4.2 or you can go to a page in CodeProject that I wrote using this library called Cryptest.
Regards,
Dan
|
|
|
|
|
Crypto++, as Dan pointed out, is an excellent choice if you're programming in C++. Should you be working in C, there are many alternative implementations to be found here: ftp://ftp.hacktic.nl/pub/crypto/libraries/
|
|
|
|
|