libtomcrypt 1.06 RC4例子


#include "stdafx.h"
#include <tomcrypt.h>
#include "RSACrypto.h"
int main(int argc, char* argv[])
{

 prng_state prng;
 unsigned char buf[32];
 int err;
 //加密
 if ((err = rc4_start(&prng)) != CRYPT_OK) {
  printf("RC4 init error: %s\n", error_to_string(err));
  exit(-1);
 }
 /* use "key" as the key */
 if ((err = rc4_add_entropy((unsigned char*)"key", 3, &prng)) != CRYPT_OK) {
  printf("RC4 add entropy error: %s\n", error_to_string(err));
  exit(-1);
 }
 /* setup RC4 for use */
 if ((err = rc4_ready(&prng)) != CRYPT_OK) {
  printf("RC4 ready error: %s\n", error_to_string(err));
  exit(-1);
 }
 /* encrypt buffer */
 char *strmsg2="123456";
 strcpy((char*)buf,strmsg2);
 unsigned long outlen=strlen(strmsg2);
 if (rc4_read(buf, outlen, &prng) != outlen) {
  printf("RC4 read error\n");
  exit(-1);
 }

 printf("%s\n\n",buf);
 rc4_done(&prng);

 //解密
 if ((err = rc4_start(&prng)) != CRYPT_OK) {
  printf("RC4 init error: %s\n", error_to_string(err));
  exit(-1);
 }
 /* use "key" as the key */
 if ((err = rc4_add_entropy((unsigned char*)"key", 3, &prng)) != CRYPT_OK) {
  printf("RC4 add entropy error: %s\n", error_to_string(err));
  exit(-1);
 }
 /* setup RC4 for use */
 if ((err = rc4_ready(&prng)) != CRYPT_OK) {
  printf("RC4 ready error: %s\n", error_to_string(err));
  exit(-1);
 }
 if (rc4_read(buf, outlen, &prng) != outlen) {
  printf("RC4 read error\n");
  exit(-1);
 }
 printf("%s",buf);

return 0;

}

posted @ 2008-04-30 10:42  ahuo  阅读(821)  评论(0编辑  收藏  举报