Re: Looking for a simple blowfish example



On May 15, 7:42 am, d-fan <rafel.co...@xxxxxxxxxxxxxx> wrote:
On May 15, 3:37 am, Randy Thompson <RThomp...@xxxxxxxxxxxxx> wrote:





d-fan wrote:
On May 14, 3:44 am, Randy Thompson <RThomp...@xxxxxxxxxxxxx> wrote:
d-fan wrote:
On May 9, 8:02 am, Randy Thompson <RThomp...@xxxxxxxxxxxxx> wrote:
d-fan wrote:
Can someone point me to a simple example of "C" programming using
Blowfishto encrypt and decrypt?  I am a newbie to the world of
encryption and I could really use an example if how to encrypt a
string of text consisting of approximately 16 characters and
decrypting them usingblowfish.  I am using a Linux platform as my
development platform.
There is a link from here:http://www.schneier.com/blowfish.html
called "Free source code available". The one by Paul Kocher is fairly
easy to understand.
You may also find something from here that you like:ftp://ftp.zedz.net/pub/crypto/libraries/blowfish/
I did read and use the sample program by Paul Kocher.  I am trying to
understant the parameters that are passed.  I can see how the key is
setup, but i can't understant the left and right concept.  I simply
want to encrypt something like "HAPPY Dog"  and be able to decrypt
it.  How does a word or phrase like this get passed a a parameter to
the encrypt and decrypt routine.
Below is the blowfish_test.c file rewritten with "HAPPYDog" as the
plaintext.
I excluded the space to make it 8 bytes long, or 1 block inBlowfish.
"HAPPYDog"  in ASCII hexadecimal is "4841505059446F67"
so L will be 0x48415050L, and R will be 0x59446F67L.

(Note: You may want to reverse those values to conform to the
little-endian format.
  i.e. L = 0x50504148L  and  R =  0x676F4459L.  And also reverse the
output from the
  Blowfish_Decrypt function. But that's not necessary here.)

/*
blowfish_test.c:  Test file forblowfish.c

Copyright (C) 1997 by Paul Kocher

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include "blowfish.h"

int main(void) {
   unsigned long L = 0x48415050L, R = 0x59446F67L;  /*  HAPPYDog  */
   BLOWFISH_CTX ctx;

   Blowfish_Init (&ctx, (unsigned char*)"TESTKEY", 7);
   Blowfish_Encrypt(&ctx, &L, &R);
   printf("%08lX %08lX\n", L, R);
   if (L == 0xF570C2B1L && R == 0xB8A3A68BL)
          printf("Test encryption OK.\n");
   else
          printf("Test encryption failed.\n");
   Blowfish_Decrypt(&ctx, &L, &R);
   printf("%08lX %08lX\n", L, R);
   if (L == 0x48415050L && R == 0x59446F67L)
          printf("Test decryption OK.\n");
   else
          printf("Test decryption failed.\n");
return 0;

}

HTH,
Randy- Hide quoted text -

- Show quoted text -

Thanks so much for your informative reply.  I do have a few remaining
questions.  What happend is the text was shorter or longet such as
"HAPPYd" or "HAPPYDogandCat"?  

Usually if there are fewer bytes than the block size, you would add extra
bytes to make it equal to the size of the block. For instance, "HAPPYd  ".
I just added two extra spaces to the end.

For "HAPPYDogandCat", you end up with 2 blocks. The first one, "HAPPYDog",
is 8 bytes, and the second one, "andCat  ",  has 2 spaces add to the end
to make
it also 8 bytes long.

Most of the time, a zero byte (0x00) is used as padding to fill up
blocks. I used a
space here for convenience.

How interchangeble are the various versions ofblowfish.  

As far as I know, they all should be interchangeable.

Can I send my encrypted data to a windows box and decrypt it?  

You most certainly can. I'm not sure how you would do that in Linux.

What is the best way to convert text to Hex in "C"?

There are several conversion routines in C to be found on the Internet.
This is where you get to learn to use almighty Google.
Or, you can write your own. You're not restricted to using what's already
written.

Best of luck,
Randy- Hide quoted text -

- Show quoted text -

Thanks for your help.  I wish that the example would have demonstrated
a string or character text instead of an integer expecially a 1 and a
2.  It make it seem like the 2 is a size reference instead of just the
right half of the number.

Trying to find conversion routines in Google is worse that a needle in
a haystack. There is usually a finite amount of hay.- Hide quoted text -

- Show quoted text
I wish that the original example had been creating using char arrays
instead of integers. That would have been much more valuable.

.



Relevant Pages

  • RE: Using Win32 CryptDecrypt to Decrypt RijndaelManaged
    ... I figured out how to use RijndaelManaged with AES in the C++ app. ... C++ crypto WILL successfully decrypt the .NET generated ... I am trying to write a Win32 app that can decrypt that string using the ... I can get both to encrypt and decrypt successfully in their own projects, ...
    (microsoft.public.platformsdk.security)
  • Re: Encrypt and Decrypt a file using .NET 2.0?
    ... public static string GenerateKey() ... DES des = DES.Create; ... // Distribute this key to the user who will decrypt this file. ... // Get the Key for the file to Encrypt. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: .NET Crypto Classes Interoperability with Win32 Crypto APIs
    ... >the hash of the string you are feeding in. ... when i encrypt a string using .NET classes and try ... >> decrypt it using Win32 APIs, ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt and Decrypt a file using .NET 2.0?
    ... public static string GenerateKey() ... using (CryptoStream cryptoStream = new CryptoStream(outFile, ... // Distribute this key to the user who will decrypt this file. ... // Get the Key for the file to Encrypt. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Decryptionfailed to bring original text back....
    ... cryptography, but now using them on web.config, any idea, I like to ... There isn't really much reason to encrypt a .NET string with ... what happen when you decrypt the encrypted ...
    (microsoft.public.dotnet.security)

Quantcast