我正在编写一个基于位/字节移位加密的程序。该程序当前接收文件,显示内容,然后显示十六进制值。
要加密文件,我想要执行位/字节移位。但是,我将文件加载到一个数组中,并通过使用临时交换执行转换。我很确定结果是一样的,但我想用适当的位移来实现它。
为了产生相同的结果,我只是说加载[x>> 3]来移位这些位?我试图实现这个目标的结果很糟糕。
下面是整个代码块,最后有一个示例输出。提前感谢大家的时间。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
FILE *fp;
char ch;
char load[100];
int count1 = 0;
int count2 = 0;
int i = 0;
fp = fopen("load.txt", "r");
if (fp == NULL)
{
perror("Exiting, an error occured while opening\n");
exit(EXIT_FAILURE);
}
while((ch = fgetc(fp)) != EOF)
{
printf("%c", ch);
++count1;
}
fclose(fp);
fp = fopen("load.txt", "r");
if (fp == NULL)
{
perror("Exiting, an error occured while opening\n");
exit(EXIT_FAILURE);
}
printf("\n\n");
while((ch = fgetc(fp)) != EOF)
{
printf("%x ", ch);
++count2;
}
fclose(fp);
printf("\n\n");
printf("Count1: %d, Count2: %d \n", count1, count2);
printf("\n\n");
fp = fopen("load.txt", "r");
if (fp == NULL)
{
perror("Exiting, an error occured while opening\n");
exit(EXIT_FAILURE);
}
i = 0;
while((ch = fgetc(fp)) != EOF)
{
load[i++] = ch;
}
load[i] = 0;
fclose(fp);
// for(i = 0; i < 100; ++i)
// {
// printf("%s", load);
// }
printf("\n\n");
for(i = 0; i < 50; ++i)
{
printf("%x ", load[i]);
}
// printf("\n index i: %c\n", load[0]);
//shift bytes
char temp, temp2, temp3, temp4;
temp = load[7];
load[7] = load[11];
load[11] = temp;
temp2 = load[8];
load[8] = load[12];
load[12] = temp2;
temp3 = load[9];
load[9] = load[13];
load[13] = temp3;
temp4 = load[10];
load[10] = load[14];
load[14] = temp4;
printf("\n\n");
for(i = 0; i < 50; ++i)
{
printf("%x ", load[i]);
}
printf("\nAfter byte shift: \n %s", load);
//shift bites
temp = load[6];
load[6] = load[4];
load[4] = temp;
temp2 = load[5];
load[5] = load[3];
load[3] = temp2;
printf("\nAfter bit shift: \n %s", load);
printf("\n\n");
for(i = 0; i < 50; ++i)
{
printf("%x ", load[i]);
}
printf("\n\n");
return 0;
}
快速的棕色狐狸跳过懒狗。
54 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 66 6f 78 20 6a 75 6d 70 65 64 20 6f 76 65 72 20 74 68 65 20 6c 61 7a 79 20 64 6f 67 2e a < / p>
伯爵1:46,伯爵2:46
54 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 66 6f 78 20 6a 75 6d 70 65 64 20 6f 76 65 72 20 74 68 65 20 6c 61 7a 79 20 64 6f 67 2e a 0 0 0 0
54 68 65 20 71 75 69 72 6f 77 6e 63 6b 20 62 20 66 6f 78 20 6a 75 6d 70 65 64 20 6f 76 65 72 20 74 68 65 20 6c 61 7a 79 20 64 6f 67 2e a 0 0 0 0
字节移位后: quirownck b fox跳过懒狗。
比特移位后: Theui qrownck b fox跳过懒狗。
54 68 65 75 69 20 71 72 6f 77 6e 63 6b 20 62 20 66 6f 78 20 6a 75 6d 70 65 64 20 6f 76 65 72 20 74 68 65 20 6c 61 7a 79 20 64 6f 67 2e a 0 0 0 0