atoll C语言实现

atoll在linux c有实现,windows vs下是没有的,也不属于标准c语言库函数,但是我需要在vs下用到这个功能,下面我自己实现的代码:


long long atoll(const char* str)
{
	long long e = 0;
	long long tmpKeep = 0;
	int i = 0;
	const int len = strlen(str);
	
	if (len <= 0) {
		goto EXIT_FUN;
	}

	if (str[0] == '-') {
		e = atoll(str + 1);
		e = (-e);
		goto EXIT_FUN;
	}
	
	for (i = 0; i < len; i++) {
		if (str[i] > '9' || str[i] < '0') {
			break;
		}
		tmpKeep = e;
		e *= 10;
		e += (((long long)(str[i] - '0')) & 0x00000000000000FF);
		if (tmpKeep > e) {
			/* 越界 */
			e = 0;
			goto EXIT_FUN;
		}
	}

EXIT_FUN:
	return e;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
保序加密(Order-Preserving Encryption,OPE)是一种加密方式,它可以将明文转换为密文,同时保持明文的大小关系不变。也就是说,如果明文 $m_1 < m_2$,那么密文 $c_1 < c_2$。这种加密方式在某些场景下非常有用,比如需要对数据库中的数据进行排序和搜索,但是又需要保护数据的隐私。 常见的保序加密有两种: 1. 基于可逆加密算法的保序加密(如AES、DES等),它们使用的是块加密算法,在加密之后会得到固定长度的密文。 2. 基于非可逆加密算法的保序加密(如SHA-1、SHA-2等),它们使用的是哈希算法,加密之后得到的密文长度是固定的,但是由于哈希算法是不可逆的,所以无法还原出原始明文。 下面是基于可逆加密算法的保序加密的实现示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> // 加密函数 void encrypt(char *plaintext, char *key, char *ciphertext){ AES_KEY aes_key; AES_set_encrypt_key(key, 128, &aes_key); AES_encrypt(plaintext, ciphertext, &aes_key); } // 解密函数 void decrypt(char *ciphertext, char *key, char *plaintext){ AES_KEY aes_key; AES_set_decrypt_key(key, 128, &aes_key); AES_decrypt(ciphertext, plaintext, &aes_key); } // OPE 加密函数 void ope_encrypt(unsigned long long plaintext, char *key, unsigned long long *ciphertext){ // 将明文转换为字符串 char plaintext_str[100]; sprintf(plaintext_str, "%llu", plaintext); // 加密字符串 char ciphertext_str[100]; encrypt(plaintext_str, key, ciphertext_str); // 将密文转换为数字 *ciphertext = atoll(ciphertext_str); } // OPE 解密函数 void ope_decrypt(unsigned long long ciphertext, char *key, unsigned long long *plaintext){ // 将密文转换为字符串 char ciphertext_str[100]; sprintf(ciphertext_str, "%llu", ciphertext); // 解密字符串 char plaintext_str[100]; decrypt(ciphertext_str, key, plaintext_str); // 将明文转换为数字 *plaintext = atoll(plaintext_str); } int main(){ // 定义明文和密钥 unsigned long long plaintext = 123456789; char *key = "0123456789abcdef"; // 加密明文 unsigned long long ciphertext; ope_encrypt(plaintext, key, &ciphertext); printf("明文:%llu,密文:%llu\n", plaintext, ciphertext); // 解密密文 unsigned long long plaintext2; ope_decrypt(ciphertext, key, &plaintext2); printf("解密后的明文:%llu\n", plaintext2); return 0; } ``` 这里使用了 OpenSSL 库中的 AES 加密算法实现 OPE 加密。需要注意的是,由于 OPE 加密只保证明文的大小关系不变,而不是保证明文的绝对值不变,所以加密之后的密文可能会很大,需要使用 unsigned long long 类型存储。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值