RSA解密在C语言中:无法获得正确的消息。

huangapple go评论108阅读模式
英文:

RSA Decryption in C: Can't get the correct message

问题

你的问题可能出在代码中,让我来帮你看看。

首先,确保你的 mod_exp 函数和解密函数 decrypt_message 中的所有参数和变量都使用正确的数据类型。在你的代码中,你使用了 unsigned long long,这是正确的数据类型。

然后,检查一下你的解密函数中的 printf 语句,确保它们正确打印解密后的 ASCII 字符。

最后,确保你的编译环境和工具链都配置正确。你提到你在 Windows 10 Pro 上使用 CodeBlocks 20.03 进行编译,确保编译器和设置都正确。

如果问题仍然存在,可以尝试使用调试器来跟踪代码执行,看看哪一部分出了问题。希望这些提示能帮助你解决问题。

英文:

Good night, guys. I am struggling for a week with an exercise of programming in C. It was basically:

"Imagine you know my public RSA key which has the modulus n = 16076635963 and the public exponent e = 456233 and you could intercept a message that was encrypted with this key and reads 5395334961. Decipher this message.

P.S.: The message is ASCII encoded."

I made plenty of tries and get these informations:

p: 123341
q: 130343
n: 16076635963
e: 456233
d: 3693459617
Public key (n, e): (16076635963, 456233)

My code is:

#include <stdio.h>

unsigned long long mod_exp(unsigned long long base, unsigned long long exponent, unsigned long long modulus) {
    unsigned long long result = 1;
    while (exponent > 0) {
        if (exponent & 1) {
            result = (result * base) % modulus;
        }
        base = (base * base) % modulus;
        exponent >>= 1;
    }
    return result;
}

void decrypt_message(unsigned long long encrypted_message, unsigned long long private_key, unsigned long long modulus) {
    unsigned long long decrypted_message = mod_exp(encrypted_message, private_key, modulus);

    printf("Decrypted Message: ");
    while (decrypted_message > 0) {
        unsigned char ascii_value = decrypted_message & 0xFF;
        printf("%c", ascii_value);
        decrypted_message >>= 8;
    }
    printf("\n");
}

int main() {
    unsigned long long modulus = 16076635963;
    unsigned long long public_exponent = 456233;
    unsigned long long private_key = 3693459617;
    unsigned long long encrypted_message = 5395334961;

    decrypt_message(encrypted_message, private_key, modulus);

    return 0;
}

But it only returns me: ║≈2♂

Please, help me. I lost 30% of my soul in this exercise and couldn't complete it. I am on Windows 10 Pro and using CodeBlocks 20.03 to compile.

答案1

得分: 0

谢谢,大家。128位也可以!

英文:

Thank you, guys. 128 bits worked as well!

huangapple
  • 本文由 发表于 2023年6月9日 06:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定