第十一章 大数 / 11.4 使用示例

       1)示例1

#include <openssl/bn.h>

       #include <string.h>

       #include <openssl/bio.h>

                           

       int    main()

       {

                     BIGNUM*bn;

                     BIO        *b;

                     chara[20];

                     int           ret;

                           

                     bn=BN_new();

                     strcpy(a,"32");

                     ret=BN_hex2bn(&bn,a);

                     b=BIO_new(BIO_s_file());

                     ret=BIO_set_fp(b,stdout,BIO_NOCLOSE);

                     BIO_write(b,"aaa",3);

                     BN_print(b,bn);

                     BN_free(bn);

                     return 0;

       }

 

       2)示例2

              加法运算

              #include <openssl/bn.h>

              #include <string.h>

              #include <openssl/bio.h>

                           

              int    main()

              {

                     BIGNUM*a,*b,*add;

                     BIO       *out;

                     charc[20],d[20];

                     int           ret;

                           

                     a=BN_new();

                     strcpy(c,"32");

                     ret=BN_hex2bn(&a,c);

                     b=BN_new();

                     strcpy(d,"100");

                     ret=BN_hex2bn(&b,d);

                     out=BIO_new(BIO_s_file());

                     ret=BIO_set_fp(out,stdout,BIO_NOCLOSE);

                     add=BN_new();

                     ret=BN_add(add,a,b);

                     if(ret!=1)

                     {

                            printf("err.\n");

                            return -1;

                     }

                     BIO_puts(out,"bn 0x32 + 0x100 = 0x");

                     BN_print(out,add);

                     BIO_puts(out,"\n");

                     BN_free(a);

                     BN_free(b);

                     BN_free(add);

                     BIO_free(out);

                     return 0;

              }

       3  示例3           

              减法运算

              #include <openssl/bn.h>

              #include <string.h>

              #include <openssl/bio.h>

             

              int    main()

              {

                     BIGNUM*a,*b,*sub;

                     BIO        *out;

                     charc[20],d[20];

                     int           ret;

             

                     a=BN_new();

                     strcpy(c,"100");

                     ret=BN_hex2bn(&a,c);

                     b=BN_new();

                     strcpy(d,"32");

                     ret=BN_hex2bn(&b,d);

                     out=BIO_new(BIO_s_file());

                     ret=BIO_set_fp(out,stdout,BIO_NOCLOSE);

                     sub=BN_new();

                     ret=BN_sub(sub,a,b);

                     if(ret!=1)

                     {

                            printf("err.\n");

                            return -1;

                     }

                     BIO_puts(out,"bn 0x100 - 0x32 = 0x");

                     BN_print(out,sub);

                     BIO_puts(out,"\n");

                     BN_free(a);

                     BN_free(b);

                     BN_free(sub);

                     BIO_free(out);

                     return 0;

       }

       4)示例4

              乘法运算

              #include <openssl/bn.h>

              #include <string.h>

              #include <openssl/bio.h>

             

              int    main()

              {

                     BIGNUM*a,*b,*mul;

                     BN_CTX*ctx;

                     BIO        *out;

                     charc[20],d[20];

                     int           ret;

                    

                     ctx=BN_CTX_new();

                     a=BN_new();

                     strcpy(c,"32");

                     ret=BN_hex2bn(&a,c);

                     b=BN_new();

                     strcpy(d,"100");

                     ret=BN_hex2bn(&b,d);

                     out=BIO_new(BIO_s_file());

                     ret=BIO_set_fp(out,stdout,BIO_NOCLOSE);

                     mul=BN_new();

                     ret=BN_mul(mul,a,b,ctx);

                     if(ret!=1)

                     {

                            printf("err.\n");

                            return -1;

                     }

                     BIO_puts(out,"bn 0x32 * 0x100 = 0x");

                     BN_print(out,mul);

                     BIO_puts(out,"\n");

                     BN_free(a);

                     BN_free(b);

                     BN_free(mul);

                     BIO_free(out);

                     BN_CTX_free(ctx);

                     return 0;

              }

       5)示例5

              除法运算

              #include <openssl/bn.h>

              #include <string.h>

              #include <openssl/bio.h>

             

              int    main()

              {

                     BIGNUM*a,*b,*div,*rem;

                     BN_CTX*ctx;

                     BIO        *out;

                     charc[20],d[20];

                     int           ret;

                    

                     ctx=BN_CTX_new();

                     a=BN_new();

                     strcpy(c,"100");

                     ret=BN_hex2bn(&a,c);

                     b=BN_new();

                     strcpy(d,"17");

                     ret=BN_hex2bn(&b,d);

                     out=BIO_new(BIO_s_file());

                     ret=BIO_set_fp(out,stdout,BIO_NOCLOSE);

                     div=BN_new();

                     rem=BN_new();

                     ret=BN_div(div,rem,a,b,ctx);

                     if(ret!=1)

                     {

                            printf("err.\n");

                            return -1;

                     }

                     BIO_puts(out,"bn 0x100 / 0x17 =0x");

                     BN_print(out,div);

                     BIO_puts(out,"\n");

                     BIO_puts(out,"bn 0x100 % 0x17 =0x");

                     BN_print(out,rem);

                     BIO_puts(out,"\n");

                     BN_free(a);

                     BN_free(b);

                     BN_free(div);

                     BN_free(rem);

                     BIO_free(out);

                     BN_CTX_free(ctx);

                     return 0;

              }

       6)示例6

              平方运算

              #include <openssl/bn.h>

              #include <string.h>

              #include <openssl/bio.h>

                           

              int    main()

              {

                     BIGNUM*a,*sqr;

                     BN_CTX*ctx;

                     BIO        *out;

                     charc[20];

                     int           ret;

                    

                     ctx=BN_CTX_new();

                     a=BN_new();

                     strcpy(c,"100");

                     ret=BN_hex2bn(&a,c);

                     sqr=BN_new();

                     out=BIO_new(BIO_s_file());

                     ret=BIO_set_fp(out,stdout,BIO_NOCLOSE);

                     ret=BN_sqr(sqr,a,ctx);

                     if(ret!=1)

                     {

                            printf("err.\n");

                            return -1;

                     }

                     BIO_puts(out,"bn 0x100 sqr  =0x");

                     BN_print(out,sqr);

                     BIO_puts(out,"\n");

                     BN_free(a);

                     BN_free(sqr);

                     BIO_free(out);

                     BN_CTX_free(ctx);

                     return 0;

              }           

       7)示例7

              次方运算

              #include <openssl/bn.h>

              #include <string.h>

              #include <openssl/bio.h>

                           

              int    main()

              {

                     BIGNUM*a,*exp,*b;

                     BN_CTX*ctx;

                     BIO        *out;

                     charc[20],d[20];

                     int           ret;

                    

                     ctx=BN_CTX_new();

                     a=BN_new();

                     strcpy(c,"100");

                     ret=BN_hex2bn(&a,c);

                     b=BN_new();

                     strcpy(d,"3");

                     ret=BN_hex2bn(&b,d);

                     exp=BN_new();

                     out=BIO_new(BIO_s_file());

                     ret=BIO_set_fp(out,stdout,BIO_NOCLOSE);

                     ret=BN_exp(exp,a,b,ctx);

                     if(ret!=1)

                     {

                            printf("err.\n");

                            return -1;

                     }

                     BIO_puts(out,"bn 0x100 exp 0x3  =0x");

                     BN_print(out,exp);

                     BIO_puts(out,"\n");

                     BN_free(a);

                     BN_free(b);

                     BN_free(exp);

                     BIO_free(out);

                     BN_CTX_free(ctx);

                     return 0;

              }