66116


<https://qiita.com/quailDegu/items/668fb0b27e757302a1e7> のコードをコンパイル時に

[code]

    RSA.c:77:7: warning: & has lower precedence than ==; == will be evaluated first
          [-Wparentheses]
                    if(e&1==1){
                        ^~~~~
    RSA.c:77:7: note: place parentheses around the '==' expression to silence this
          warning
                    if(e&1==1){
                        ^
                         (   )
    RSA.c:77:7: note: place parentheses around the & expression to evaluate it first
                    if(e&1==1){
                        ^
                       (  )
    1 warning generated.
    
[/code]

とでます。

[code]

    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include<time.h>
    
    bool primaryNum(long number){
        int i=0;
        bool flag=true;
        if(number<2){
            return false;
        }
        for(i=2;i<sqrt(number);i++){
            if(number%i==0){
                return false;
            }
        }
        return flag;
    }
    
    long gcd(long a,long b){
        if(a<b){
            return false;
        }
        if(b==0){
            return a;
        }else{
            return gcd(b,a%b);
        }
    }
    
    long leastCommonMultiple(long a,long b){
        return a*b/gcd(a,b);
    }
    long euclidEx(long a,long b){
        long x;
        long y;
        long x1 = 1;
        long x2 = 0;
        long x3;
        long y1 = 0;
        long y2 = 1;
        long y3;
        long result1 = b;
        long result2 = a;
        long result3;
        long q;
        while(1){
            if(result2==0){
                printf("%ld\n",result1);
                printf("0乗算によるエラーもう一度実行してください\n");
                exit(1);
            }
            q = result1 / result2;
            x3 = x1 - (q*x2);
            y3 = y1 - (q*y2);
            result3 = result1 - q*result2;
            if(result3==1){
                if(y3<0){
                    return y3+b;
                }
                return y3;
            }
            x1 = x2;
            y1 = y2;
            result1 = result2;
            x2 = x3;
            y2 = y3;
            result2 = result3;
        }
        return 0;
    }
    
    long modpow(long a,long e,long n){
        long result = 1;
        while(e>0){
            if(e&1==1){
                result = (result*a)%n;
            }
            e>>=1;
            a=(a*a)%n;
        }
        return result;
    }
    
    long encryption(long a,long e,long n){
        long code = modpow(a,e,n);
        if(code<0){
            code = (code+n)%n;
        }
        return code;
    }
    
    long decryption(long b,long d,long n){
        return modpow(b,d,n);
    }
    
    int main(){
        long q,p,n,e,d;
        long demoNum;
        long code,plaintext;
        srand((unsigned) time(NULL));
        scanf("%ld",&demoNum);
        while(1){
            q=rand()%(9999-5+1)+5;
            if(primaryNum(q)){
                break;
            }
        }
        while(1){
            p=rand()%(9999-5+1)+5;
            if(primaryNum(p)&&p!=q){
                break;
            }
        }
        n=q*p;
        printf("n(公開鍵1)->%ld\n",n);
        while(1){
            e=rand()%((n-1)-5+1)+5;
            if(gcd((p-1)*(q-1),e)==1){
                break;
            }
        }
        printf("e(公開鍵2)->%ld\n",e);
        d = euclidEx(e,(p-1)*(q-1));
        printf("d(秘密鍵)->%ld\n",d);
        code = encryption(demoNum,e,n);
        printf("%ldを%ld,%ldで暗号化->%ld\n",demoNum,n,e,code);
        plaintext = decryption(code,d,n);
        printf("%ldを%ld,%ldで復号化->%ld\n",code,n,d,plaintext);
        return 0;
    }
    
[/code]


CC BY-SA 4.0
2020-04-29T11:08:11.050
66108
2020-04-29T13:12:58.563
2020-04-29T13:03:33.980
19110
39787
question
c
RSA暗号のプログラムを参考にしたのですがコンパイルできません

[code]

    if(e&1==1){
    
[/code]

を

[code]

    if((e&1)==1){
    
[/code]

してみたらどうですか？

`&` よりも `==` の方が優先度が高いので、`()` を付けないと、`e & (1 == 1)` で評価が行われてしまうよ、という話です。  
`1 == 1` なんて、わざわざ評価する必要ありませんから、`(e & 1) == 1` のはずだと思います。


CC BY-SA 4.0
2020-04-29T13:12:58.563
66116
2020-04-29T13:12:58.563
7290
66108
answer