openssl库中BIGNUM的使用

随机数

加减乘除

取模

指数

 

#include 
#include 
#include 
#include 

void bn_printf(BIGNUM * a, int n)
{
	printf("0x");
	BN_print_fp(stdout, a);
	if (n)
		printf("\n");
}

void bn_hex_printf(BIGNUM * a)
{
	char *p = BN_bn2hex(a);
	printf("0x%s\n", p);
	OPENSSL_free(p);
}

void bn_dec_printf(BIGNUM * a)
{
	char *p = BN_bn2dec(a);
	printf("%s\n", p);
	OPENSSL_free(p);
}

int main(int argc, char *argv[])
{
	int bits = 3;

	if (argc > 1) {
		bits = atoi(argv[1]);
	}
	if (bits <= 0) {
		bits = 8;
	}
	printf("bits: %d\n", bits);

	BN_CTX *ctx;
	BIGNUM a, b, c, d;

	ctx = BN_CTX_new();
	if (ctx == NULL)
		exit(1);

	BN_init(&a);
	BN_init(&b);
	BN_init(&c);
	BN_init(&d);

	BN_rand(&a, bits, 1, 1);
	printf("BN_rand a: ");
	bn_printf(&a, 1);
	bn_hex_printf(&a);
	bn_dec_printf(&a);

	BN_add_word(&a, 1);
	printf("BN_add_word a + 1\n");
	bn_printf(&a, 1);

	BN_free(&a);
	BN_init(&a);

	BN_rand(&a, bits, 1, 0);
	printf("BN_rand a: ");
	bn_printf(&a, 1);

	BN_rand(&b, bits, 0, 1);
	printf("BN_rand b: ");
	bn_printf(&b, 1);

	BN_add(&c, &a, &b);
	printf("BN_add a + b\n");
	bn_printf(&a, 0);
	printf(" + ");
	bn_printf(&b, 0);
	printf(" = ");
	bn_printf(&c, 1);

	BN_sub(&c, &a, &b);
	printf("BN_sub a - b\n");
	bn_printf(&a, 0);
	printf(" - ");
	bn_printf(&b, 0);
	printf(" = ");
	bn_printf(&c, 1);

	BN_mul(&c, &a, &b, ctx);
	printf("BN_mul a * b\n");
	bn_printf(&a, 0);
	printf(" * ");
	bn_printf(&b, 0);
	printf(" = ");
	bn_printf(&c, 1);

	BN_div(&d, &c, &a, &b, ctx);
	printf("BN_div a / b\n");
	bn_printf(&a, 0);
	printf(" / ");
	bn_printf(&b, 0);
	printf(" = ");
	bn_printf(&d, 1);
	printf("remainder ");
	bn_printf(&c, 1);

	BN_mod(&c, &a, &b, ctx);
	printf("BN_mod a %% b\n");
	bn_printf(&a, 0);
	printf(" %% ");
	bn_printf(&b, 0);
	printf(" = ");
	bn_printf(&c, 1);

	BN_exp(&c, &a, &b, ctx);
	printf("BN_exp a ^ b\n");
	bn_printf(&a, 0);
	printf(" ^ ");
	bn_printf(&b, 0);
	printf(" = ");
	if (BN_num_bits(&c) < 64 * 8) {
		bn_printf(&c, 1);
	} else {
		printf("BN_num_bits(c) %d too long, not print\n",
		       BN_num_bits(&c));
	}

	BN_set_word(&a, 100);
	printf("BN_set_word a: ");
	bn_printf(&a, 1);

	bn_printf(&a, 0);
	if (BN_is_word(&a, 100)) {
		printf(" is 100\n");
	} else {
		printf(" is not 100\n");
	}

	BN_set_negative(&a, 1);
	printf("BN_set_negative a: ");
	bn_dec_printf(&a);

	BN_free(&a);
	BN_init(&a);

	BN_generate_prime_ex(&a, bits, 1, NULL, NULL, NULL);
	printf("BN_generate_prime_ex a: ");
	bn_printf(&a, 1);

	bn_printf(&a, 0);
	if (1 == BN_is_prime_ex(&a, BN_prime_checks, NULL, NULL)) {
		printf(" is prime\n");
	} else {
		printf(" is not prime\n");
	}

	BN_add_word(&a, 2);
	bn_printf(&a, 0);
	if (1 == BN_is_prime_ex(&a, BN_prime_checks, NULL, NULL)) {
		printf(" is prime\n");
	} else {
		printf(" is not prime\n");
	}

	BN_free(&a);
	BN_free(&b);
	BN_free(&c);
	BN_free(&d);

	BN_CTX_free(ctx);

	return 0;
}


 

运行结果

 

$ gcc -Wall -lcrypto bn.c


$ ./a.out 5                                             
bits: 5
BN_rand a: 0x19
0x19
25
BN_add_word a + 1
0x1A
BN_rand a: 0x1E
BN_rand b: 0x1D
BN_add a + b
0x1E + 0x1D = 0x3B
BN_sub a - b
0x1E - 0x1D = 0x1
BN_mul a * b
0x1E * 0x1D = 0x366
BN_div a / b
0x1E / 0x1D = 0x1
remainder 0x1
BN_mod a % b
0x1E % 0x1D = 0x1
BN_exp a ^ b
0x1E ^ 0x1D = 0x4EC8A7BC9DC1FCCECDF11AC97979E0000000
BN_set_word a: 0x64
0x64 is 100
BN_set_negative a: -100
BN_generate_prime_ex a: 0x8C27
0x8C27 is prime
0x8C29 is not prime


$ ./a.out 16
bits: 16
BN_rand a: 0xD3C5
0xD3C5
54213
BN_add_word a + 1
0xD3C6
BN_rand a: 0xCDE5
BN_rand b: 0xB79B
BN_add a + b
0xCDE5 + 0xB79B = 0x18580
BN_sub a - b
0xCDE5 - 0xB79B = 0x164A
BN_mul a * b
0xCDE5 * 0xB79B = 0x93AB5CA7
BN_div a / b
0xCDE5 / 0xB79B = 0x1
remainder 0x164A
BN_mod a % b
0xCDE5 % 0xB79B = 0x164A
BN_exp a ^ b
0xCDE5 ^ 0xB79B = BN_num_bits(c) 737278 too long, not print
BN_set_word a: 0x64
0x64 is 100
BN_set_negative a: -100
BN_generate_prime_ex a: 0xDEA7
0xDEA7 is prime
0xDEA9 is not prime


 

你可能感兴趣的:(openssl)