免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7081 | 回复: 2
打印 上一主题 下一主题

[算法] AES C 代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-20 10:18 |只看该作者 |倒序浏览
从网上cp过来的

  1. #ifndef _AES_H
  2. #define _AES_H



  3. /**
  4. * \brief          AES context structure
  5. */
  6. typedef struct
  7. {
  8.     unsigned long erk[64];     /*!< encryption round keys */
  9.     unsigned long drk[64];     /*!< decryption round keys */
  10.     int nr;                    /*!< number of rounds      */
  11. }
  12. aes_context;

  13. /**
  14. * \brief          AES key schedule
  15. *
  16. * \param ctx      AES context to be initialized
  17. * \param key      the secret key
  18. * \param keysize  must be 128, 192 or 256
  19. */
  20. void aes_set_key( aes_context *ctx, unsigned char *key, int keysize );

  21. /**
  22. * \brief          AES block encryption (ECB mode)
  23. *
  24. * \param ctx      AES context
  25. * \param input    plaintext  block
  26. * \param output   ciphertext block
  27. */
  28. void aes_encrypt( aes_context *ctx,
  29.                   unsigned char input[16],
  30.                   unsigned char output[16] );

  31. /**
  32. * \brief          AES block decryption (ECB mode)
  33. *
  34. * \param ctx      AES context
  35. * \param input    ciphertext block
  36. * \param output   plaintext  block
  37. */
  38. void aes_decrypt( aes_context *ctx,
  39.                   unsigned char input[16],
  40.                   unsigned char output[16] );

  41. /**
  42. * \brief          AES-CBC buffer encryption
  43. *
  44. * \param ctx      AES context
  45. * \param iv       initialization vector (modified after use)
  46. * \param input    buffer holding the plaintext
  47. * \param output   buffer holding the ciphertext
  48. * \param len      length of the data to be encrypted
  49. */
  50. void aes_cbc_encrypt( aes_context *ctx,
  51.                       unsigned char iv[16],
  52.                       unsigned char *input,
  53.                       unsigned char *output,
  54.                       int len );

  55. /**
  56. * \brief          AES-CBC buffer decryption
  57. *
  58. * \param ctx      AES context
  59. * \param iv       initialization vector (modified after use)
  60. * \param input    buffer holding the ciphertext
  61. * \param output   buffer holding the plaintext
  62. * \param len      length of the data to be decrypted
  63. */
  64. void aes_cbc_decrypt( aes_context *ctx,
  65.                       unsigned char iv[16],
  66.                       unsigned char *input,
  67.                       unsigned char *output,
  68.                       int len );


  69. //static const char _aes_src[] = "_aes_src";



  70. /*
  71. * AES-ECB test vectors (source: NIST, rijndael-vals.zip)
  72. */



  73. #endif /* aes.h */
复制代码

以上是头文件,源文件由于太长就不贴出来了,请见:http://www.google.com/codesearch ... p;q=lang:c%20base64

[ 本帖最后由 diyself 于 2009-10-20 10:34 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-10-20 10:20 |只看该作者
测试程序

  1. int aes_self_test(  )
  2. {
  3.     int i, j, u, v;
  4.     aes_context ctx;
  5.     unsigned char buf[16],temp[16];
  6.        
  7.     u = 1 >> 10;

  8.     strcpy(buf,"test");
  9.     //aes_set_key( &ctx, buf, 128 + u * 64 );
  10.         aes_set_key( &ctx, buf, 128);
  11.         //unsigned char iv[16];
  12.         i=0;
  13.         //memset(iv, 0, sizeof(iv));
  14.         //aes_cbc_encrypt(&ctx, iv,buf, temp,strlen(buf));
  15.     aes_encrypt( &ctx, buf, temp );
  16.     strcpy(buf,"");
  17.         //aes_cbc_decrypt(&ctx, iv,temp, buf,strlen(temp));
  18.     aes_decrypt( &ctx, temp, buf );
  19.         printf("\n%s\n",buf);

  20.     return( 0 );
  21. }
复制代码


结果打印出test,解码成功

[ 本帖最后由 diyself 于 2009-10-20 10:23 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-10-20 10:24 |只看该作者
问题:用cbc,下面的代码解码出现乱码,请高手帮忙指正,谢谢

  1. int aes_self_test(  )
  2. {
  3.     int i, j, u, v;
  4.     aes_context ctx;
  5.     unsigned char buf[16],temp[16];
  6.        
  7.     u = 1 >> 10;

  8.     strcpy(buf,"test");
  9.     //aes_set_key( &ctx, buf, 128 + u * 64 );
  10.         aes_set_key( &ctx, buf, 128);
  11.         unsigned char iv[16];
  12.         i=0;
  13.         memset(iv, 0, sizeof(iv));
  14.         aes_cbc_encrypt(&ctx, iv,buf, temp,strlen(buf));
  15.     //aes_encrypt( &ctx, buf, temp );
  16.     strcpy(buf,"");
  17.         aes_cbc_decrypt(&ctx, iv,temp, buf,strlen(temp));
  18.     //aes_decrypt( &ctx, temp, buf );
  19.         printf("\n%s\n",buf);

  20.     return( 0 );
  21. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP