📄 gost.txt
字号:
数据结构与算法数据结构与算法
GOST源代码
版权所有
// Gost.h: interface for the CGost class.
// 数据吞吐量 473.5K/S
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_GOST_H__73DCE164_439C_42B7_BB89_6120799F2F8B__INCLUDED_)
#define AFX_GOST_H__73DCE164_439C_42B7_BB89_6120799F2F8B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "windows.h"
class CGost
{
public:
DWORDLONG Encrypt(DWORDLONG);
DWORDLONG Decrypt(DWORDLONG);
void SetPwd(char *,int);
CGost();
virtual ~CGost();
private:
DWORDLONG temp;
void G_S();
DWORDLONG Key[32];
DWORDLONG TS[8][16];
static DWORDLONG S[128];
};
#endif // !defined(AFX_GOST_H__73DCE164_439C_42B7_BB89_6120799F2F8B__INCLUDED_)
// Gost.cpp: implementation of the CGost class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Gost.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGost::CGost():temp(0)
{
G_S();
}
CGost::~CGost()
{
}
void CGost::G_S()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<16;j++)
{
TS[i][j]=S[i*16+j];
}
}
}
void CGost::SetPwd(char *in,int len)
{
int i,j=0;
DWORDLONG temp;
DWORDLONG k[8]={0};
for(i=0;i<8;i++)
{
temp=0;
for(j=0;j<8;j++)
{
temp=temp | ((DWORDLONG)(*(in+i*8+j))<<j*8);
}
k[i]=temp;
}
for(i=0;i<8;i++)
{
Key[i]=k[i];
Key[8+i]=k[i];
Key[16+i]=k[i];
Key[31-i]=k[i];
}
// OK
}
DWORDLONG CGost::Encrypt(DWORDLONG in)
{
DWORDLONG L[33],R[33];
L[0]=in & 0xffffffff;
R[0]=in >> 32;
for(int i=0;i<32;i++)
{
temp^=temp;
L[i+1]=R[i];
R[i]=R[i]+Key[i];
R[i]=R[i] & 0xffffffff;
// S盒置换
for(int j=0;j<8;j++)
{
int m=(int)(R[i]&0xf);
temp=temp|(TS[j][m]<<j*4);
}
// 循环左移
R[i]^=R[i];
R[i]=((temp & 0x7ff)<<21) | ((temp & 0xfffff800)>>11);
// 异或
R[i+1]=R[i]^L[i];
}
temp^=temp;
// temp=L[32] | (R[32]<<32);
temp=(L[32]<<32) | R[32];
return temp;
}
DWORDLONG CGost::Decrypt(DWORDLONG in)
{
DWORDLONG L[33],R[33];
L[0]=in & 0xffffffff;
R[0]=in >> 32;
for(int i=0;i<32;i++)
{
temp^=temp;
L[i+1]=R[i];
R[i]=R[i]+Key[31-i];
R[i]=R[i] & 0xffffffff;
// S盒置换
for(int j=0;j<8;j++)
{
int m=(int)(R[i]&0xf);
temp=temp|(TS[j][m]<<j*4);
}
// 循环左移
R[i]^=R[i];
R[i]=((temp & 0x7ff)<<21) | ((temp & 0xfffff800)>>11);
// 异或
R[i+1]=R[i]^L[i];
}
temp^=temp;
// temp=L[32] | (R[32]<<32);
temp=(L[32]<<32) | R[32];
return temp;
}
DWORDLONG CGost::S[128]={
4,10,9,2,13,8,0,14,6,11,1,12,7,15,5,3,
14,11,4,12,6,13,15,10,2,3,8,1,0,7,5,9,
5,8,1,13,10,3,4,2,14,15,12,7,6,0,9,11,
7,13,10,1,0,8,9,15,14,4,6,12,11,2,5,3,
6,12,7,1,5,15,13,8,4,10,9,14,0,3,11,2,
4,11,10,0,7,2,1,13,3,6,8,5,9,12,15,14,
13,11,4,1,3,15,5,9,0,10,14,7,6,8,2,12,
1,15,13,0,5,7,10,4,9,2,3,14,6,11,8,12};
本站由 WWW 工作室建立 2000.1.14
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -