这是一个基于均态分布随机数算法(非正态分布/高斯分布随机数算法)实现香农一位一密加解密的程序源码。
加解密速率很快,可以用于信息实时安全传输,估计超级计算机也破解不了。
这是本人改造/改良/改进的成果(一位一密是香农首创独创),如果你发现网上早有别人实现了同样的程序,请告知本人。
欢迎各路加解密精英高手破解,如果你破解了,也请告知本人,万分感谢!
因为你破解了,也就是破解了信息学/信息论鼻祖香农首创也是独创的一位一密加解密算法,你就是个天才。
我开源的目的就是要结识你这样的加解密天才。
这是在 macOS Mojave 下用 gcc 64位版本编译通过,Linux 和 BSD 都通用,Windows 下用 Visual Studio 2019 64位版本编译通过。
用法
加密:OneBitOneCipher plaintext ciphertext password
解密:OneBitOneCipher ciphertext plaintext password
如果排版不清楚,请下载附件 C 源码。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// 这个头文件 Windows 下不用
#include <unistd.h>
// Windows 下要用的头文件
//#include <io.h>
//#include <share.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);
// Windows 改用这里
/*
struct _stat statFileSize;
_stat(argv[1], &statFileSize);
unsigned int uiFileSize = statFileSize.st_size;
unsigned char *pucPlaintext_Cyphertext = (unsigned char*)malloc(uiFileSize);
*/
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);
// Windows 改用这里
/*
int iPlaintext_CyphertextFD, iCyphertext_PlaintextFD;
_sopen_s(&iPlaintext_CyphertextFD, argv[1], _O_RDONLY, _SH_DENYWR, _S_IREAD);
_sopen_s(&iCyphertext_PlaintextFD, argv[2], _O_CREAT | O_WRONLY, _SH_DENYNO, S_IWRITE);
*/
read(ulPlaintext_CyphertextFD, pucPlaintext_Cyphertext, ulFileSize);
// Windows 改用这里
/*
_read(iPlaintext_CyphertextFD, pucPlaintext_Cyphertext, uiFileSize);
*/
unsigned long i, j;
// 至少8个可打印字符的密码(包括空格),最多16个。
unsigned char aucPassword[16], aucKeyTable[256];
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 && j + l < ulFileSize; ++l)
{
pucPlaintext_Cyphertext[j + l] ^= aucKeyTable[l];
}
}
write(ulCyphertext_PlaintextFD, pucPlaintext_Cyphertext, ulFileSize);
close(ulCyphertext_PlaintextFD);
close(ulPlaintext_CyphertextFD);
// Windows 改用这里
/*
_write(iCyphertext_PlaintextFD, pucPlaintext_Cyphertext, uiFileSize);
_close(iCyphertext_PlaintextFD);
_close(iPlaintext_CyphertextFD);
*/
return 0;
}
[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界
最后于 2020-7-5 08:56
被zx_838914编辑
,原因: 更新内容