资源简介
用于CP_ABE相关方案的编程。The cpabe toolkit provides a set of programs implementing a ciphertext-policy attribute-based encryption scheme. It uses the PBC library for the algebraic operations. Note that the cpabe toolkit might not compile against versions of PBC older than 0.5.4.
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include “common.h“
void
init_aes( element_t k int enc AES_KEY* key unsigned char* iv )
{
int key_len;
unsigned char* key_buf;
key_len = element_length_in_bytes(k) < 17 ? 17 : element_length_in_bytes(k);
key_buf = (unsigned char*) malloc(key_len);
element_to_bytes(key_buf k);
if( enc )
AES_set_encrypt_key(key_buf + 1 128 key);
else
AES_set_decrypt_key(key_buf + 1 128 key);
free(key_buf);
memset(iv 0 16);
}
GByteArray*
aes_128_cbc_encrypt( GByteArray* pt element_t k )
{
AES_KEY key;
unsigned char iv[16];
GByteArray* ct;
guint8 len[4];
guint8 zero;
init_aes(k 1 &key iv);
/* TODO make less crufty */
/* stuff in real length (big endian) before padding */
len[0] = (pt->len & 0xff000000)>>24;
len[1] = (pt->len & 0xff0000)>>16;
len[2] = (pt->len & 0xff00)>>8;
len[3] = (pt->len & 0xff)>>0;
g_byte_array_prepend(pt len 4);
/* pad out to multiple of 128 bit (16 byte) blocks */
zero = 0;
while( pt->len % 16 )
g_byte_array_append(pt &zero 1);
ct = g_byte_array_new();
g_byte_array_set_size(ct pt->len);
AES_cbc_encrypt(pt->data ct->data pt->len &key iv AES_ENCRYPT);
return ct;
}
GByteArray*
aes_128_cbc_decrypt( GByteArray* ct element_t k )
{
AES_KEY key;
unsigned char iv[16];
GByteArray* pt;
unsigned int len;
init_aes(k 0 &key iv);
pt = g_byte_array_new();
g_byte_array_set_size(pt ct->len);
AES_cbc_encrypt(ct->data pt->data ct->len &key iv AES_DECRYPT);
/* TODO make less crufty */
/* get real length */
len = 0;
len = len
| ((pt->data[0])<<24) | ((pt->data[1])<<16)
| ((pt->data[2])<<8) | ((pt->data[3])<<0);
g_byte_array_remove_index(pt 0);
g_byte_array_remove_index(pt 0);
g_byte_array_remove_index(pt 0);
g_byte_array_remove_index(pt 0);
/* truncate any garbage from the padding */
g_byte_array_set_size(pt len);
return pt;
}
FILE*
fopen_read_or_die( char* file )
{
FILE* f;
if( !(f = fopen(file “r“)) )
die(“can‘t read file: %s\n“ file);
return f;
}
FILE*
fopen_write_or_die( char* file )
{
FILE* f;
if( !(f = fopen(file “w“)) )
die(“can‘t write file: %s\n“ file);
return f;
}
GByteArray*
suck_file( char* file )
{
FILE* f;
GByteArray* a;
struct stat s;
a = g_byte_array_new();
stat(file &s);
g_byte_array_set_size(a s.st_size);
f = fopen_read_or_die(file);
fread(a->data 1 s.st_size f);
fclose(f);
return a;
}
char*
suck_file_str( char* file )
{
GByteArray* a;
char* s;
unsigned char zero;
a = suck_file(file);
zero = 0;
g_byte_array_append(a &zero 1);
s = (char*) a->data;
g_byte_array_free(a 0);
return s;
}
char*
suck_stdin()
{
GString* s;
char* r;
int c;
s
- 上一篇:ANSYS二次开发及应用详解
- 下一篇:DSP实验直流电机 DSP实验直流电机
评论
共有 条评论