2013-06-06 4 views

답변

0

이 기능을 직접 수행 할 수없는 것처럼 보입니다. 따라서이 기능을 기반으로하는 무언가를 찾아야합니다. 뭔가 같은 :이 a의 비트 수보다 큰 b 또는 그 반대의 경우 수동으로 0으로 높은 순서 비트를 설정해야 할 수도

BIGNUM *a, *b, *result; 
unsigned current = 0; 

//Creation of a, b, result 

while(!BN_zero(a) && !BN_zero(b)) { 
    if(BN_is_bit_set(a, current) && BN_is_bit_set(b, current)) { 
     BN_set_bit(result, current); 
    } else { 
     BN_clear_bit(result, current); 
    } 
    ++current; 
    BN_rshift1(a, a); 
    BN_rshift1(b, b); 
} 

참고. 그러나 시작하기에 충분해야합니다.

3

OpenSSL에서 BIGNUM에 대한 비트와 함수가 없습니다. 비트 방식을 사용하는 방법은 다음과 같습니다. 적절한 솔루션을 찾을 때까지 사용할 수 있습니다.

BN_ULONG bn_and_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) 
{ 
    BN_ULONG l,t; 

    if (n <= 0) return((BN_ULONG)0); 

    while(n) 
    { 
     t=a[0]; 
     l=(t&b[0]); 
     l=(t&b[0])&BN_MASK2; 
     r[0]=l; 
     a++; b++; r++; n--; 
    } 
    return((BN_ULONG)*r); 
} 

상기 내부 함수 bn_and_words

이 기능에서 사용된다 :

int BN_bitwise_and(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 
{ 
    int max,min,dif; 
    BN_ULONG *ap,*bp,*rp; 
    const BIGNUM *tmp; 

    bn_check_top(a); 
    bn_check_top(b); 

    if (a->used< b->used) 
     { tmp=a; a=b; b=tmp; } 
    max = a->used; 
    min = b->used; 
    dif = max - min; 

    if (bn_wexpand(r,max+1) == NULL) 
     return 0; 

    r->used=max; 

    ap=a->d; 
    bp=b->d; 
    rp=r->d; 

    bn_and_words(rp,ap,bp,min); 
    rp+=min; 
    ap+=min; 
    bp+=min; 

    while (dif) 
    { 
     *(rp++) = *(ap++); 
     dif--; 
    } 
    r->neg = 0; 
    bn_check_top(r); 
    return 1; 
} 
a AND b

결과 r의 첫번째 인수 및 기능 BN_bitwise_and의 리턴 값이다.

int test_and() 
{ 
    BIGNUM *a,*b,*r; 
    a=BN_new(); 
    b=BN_new(); 
    r=BN_new(); 

    if (!BN_hex2bn(&a, "1234567890ABCDEF")) return -1; 
    if (!BN_hex2bn(&b, "FEDCBA0987654321")) return -1; 

    BN_bitwise_and(r,a,b); 
    BN_print_fp(stdout, r); 

    BN_free(a); 
    BN_free(b); 
    BN_free(r); 
} 

표준 출력에 인쇄 된 결과 r이 도움이

1214120880214121 

희망입니다 : 여기

는 테스트입니다.

+0

고마워요. 이것이 오픈 솔에서 직접 가능하지 않다는 것은 놀라운 일이 아닙니다. 그러나 이것은 멋지게 보입니다. – user2457823

+0

당신은 환영합니다 :) – ChiaraHsieh