将 OpenMP 用于 XOR 密码时的加扰输出
Scrambled output when using OpenMP for XOR cipher
我在 C 中有下面的代码,它是一个简单的 XOR 密码,我正在尝试使用 OpenMP 对其进行并行化。我添加了一个简单的 'for' 指令,它会导致输出混乱。如果有人能帮助我理解为什么会这样,我将不胜感激。
#include <stdio.h>
#include <string.h>
#include "omp.h"
void encryptDecrypt(char inpString[])
{
// Define XOR key
// Any character value will work
char xorKey = 'P';
// calculate length of input string
int len = strlen(inpString);
// perform XOR operation of key
// with every caracter in string
#pragma omp parallel for
for (int i = 0; i < len; i++)
{
inpString[i] = inpString[i] ^ xorKey;
printf("%c",inpString[i]);
}
}
// Driver program to test above function
int main()
{
char sampleString[] = "GeeksforGeeks";
// Encrypt the string
printf("Encrypted String: ");
encryptDecrypt(sampleString);
printf("\n");
// Decrypt the string
printf("Decrypted String: ");
encryptDecrypt(sampleString);
printf("\n");
return 0;
}
没有 OpenMP 的示例输出:
Encrypted String: 55;#6?"55;#
Decrypted String: GeeksforGeeks
使用 OpenMP 的示例输出:
Encrypted String: 5##6?"55;;5
Decrypted String: GesfoGeresekk
OpenMP 线程不会运行任何特定的顺序,除非您明确要求它们这样做。
你或许应该从循环中删除 printf
并在循环执行后(所有线程都已执行)将其放在外面,然后打印字符串以获得确定性输出。
有趣的是,您的字符串包含正确的加密和解密值。只是打印顺序不对
[..]
#pragma omp parallel for
for (int i = 0; i < len; i++)
{
inpString[i] = inpString[i] ^ xorKey;
// printf("%c",inpString[i]); Remove this
}
[..]
// Encrypt the string
printf("Encrypted String: ");
encryptDecrypt(sampleString);
printf("%s\n", sampleString); // Add print here
[..]
"Geeks"让你抢一个小点(其实那里准备了--2--个小点...)
#pragma omp parallel for
-block 明显是用来迷惑 omp
-新生的花招,但还有另一个惊喜,Mohit 用这些话表达了:
Interestingly your string contains the correct encrypted and decrypted value.
然而,
修改字符串的部分是干净的,实际上什么都没有"interesting"那里(非常低效,是的,但完全合法)。它是对 sampleString[]
disjunct char
值的有序、互不交互、简单 [i]
对齐、直接修改,但试图得到所有那些 printf()
-s 产生的个体 (char)
-s 把 "back" 放在一行中,在一个干净且人类可读的序列中是第一个斑点(这是显而易见的)。
The program ( inside the live-analyser-debugger-IDE ) stdout
但是读起来好像第一个(正确排序的)(char)
-s 在“Ge 中一定是相同的...forGe..."(以便产生同样的 XOR
-ed 输出 (char)
- s “55...55...” 在 XOR
修改后的字符串中,它们不是必须的吗?
[13]: -->GeeksforGeeks<--
NEXT: will encrypt the original String:
[13]: -->55;#6?"55;#<--
[13]: -->55;#6?"55;#<--
NEXT: will decrypt the (now) encrypted String:
[13]: -->GeeksforGeeks<--
[13]: -->55;#6?"55;#<--
XOR
-ed 版画看起来也有点像 "shorter",不是吗?
哎呀他们这样做 是有原因的 :o) 。 . . . . . . . . . . . . . . . . . . . . . . .迷惑你
[37]: -->GagarinGonnaGroomGreetingsGospelGoers<--
NEXT: will encrypt the original String:
[37]: -->171"9>?>>1"??="55>7#?# 5<?5"#<--
NEXT: will encrypt the original String:
[37]: -->171"9>?>>1"??="55>7#?# 5<?5"#<--
[37]: -->171"9>?>>1"??="55>7#?# 5<?5"#<--
NEXT: will decrypt the (now) encrypted String:
[37]: -->GagarinGonnaGroomGreetingsGospelGoers<--
[37]: -->171"9>?>>1"??="55>7#?# 5<?5"#<--
Platform's sizeof(char) was 1
请随意阅读上面实时 IDE 中的 line 49
,并可能会进一步试验此 Double-Speck
我在 C 中有下面的代码,它是一个简单的 XOR 密码,我正在尝试使用 OpenMP 对其进行并行化。我添加了一个简单的 'for' 指令,它会导致输出混乱。如果有人能帮助我理解为什么会这样,我将不胜感激。
#include <stdio.h>
#include <string.h>
#include "omp.h"
void encryptDecrypt(char inpString[])
{
// Define XOR key
// Any character value will work
char xorKey = 'P';
// calculate length of input string
int len = strlen(inpString);
// perform XOR operation of key
// with every caracter in string
#pragma omp parallel for
for (int i = 0; i < len; i++)
{
inpString[i] = inpString[i] ^ xorKey;
printf("%c",inpString[i]);
}
}
// Driver program to test above function
int main()
{
char sampleString[] = "GeeksforGeeks";
// Encrypt the string
printf("Encrypted String: ");
encryptDecrypt(sampleString);
printf("\n");
// Decrypt the string
printf("Decrypted String: ");
encryptDecrypt(sampleString);
printf("\n");
return 0;
}
没有 OpenMP 的示例输出:
Encrypted String: 55;#6?"55;#
Decrypted String: GeeksforGeeks
使用 OpenMP 的示例输出:
Encrypted String: 5##6?"55;;5
Decrypted String: GesfoGeresekk
OpenMP 线程不会运行任何特定的顺序,除非您明确要求它们这样做。
你或许应该从循环中删除 printf
并在循环执行后(所有线程都已执行)将其放在外面,然后打印字符串以获得确定性输出。
有趣的是,您的字符串包含正确的加密和解密值。只是打印顺序不对
[..]
#pragma omp parallel for
for (int i = 0; i < len; i++)
{
inpString[i] = inpString[i] ^ xorKey;
// printf("%c",inpString[i]); Remove this
}
[..]
// Encrypt the string
printf("Encrypted String: ");
encryptDecrypt(sampleString);
printf("%s\n", sampleString); // Add print here
[..]
"Geeks"让你抢一个小点(其实那里准备了--2--个小点...)
#pragma omp parallel for
-block 明显是用来迷惑 omp
-新生的花招,但还有另一个惊喜,Mohit 用这些话表达了:
Interestingly your string contains the correct encrypted and decrypted value.
然而,
修改字符串的部分是干净的,实际上什么都没有"interesting"那里(非常低效,是的,但完全合法)。它是对 sampleString[]
disjunct char
值的有序、互不交互、简单 [i]
对齐、直接修改,但试图得到所有那些 printf()
-s 产生的个体 (char)
-s 把 "back" 放在一行中,在一个干净且人类可读的序列中是第一个斑点(这是显而易见的)。
The program ( inside the live-analyser-debugger-IDE ) stdout
但是读起来好像第一个(正确排序的)(char)
-s 在“Ge 中一定是相同的...forGe..."(以便产生同样的 XOR
-ed 输出 (char)
- s “55...55...” 在 XOR
修改后的字符串中,它们不是必须的吗?
[13]: -->GeeksforGeeks<--
NEXT: will encrypt the original String:
[13]: -->55;#6?"55;#<--
[13]: -->55;#6?"55;#<--
NEXT: will decrypt the (now) encrypted String:
[13]: -->GeeksforGeeks<--
[13]: -->55;#6?"55;#<--
XOR
-ed 版画看起来也有点像 "shorter",不是吗?
哎呀他们这样做 是有原因的 :o) 。 . . . . . . . . . . . . . . . . . . . . . . .迷惑你
[37]: -->GagarinGonnaGroomGreetingsGospelGoers<--
NEXT: will encrypt the original String:
[37]: -->171"9>?>>1"??="55>7#?# 5<?5"#<--
NEXT: will encrypt the original String:
[37]: -->171"9>?>>1"??="55>7#?# 5<?5"#<--
[37]: -->171"9>?>>1"??="55>7#?# 5<?5"#<--
NEXT: will decrypt the (now) encrypted String:
[37]: -->GagarinGonnaGroomGreetingsGospelGoers<--
[37]: -->171"9>?>>1"??="55>7#?# 5<?5"#<--
Platform's sizeof(char) was 1
请随意阅读上面实时 IDE 中的 line 49
,并可能会进一步试验此 Double-Speck