<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>
"); //-->

博客專(zhuān)欄

EEPW首頁(yè) > 博客 > SM2算法第二十七篇: openssl庫中的BIGNUM(超贊)

SM2算法第二十七篇: openssl庫中的BIGNUM(超贊)

發(fā)布人:電子禪石 時(shí)間:2023-09-05 來(lái)源:工程師 發(fā)布文章

openssl庫中的BIGNUM,處理大數比較好,可以用在很多方面。

BIGNUM是一個(gè)typedef的結構,可以直接使用。但一般來(lái)說(shuō),使用它的指針結構。如:BIGNUM *p;

BIGNUM的創(chuàng )建與釋放
函數原型解釋示例
BIGNUM * BN_new (void);創(chuàng )建一個(gè)BIGNUM的結構,返回新BIGNUM結構的指針BIGNUM *a = BN_new ();
void BN_free (BIGNUM *);釋放一個(gè)BIGNUMfree (a);
BIGNUM的值測試
函數原型解釋示例
int BN_cmp (BIGNUM *a, BIGNUM *b);| 判斷a與b是否相等if (BN_cmp (a, b) { printf ("a equ b/n"); }
<verbatim>int BN_ucmp (BIGNUM *a, BIGNUM *b);判斷a與b的絕對值是否相等if (BN_ucmp (a, b)
int BN_is_zero(BIGNUM *a);判斷a是不是為0if (BN_is_zero (a))
int BN_is_one(BIGNUM *a);判斷a是不是1if (BN_is_one (a))
int BN_is_word(BIGNUM *a, BN_ULONG w);判斷a是不是值wif (BN_is_word (a, 12))
int BN_is_odd(BIGNUM *a);判斷a是不是一個(gè)奇數if (BN_is_odd (a))
BIGNUM的賦值與取值
函數原型解釋示例
int BN_num_bytes(BIGNUM *a);返回a的字節數printf ("length: %d/n", BN_num_tytes (a));
int BN_num_bits(BIGNUM *a);返回a的二進(jìn)制位數printf ("bits: %d/n", BN_num_bits (a));
int BN_one(BIGNUM *a);設置a為1BN_one (a);
int BN_zero(BIGNUM *a);設置a為0BN_zero (a);
int BN_bn2bin(const BIGNUM *a, unsigned char *to);取a為二進(jìn)制到to中,返回字符串長(cháng)度char s[1024];
int length = BN_bn2bin (a, s);
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);賦二進(jìn)制值s到ret中,返回retchar s[] = "1001001";
BN_bin2bn (s, strlen (s), a);
char *BN_bn2hex(const BIGNUM *a);取a的16進(jìn)制值,返回一個(gè)字符串的指針。此指針要使用完后,手動(dòng)使用OPENSSL_free釋放char *p = BN_bn2hex (a); 
if (p)
{
printf ("number is 0x%s/n", p);
OPENSSL_free (p);
}
char *BN_bn2dec(const BIGNUM *a);取a的10進(jìn)制值,返回一個(gè)字符串的指針。此指針要使用完后,手動(dòng)使用OPENSSL_free釋放p = BN_bn2dec (a);
int BN_hex2bn(BIGNUM **a, const char *str);賦16進(jìn)制值str到*a中,返回成功與否BN_hex2bn (&a, "0x123F23D12");
int BN_dec2bn(BIGNUM **a, const char *str);賦10進(jìn)制值str到*a中,返回成功與否BN_dec2bn (&a, "1999");
BIGNUM的計算
函數原型解釋示例
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);計算a與b的和,值儲存在r中。如果成功,返回1;否則,返回0BIGNUM *a = BN_new (), *b = BN_new (), *r = BN_new ();
BN_dec2bn (&a, "1234");
BN_dec2bn (&b, "2345");
BN_add (r, a, b);
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);計算a與b的差,值儲存在r中。返回1或者0BN_sum (r, a, b);
int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);計算a與b的積,值儲存在r中。返回1或者0。ctx為一個(gè)上下文相關(guān)的結構,可以用BN_CTX_new與BN_CTX_free來(lái)創(chuàng )建與釋放它BN_CTX *ctx = BN_CTX_new ();
BN_mul (r, a, b, ctx);
BN_CTX_free (ctx);
int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx);計算a的平方,值儲存在r中。返回1或者0BN_sqr (r, a, ctx);
int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d, BN_CTX *ctx);計算a與d的商,值儲存在dv中,余數儲存在rem中。返回1或者0BN_div (dv, r, a, b);
int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);計算a與m的模,值儲存在rem中。返回1或者0BN_mod (r, a, m, ctx);
int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);計算a與m的模,并且結果如果小于0,就加上m,值儲存在r中。返回1或者0BN_nnmod (r, a, m, ctx);
int BN_mod_add(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);計算a與b的和,再模m,值儲存在r中。返回1或者0BN_mod_add (r, a, b, m, ctx);
int BN_mod_sub(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);計算a與b的差,再模m,值儲存在r中。返回1或者0BN_mod_sub (r, a, b, m, ctx);
int BN_mod_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);計算a與b的積,再模m,值儲存在r中。返回1或者0BN_mod_mul (r, a, b, m, ctx);
int BN_mod_sqr(BIGNUM *r, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);計算a的平方根,再模m,值儲存在r中。返回1或者0BN_mod_sqr (r, a, m, ctx);
int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);計算a的p次方,值儲存在r中。返回1或者0BN_exp (r, a, p, ctx);
int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx);計算a的p次方,再模m,值儲存在r中。返回1或者0BN_mod_exp (r, a, p, m, ctx);
int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);計算a與b的最大公約數,值儲存在r中。返回1或者0BN_gcd (r, a, b, ctx);
int BN_add_word(BIGNUM *a, BN_ULONG w);大數a加上w,值儲存在a中,返回1或者0BN_dec2bn (a, "1234");
BN_add_word (a, 1);
int BN_sub_word(BIGNUM *a, BN_ULONG w);大數a減去w,值儲存在a中,返回1或者0BN_sub_word (a, 23);
int BN_mul_word(BIGNUM *a, BN_ULONG w);大數a乘以w,值儲存在a中,返回1或者0BN_mul_word (a, 2);
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);大數a除以w,值儲存在a中,返回余數BN_ULONG ru = BN_div_word (a, 256);
BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);大數a模w,返回余數ru = BN_mod_word (a, 256);

轉自:http://blogold.chinaunix.net/u/19628/showart_2081074.html


*博客內容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀(guān)點(diǎn),如有侵權請聯(lián)系工作人員刪除。



關(guān)鍵詞: openssl

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>