本帖最后由 Joduska 于 2019-7-4 10:34 编辑
[C] 纯文本查看 复制代码 使用方法
加密:OneBitOneCypher plaintext cyphertext password
解密:OneBitOneCypher cyphertext plaintext password
*/
#include <stdio.h>
#include <stdlib.h>
// struct stat 结构的头文件。
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
// 取明文/密文的文件大小。
struct stat statFileSize;
stat(argv[1], &statFileSize);
unsigned long ulFileSize = statFileSize.st_size;
unsigned char *pucPlaintext_Cyphertext = malloc(ulFileSize);
// 取明文/密文的文件描述符。
unsigned long ulPlaintext_CyphertextFD = open(argv[1], O_RDONLY, S_IRUSR | S_IWUSR);
unsigned long ulCyphertext_PlaintextFD = open(argv[2], O_CREAT | O_WRONLY, S_IREAD | S_IWRITE);
// read API。
read(ulPlaintext_CyphertextFD, pucPlaintext_Cyphertext, ulFileSize);
// 至少8个字符的密码(包括空格)
unsigned char aucKeyTable[256], aucPassword[16];
unsigned long i, j;
for(i = 0; i < 256; ++i)
{
Loop:
aucKeyTable[i] = rand() & 255;
for(j = 0; j < i; ++j)
{
if(aucKeyTable[j] == aucKeyTable[i])
{
goto Loop;
}
}
}
for(i = 0; argv[3][i]; ++i)
{
aucPassword[i] = argv[3][i];
}
srand(*((unsigned int*)aucPassword) % RAND_MAX);
for(j = 0; j < ulFileSize; j += 256)
{
unsigned long k, l, ulTablePosition;
for(k = 0; k < i; ++k)
{
ulTablePosition = aucPassword[k] % 256;
aucPassword[k] = aucKeyTable[ulTablePosition];
}
if(!(j /256 % 2))
{
unsigned long *pulKeySwap1 = (unsigned long*)aucKeyTable, *pulKeySwap2 = (unsigned long*)aucKeyTable, ulKeyTemp;
for(l = 0; l < 32; ++l)
{
ulTablePosition = aucPassword[l] % 32;
if(l != ulTablePosition)
{
ulKeyTemp = pulKeySwap1[l];
pulKeySwap1[l] = pulKeySwap2[ulTablePosition];
pulKeySwap2[ulTablePosition] = ulKeyTemp;
}
}
}
else
{
for(k = 0; k < 256; ++k)
{
ulTablePosition = rand() % 256;
if(k != ulTablePosition)
{
aucKeyTable[k] += aucKeyTable[ulTablePosition];
aucKeyTable[ulTablePosition] = aucKeyTable[k] - aucKeyTable[ulTablePosition];
aucKeyTable[k] -= aucKeyTable[ulTablePosition];
}
}
}
for(l = 0; l < 256; ++l)
{
pucPlaintext_Cyphertext[j + l] ^= aucKeyTable[l];
}
}
write(ulCyphertext_PlaintextFD, pucPlaintext_Cyphertext, ulFileSize);
return 0;
}
方法并不适用!只是分析出来给大家解密的!
|