并行运行时不像我预期
我正在尝试使用OpenMP实现RSA算法的并行版本。该程序将公共密钥和N作为用户输入,计算到质数并生成公共密钥和私钥对,还生成了加密和解密的消息。我瘫痪了主要功能中的公共密钥生成循环。现在,问题在于我的程序无法计算经过的时间。因此,我对我的并行实施不佳。
代码块
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<omp.h>
#include<stdbool.h>
int GCD(int , int);
long int Decryption__k(long int , int );
unsigned Decryption(unsigned , unsigned, unsigned);
int main(int argc, char* argv[])
{
unsigned public_key, n, totient, private_key, encryption, my_msg, plainText;
unsigned f_prime, s_prime, temp = 0;
double start, end;
int thread_count=strtol(argv[1], NULL, 10);
printf("Enter value for public key: ");
scanf("%d", &public_key);
printf("Enter value for N: ");
scanf("%d", &n);
printf("\nEnter encrypted message: ");
scanf("%u", &my_msg);
if(my_msg >= n){
printf("\nError enter message small than N\n");
exit(0);
}
start = omp_get_wtime();
for (int i = 1; i <= n; ++i) {
if (n % i == 0) {
if(i % 2 == 1 && i != 1 && i != n){
f_prime = i;
if(f_prime != temp){
temp = f_prime;
f_prime = s_prime;
s_prime = temp;
}
}
}
}
totient = (f_prime - 1) * (s_prime - 1);
bool flag = false;
#pragma omp parallel num_threads(thread_count)
#pragma omp parallel for
for(public_key=0; public_key < totient; public_key++){
if(flag)continue;
if(GCD(public_key, totient) == 1){
encryption = public_key;
flag=true;
}
}
private_key = Decryption__k(encryption, totient);
printf("\nPublic Key: {%u, %d}\nPrivate Key: {%u, %d}\n Prime 1 : %d\nPrime 2: %d\n", encryption, n, private_key, n, f_prime, s_prime);
plainText = Decryption(my_msg, private_key, n);
printf("\nEncrypted message: %u\nDecrypted message: %u\n", my_msg, plainText);
end=omp_get_wtime();
printf("\nElapsed time =%e seconds\n", end - start);
return 0;
}
long int Decryption__k(long int e, int totient)
{
int d = 1;
while ((e * d) % totient != 1) {
d++;
}
return d;
}
unsigned Decryption(unsigned message, unsigned key, unsigned n)
{
unsigned r = 1;
for (unsigned a = 1, p = message; a <= key; a <<= 1, p = p * p % n){
if (key & a){
r = r * p % n;
}
}
return r;
}
int GCD(int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
输出:
根据输出的时间为NAN,
我在这里不包括GCD,解密__k和解密功能,因为我没有在那里实现催化作用。
谢谢
I am trying to implement a parallel version of RSA algorithm using openMP. The program takes public key and N as a user input, calculate to prime numbers and generate public key and private key pairs and also generated encrypted and decrypted message. I paralyzed the for loop for public key generation in the main function. Now, the problem is that my program unable to calculate elapsed time. So I am skeptical that my parallel implementation did not work well.
The code block
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<omp.h>
#include<stdbool.h>
int GCD(int , int);
long int Decryption__k(long int , int );
unsigned Decryption(unsigned , unsigned, unsigned);
int main(int argc, char* argv[])
{
unsigned public_key, n, totient, private_key, encryption, my_msg, plainText;
unsigned f_prime, s_prime, temp = 0;
double start, end;
int thread_count=strtol(argv[1], NULL, 10);
printf("Enter value for public key: ");
scanf("%d", &public_key);
printf("Enter value for N: ");
scanf("%d", &n);
printf("\nEnter encrypted message: ");
scanf("%u", &my_msg);
if(my_msg >= n){
printf("\nError enter message small than N\n");
exit(0);
}
start = omp_get_wtime();
for (int i = 1; i <= n; ++i) {
if (n % i == 0) {
if(i % 2 == 1 && i != 1 && i != n){
f_prime = i;
if(f_prime != temp){
temp = f_prime;
f_prime = s_prime;
s_prime = temp;
}
}
}
}
totient = (f_prime - 1) * (s_prime - 1);
bool flag = false;
#pragma omp parallel num_threads(thread_count)
#pragma omp parallel for
for(public_key=0; public_key < totient; public_key++){
if(flag)continue;
if(GCD(public_key, totient) == 1){
encryption = public_key;
flag=true;
}
}
private_key = Decryption__k(encryption, totient);
printf("\nPublic Key: {%u, %d}\nPrivate Key: {%u, %d}\n Prime 1 : %d\nPrime 2: %d\n", encryption, n, private_key, n, f_prime, s_prime);
plainText = Decryption(my_msg, private_key, n);
printf("\nEncrypted message: %u\nDecrypted message: %u\n", my_msg, plainText);
end=omp_get_wtime();
printf("\nElapsed time =%e seconds\n", end - start);
return 0;
}
long int Decryption__k(long int e, int totient)
{
int d = 1;
while ((e * d) % totient != 1) {
d++;
}
return d;
}
unsigned Decryption(unsigned message, unsigned key, unsigned n)
{
unsigned r = 1;
for (unsigned a = 1, p = message; a <= key; a <<= 1, p = p * p % n){
if (key & a){
r = r * p % n;
}
}
return r;
}
int GCD(int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
Output:
As per the output elapsed time is nan
I did not include GCD, Decryption__k, and Decryption function here because I did not implement paralization there.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论