Windows C ++ char无法正常显示,只显示一些unknkown字符

时间:2018-04-11 06:01:05

标签: c++ windows

我在Ubuntu上用C ++编写了RSA代码。它工作正常,它在Windows Dev C ++上运行良好,但它没有正确显示该字符。

以下是代码:

#include<iostream>
#include<stdlib.h>                 // for rand()
#include<math.h>                   // for floor function
#include<string.h>             
using namespace std;


//function to check whether a number is prime or not
int  check_prime(int number)
{
    int count = 0;
    for(int i = 2; i<number + 1; i++)
    {
        if(number%i == 0)
        {
            count++;
        }
    }
    if(count>2)
    {
        return 0;
    }
    else
    {
        return 1;
    }

}

//function to generate a random prime number
int generate_random_prime()
{
    int temp;
    while(1)
    {
        temp = rand() % 50;
        if(check_prime(temp) == 1)
        {
            return  temp;
        }
    }
}

int gcd(int a, int b)
{
    int temp;
    while(b != 0)
    {
        temp = b;
        b = a%b;
        a = temp;
    }
    return a;
}

//  Extended Euclid GCD to find d such de congruent to 1
int extended_gcd(int a, int b)
{
    int d, x, y, r, q;
    if(b == 0)
    {
        d = a;
        x = 1;
        y = 0;
        cout << "\n d= " << d << " x= " << x << " y= " << y << "\n";
    }
    int x2, x1, y2, y1;
    x2 = 1;
    x1 = 0;
    y2 = 0;
    y1 = 1;
    while(b > 0)
    {
        q = floor(a / b);
        r = a - q*b;
        x = x2 - q*x1;
        y = y2 - q*y1;
        a = b;
        b = r;
        x2 = x1;
        x1 = x;
        y2 = y1;
        y1 = y;
    }
    d = a;
    x = x2;
    y = y2;
    return x2;
}

//returns a^b mod n using square and multiply method
int modular_exponentiation(int a, int b, int n)
{
    if(a == 1)
    {
        return 0;
    }
    int c = 1;
    for(int i = 1; i < b + 1; i++)
    {
        c = (c*a) % n;
    }
    return c;
}


//cipher text = (message^e) %n
int cipher_text(int m, int e, int n)
{
    return modular_exponentiation(m, e, n);
}

//decrypted_text= (cipher^d)%n
int decrypt_cipher(int c, int d, int n)
{
    return modular_exponentiation(c, d, n);
}

int main()
{

    // generating two random prime p and q
    int p = generate_random_prime();
    int q = generate_random_prime();

    cout << "Prime p : " << p << "and  q : " << q << "\n";

    int n = p*q;
    cout << "n=p*q = " << n << "\n";
    //calculating Euler Totient for prime p and q
    int euler_phi = (p - 1)*(q - 1);
    cout << "Euler totient is : " << euler_phi << "\n";

    int d, e;
    // calculating e such that 1<e<euler_phi and gcd(n,euler_phi)=1
    while(1)
    {
        e = rand() % (euler_phi - 1 + 1) + 1;
        if(gcd(euler_phi, e) == 1)
        {
            break;
        }
    }

    cout << "e value is : " << e << "\n";

    //calculating d such that ed congruent 1, ed=1
    d = extended_gcd(e, euler_phi);
    //d=5;
    cout << "d value is : " << d << "\n";

    //storing the message to be encrypted as char array and encrypting each char element
    char message[20];
    int cipher[20];
    cout << "Enter the message to be encrypted : ";
    cin >> message;


    cout << "Message to be encrypted is : " << message << "\n";

    int size = strlen(message);

    //calculating cipher text c
    for(int i = 0; i < size; i++)
    {
        cipher[i] = cipher_text(int(message[i]), e, n);
    }

    cout << "Cipher text is : ";
    for(int i = 0; i < size; i++)
    {
        cout << cipher[i] << " ";
    }

    char message_decrypted[size];
    //decrypting cipher text
    for(int i = 0; i < size; i++)
    {
        message_decrypted[i] = decrypt_cipher(cipher[i], d, n);
    }

    cout << "\nDecrypted message is : ";
    for(int i = 0; i < size; i++)
    {
        cout << message_decrypted[i];
    }
    cout << "\n";

    return 0;
}

我在DevC ++上尝试过使用g ++的代码。 检查图像: Image 1

使用g ++编译器的图像 Image 2

我需要一种方法来打印要正确显示的字符。

我认为需要更改message_decrypted[i]=decrypt_cipher(cipher[i],d,n);以在Devcpp中正确打印字符

以下是在线IDE中代码的链接,它可以正常工作https://repl.it/@shubhamjohar/RSA

2 个答案:

答案 0 :(得分:3)

当您的main例程调用

decrypt_cipher(cipher[i], d, n);

密码[0]为386,与上面的输出相匹配。 d是-179。 n是697

modular_exponentiation(a=386, b=-179, n=697)的相应调用会导致跳过此for循环:

for (int i = 1; i<b + 1; i++) {
    c = (c*a) % n;
}

因为i < (b + 1)评估为(1 < -178),其评估结果为false

因此,您的modular_exponentiation会返回1,这是一个不可打印的字符。

同样适用于从main调用decrypt_cipher的后续操作。

我对RSA算法知之甚少,不知道您的实现是否正确。但是当d为负时,for循环不会进行任何循环。

答案 1 :(得分:-1)

可能是您的程序中的以下表达式引起的:

  

char message_decrypted [size];

有一些与此用法相关的标准更改。请阅读以下页面了解更多详情。 https://www.geeksforgeeks.org/variable-length-arrays-in-c-and-c/

或尝试使用new char[size]之类的东西动态分配内存。