git地址:https://github.com/xxtea/xxtea-c.git

代码块:

xxtea.h

/**********************************************************\
|                                                          |
| xxtea.h                                                  |
|                                                          |
| XXTEA encryption algorithm library for C.                |
|                                                          |
| Encryption Algorithm Authors:                            |
|      David J. Wheeler                                    |
|      Roger M. Needham                                    |
|                                                          |
| Code Authors: Chen fei <cf850118@163.com>                |
|               Ma Bingyao <mabingyao@gmail.com>           |
| LastModified: Mar 3, 2015                                |
|                                                          |
\**********************************************************/#ifndef XXTEA_INCLUDED
#define XXTEA_INCLUDED#include <stdlib.h>#ifdef __cplusplus
extern "C" {
#endif/*** Function: xxtea_encrypt* @data:    Data to be encrypted* @len:     Length of the data to be encrypted* @key:     Symmetric key* @out_len: Pointer to output length variable* Returns:  Encrypted data or %NULL on failure** Caller is responsible for freeing the returned buffer.*/
void * xxtea_encrypt(const void * data, size_t len, const void * key, size_t * out_len);/*** Function: xxtea_decrypt* @data:    Data to be decrypted* @len:     Length of the data to be decrypted* @key:     Symmetric key* @out_len: Pointer to output length variable* Returns:  Decrypted data or %NULL on failure** Caller is responsible for freeing the returned buffer.*/
void * xxtea_decrypt(const void * data, size_t len, const void * key, size_t * out_len);#ifdef __cplusplus
}
#endif#endif

xxtea.c

/**********************************************************\
|                                                          |
| xxtea.c                                                  |
|                                                          |
| XXTEA encryption algorithm library for C.                |
|                                                          |
| Encryption Algorithm Authors:                            |
|      David J. Wheeler                                    |
|      Roger M. Needham                                    |
|                                                          |
| Code Authors: Chen fei <cf850118@163.com>                |
|               Ma Bingyao <mabingyao@gmail.com>           |
| LastModified: Feb 7, 2016                                |
|                                                          |
\**********************************************************/#include "xxtea.h"#include <string.h>
#if defined(_MSC_VER) && _MSC_VER < 1600
typedef unsigned __int8 uint8_t;
typedef unsigned __int32 uint32_t;
#else
#if defined(__FreeBSD__) && __FreeBSD__ < 5
/* FreeBSD 4 doesn't have stdint.h file */
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#endif#include <sys/types.h> /* This will likely define BYTE_ORDER */#ifndef BYTE_ORDER
#if (BSD >= 199103)
# include <machine/endian.h>
#else
#if defined(linux) || defined(__linux__)
# include <endian.h>
#else
#define LITTLE_ENDIAN   1234    /* least-significant byte first (vax, pc) */
#define BIG_ENDIAN  4321    /* most-significant byte first (IBM, net) */
#define PDP_ENDIAN  3412    /* LSB first in word, MSW first in long (pdp)*/#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \defined(vax) || defined(ns32000) || defined(sun386) || \defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \defined(__alpha__) || defined(__alpha)
#define BYTE_ORDER    LITTLE_ENDIAN
#endif#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\defined(apollo) || defined(__convex__) || defined(_CRAY) || \defined(__hppa) || defined(__hp9000) || \defined(__hp9000s300) || defined(__hp9000s700) || \defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
#define BYTE_ORDER  BIG_ENDIAN
#endif
#endif /* linux */
#endif /* BSD */
#endif /* BYTE_ORDER */#ifndef BYTE_ORDER
#ifdef __BYTE_ORDER
#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#endif
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
#define BYTE_ORDER LITTLE_ENDIAN
#else
#define BYTE_ORDER BIG_ENDIAN
#endif
#endif
#endif
#endif#define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))
#define DELTA 0x9e3779b9#define FIXED_KEY \size_t i;\uint8_t fixed_key[16];\memcpy(fixed_key, key, 16);\for (i = 0; (i < 16) && (fixed_key[i] != 0); ++i);\for (++i; i < 16; ++i) fixed_key[i] = 0;\static uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, int inc_len, size_t * out_len) {uint32_t *out;
#if !(defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN))size_t i;
#endifsize_t n;n = (((len & 3) == 0) ? (len >> 2) : ((len >> 2) + 1));if (inc_len) {out = (uint32_t *)calloc(n + 1, sizeof(uint32_t));if (!out) return NULL;out[n] = (uint32_t)len;*out_len = n + 1;}else {out = (uint32_t *)calloc(n, sizeof(uint32_t));if (!out) return NULL;*out_len = n;}
#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)memcpy(out, data, len);
#elsefor (i = 0; i < len; ++i) {out[i >> 2] |= (uint32_t)data[i] << ((i & 3) << 3);}
#endifreturn out;
}static uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, size_t * out_len) {uint8_t *out;
#if !(defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN))size_t i;
#endifsize_t m, n;n = len << 2;if (inc_len) {m = data[len - 1];n -= 4;if ((m < n - 3) || (m > n)) return NULL;n = m;}out = (uint8_t *)malloc(n + 1);#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)memcpy(out, data, n);
#elsefor (i = 0; i < n; ++i) {out[i] = (uint8_t)(data[i >> 2] >> ((i & 3) << 3));}
#endifout[n] = '\0';*out_len = n;return out;
}static uint32_t * xxtea_uint_encrypt(uint32_t * data, size_t len, uint32_t * key) {uint32_t n = (uint32_t)len - 1;uint32_t z = data[n], y, p, q = 6 + 52 / (n + 1), sum = 0, e;if (n < 1) return data;while (0 < q--) {sum += DELTA;e = sum >> 2 & 3;for (p = 0; p < n; p++) {y = data[p + 1];z = data[p] += MX;}y = data[0];z = data[n] += MX;}return data;
}static uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key) {uint32_t n = (uint32_t)len - 1;uint32_t z, y = data[0], p, q = 6 + 52 / (n + 1), sum = q * DELTA, e;if (n < 1) return data;while (sum != 0) {e = sum >> 2 & 3;for (p = n; p > 0; p--) {z = data[p - 1];y = data[p] -= MX;}z = data[n];y = data[0] -= MX;sum -= DELTA;}return data;
}static uint8_t * xxtea_ubyte_encrypt(const uint8_t * data, size_t len, const uint8_t * key, size_t * out_len) {uint8_t *out;uint32_t *data_array, *key_array;size_t data_len, key_len;if (!len) return NULL;data_array = xxtea_to_uint_array(data, len, 1, &data_len);if (!data_array) return NULL;key_array  = xxtea_to_uint_array(key, 16, 0, &key_len);if (!key_array) {free(data_array);return NULL;}out = xxtea_to_ubyte_array(xxtea_uint_encrypt(data_array, data_len, key_array), data_len, 0, out_len);free(data_array);free(key_array);return out;
}static uint8_t * xxtea_ubyte_decrypt(const uint8_t * data, size_t len, const uint8_t * key, size_t * out_len) {uint8_t *out;uint32_t *data_array, *key_array;size_t data_len, key_len;if (!len) return NULL;data_array = xxtea_to_uint_array(data, len, 0, &data_len);if (!data_array) return NULL;key_array  = xxtea_to_uint_array(key, 16, 0, &key_len);if (!key_array) {free(data_array);return NULL;}out = xxtea_to_ubyte_array(xxtea_uint_decrypt(data_array, data_len, key_array), data_len, 1, out_len);free(data_array);free(key_array);return out;
}// public functionsvoid * xxtea_encrypt(const void * data, size_t len, const void * key, size_t * out_len) {FIXED_KEYreturn xxtea_ubyte_encrypt((const uint8_t *)data, len, fixed_key, out_len);
}void * xxtea_decrypt(const void * data, size_t len, const void * key, size_t * out_len) {FIXED_KEYreturn xxtea_ubyte_decrypt((const uint8_t *)data, len, fixed_key, out_len);
}

示例代码如下,它只有2个接口,一个加密,一个解密。

#include <stdio.h>
#include <string.h>
#include <xxtea.h>int main()
{const char *text = "Hello World! 你好,中国!";const char *key = "1234567890";size_t len;unsigned char *encrypt_data = xxtea_encrypt(text, strlen(text), key, &len);char *decrypt_data = xxtea_decrypt(encrypt_data, len, key, &len);if (strncmp(text, decrypt_data, len) == 0) {printf("success!\n");}else {printf("fail!\n");}free(encrypt_data);free(decrypt_data);return 0;
}

推荐一款开源的加解密算法 --- XXTEA相关推荐

  1. 加解密算法的概述和总结

    加解密算法的概述和总结 一.单向散列算法 也称为Hash(哈希)算法.是一种将任意长度的消息压缩到某一固定长度(消息摘要)的函数(该过程不可逆).Hash函数可用于数字签名.消息的完整性检测.消息起源 ...

  2. 还没使用过Web Worker? 推荐一款开源工具Workerize-Loader,让你在webpack项目中轻松使用Web Worker

    还没使用过Web Worker? 推荐一款开源工具Workerize-Loader,让你在webpack项目中轻松使用Web Worker Workerize-Loader 将模块及其依赖项移动到 W ...

  3. php aes 256 加解密,PHP完整的AES加解密算法使用及例子(256位)

    依赖PHP自身的mcrypt扩展 class aes { // CRYPTO_CIPHER_BLOCK_SIZE 32 private $_secret_key = 'default_secret_k ...

  4. 2023年推荐几款开源或免费的web应用防火墙

    2023年推荐几款开源或免费的web应用防火墙 2023年,数字经济将强势崛起,并且成为新一轮经济发展的动力,传统的黑客破坏性攻击如CC,转为更隐蔽的如0day进行APT渗透.所以无论私有服务器还是云 ...

  5. 区块链之加解密算法数字证书

    目录 一.加解密算法 数字签名 对称加密 DES(Data EncryptionStandard) 3DES(Triple DES) AES(Advanced EncryptionStandard) ...

  6. .Net/C# 实现: FlashFXP 地址簿中站点密码的加解密算法

    参阅 CCFer & TLFer : kanbol 的 Java 代码翻译修改而成: kanbol 说: 之前在TLF写过一个程序自动更新flashfxp的地址簿,也就是修改sites.dat ...

  7. (转)base64编码(严格说来,base64不算作加解密算法)

    [README] 1.本文转自: Java base64加密解密 - xuwc - 博客园参考: https://www.cnblogs.com/luguo3000/p/3940197.html ht ...

  8. C语言实现DES加解密算法

    C语言实现DES加解密算法 DES加解密 DES加解密 #include <stdio.h> #include <stdlib.h> #include <string.h ...

  9. C语言实现AES加解密算法

    C语言实现AES加解密算法 AES加解密 AES加解密 #include <stdio.h> #include <stdint.h> #include <memory.h ...

  10. C语言实现TEA系列加解密算法

    C语言实现TEA系列加解密算法 TEA加解密 XTEA加解密 XXTEA加解密 TEA加解密 #include <stdio.h> #include <stdint.h>//加 ...

最新文章

  1. 如果可以,我想给这本书打十星!
  2. SPOJ - VLATTICE
  3. springcloud的fallback与fallbackFactory
  4. 2/2 常用函数:标准库函数
  5. 深入解读 Entity Framework 4.0和4.1
  6. 如何重命名图层名称_PS新手教程:教你认识“图层”面板及图层面板的相关操作方法...
  7. mysql性能剖析工具_MySQL性能剖析工具(pt-query-digest)【转】
  8. 晚间看图片就高亮,这体验太差
  9. calibre怎么把HTML转换mobi,calibre:mobi格式转换成pdf格式 | 求索阁
  10. 切换IP配置的bat批处理命令
  11. mac 安装ffmpeg以及各种编码器
  12. 谷歌放弃火狐的谷歌工具栏产品
  13. 工业物联网盒子python_【工业4.0面面观】之十八:基于AWS的工业物联网应用案例...
  14. [NOIP2011] 观光公交解题报告
  15. python-django(一)
  16. vue获取微信登陆权限_vue 微信授权登录解决方案
  17. 易盾php,PHP接入网易易盾验证码
  18. 新版标准日本语初级_第二十三课
  19. 数据分析--数据分析是什么?
  20. JavaSE程序分析005 Integer的小事情

热门文章

  1. 各个国家/地区以及对应的手机区号
  2. blender 常用快捷键
  3. QT项目 MyQQ 学习笔记(一)
  4. C# 打印PDF文件之使用不同打印机打印所有页面或部分页面
  5. Ubuntu20.04使用1080Ti配置深度学习工作环境出现桌面滚动时卡顿问题解决
  6. 2022年上半年软件设计师(软考)————考后总结
  7. MTK6589手机工程模式调大各种音量方法
  8. decklink linux 驱动下载,Blackmagic Design DeckLink采集卡驱动
  9. 命令改计算机用户名和密码,利用NET命令添加、修改用户账户信息
  10. X,X11,Xorg,XServer,XClient,Xlib