[Botan-devel] Stream cipher operations

Jack Lloyd lloyd at randombit.net
Wed Oct 12 17:28:23 EDT 2005

Stream ciphers are stateful, and for most stream ciphers, encryption and
decryption are the same operation. So you actually didn't encrypt and decrypt
the message in the code below, you encrypted it twice.

While those semantics are somewhat confusing, the alternative would be that
each stream cipher object has two full states, one for encryption and one for
decryption, and the object uses different states depending on if it is
encrypting or decrypting. And in fact in this case that's how you would do it;
create two stream cipher objects, key them both, and use one to encrypt and one
to decrypt. As far as I can tell, the case where a single process needs to both
encrypt and decrypt with a stream cipher using the same key in each direction
is pretty rare, which is why it is not supported directly.


On Wed, Oct 12, 2005 at 05:20:34PM -0400, Carlton Davis wrote:
> Thanks for your feedback on my previous messages.
> I did some testing on stream cipher operations, but
> it seems as if I am doing something wrong. Here is the code:
>   std::string passphrase = "testing"
>   LibraryInitializer init;
>   //Just testing here: a quick insecure way of getting a 128-bit key
>   HashFunction* hash = get_hash("MD5");
>   SymmetricKey skey = hash->process(passphrase)
>   StreamCipher* cipher = get_stream_cipher("WiderWake4+1-BE");
>   cipher->set_key(skey);
>   unsigned short flag = 4;
>   for (i = 0; i != sizeof(flag); i++)
>     buf[i] = get_byte(i, flag);
>   fprintf(stderr, "\nThe byte values are %x and %x\n",
>           buf[0], buf[1]);
>   cipher->encrypt(buf, 2);
>   cipher->decrypt(buf, 2);
>   fprintf(stderr, "\nAfter decryption, the byte values are %x and %x\n",
> 	  buf[0], buf[1]);
>   unsigned short decryptflag = make_u16bit(buf[0], buf[1]);
>   fprintf(stderr, "\nThe decrypted number is: %i\n", decryptflag);
> --
> I was expecting to get the same byte values after the
> encryption/decryption operations, but here is the output:
> -----
> The byte values are 0 and 4
> After decryption, the byte values are 6a and 46
> The decrypted number is: 27206
> -----
> What am I doing wrong?
> Thanks and best regards,
> -Carlton
> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://www.randombit.net/mailman/listinfo/botan-devel

More information about the botan-devel mailing list