c++ - Windows C++ 字符显示不正确,只显示一些未知字符

标签 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

最佳答案

当您的 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 循环不会执行任何循环。

关于c++ - Windows C++ 字符显示不正确,只显示一些未知字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49767568/

相关文章:

windows - 如何在 Inno Setup 中为每个用户(包括 future 的新用户)安装文件?

.net - 您可以依赖哪些 Windows 环境变量?

windows - 使用 windows 传真服务器 2008 传真 PDF

c++ - 如何在派生 View 类中实现 CView::OnUpdate

c++ - 不同机器上的不同 Valgrind 输出

c++ - 将 __m256i 寄存器转换为 uint64_t 位掩码,使得每个字节的值是输出中的一个设置位

c++ - sem_timedwait 与 CLOCK_MONOTONIC_RAW/CLOCK_MONOTONIC

windows - 我在 microsoft.com/ABCTest 上唱 "Relying party with identifier ' https ://ABC. 时遇到问题'未找到。”

c++ - if 语句和 --variable

windows - 无法安装 Windows Azure Active Directory 模块 - Powershell