Posted on 2005-09-22 11:22 Panic 阅读(10577)
评论(36) 编辑 收藏
RSA相关代码,包含无符号大整数运算的实现。
代码说明:
大数运算仅为RSA算法设计,使用了固定数组作为元素存储的单元。
RSA中不需要使用的算法,本代码都没有实现。
大数运算中,部分代码出于效率的原因,使用了内联汇编。
RSA代码中,仅提供了数学实现,没有对密钥存储等细节作进一步封装。
注意,程序中默认定义的数组长度:
enum LIMIT{ LENGTH = 0xFF,SMALLPRIMES = 5000};
只有255,这个长度只能处理4080bit的密钥,也就是每个质数的长度不能超过2040bit,需要处理更大的密钥请自行修改这个定义的数值。
新上传的版本,这个数值已经改为0x3FF,这样每个质数的大小就达到8184bit,足够满足一般的应用了。
有任何疑问请到本博客留言或跟帖。
VC6.0工程源代码下载:
http://blog.vckbase.com/Files/Panic/rsa.rar
Feedback
# re: RSA相关代码,包含无符号大整数运算的实现。
2005-09-22 11:38 by
多谢啊,马上试用
# re: RSA相关代码,包含无符号大整数运算的实现。
2005-11-03 09:12 by
RSA代码没有问题,我调用此类完成了我需要的功能,调试通过。
这个程序在C++下编译没有问题,运行结果正确,可是我把完成好的程序交给我同学用MFC去做图形界面,编译就出现了问题,我们对MFC了解不多,能不能给点提示是怎么回事啊?
错误如下:
--------------------Configuration: image_encrypt - Win32 Debug--------------------
Compiling resources...
c:\program files\microsoft visual studio\vc98\include\new(35) : error C2061: syntax error : identifier 'THIS_FILE'
c:\program files\microsoft visual studio\vc98\include\new(35) : error C2091: function returns function
c:\program files\microsoft visual studio\vc98\include\new(35) : error C2809: 'operator new' has no formal parameters
c:\program files\microsoft visual studio\vc98\include\new(36) : error C2061: syntax error : identifier 'THIS_FILE'
c:\program files\microsoft visual studio\vc98\include\new(37) : error C2091: function returns function
c:\program files\microsoft visual studio\vc98\include\new(37) : error C2556: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,const struct std::nothrow_t &)' : overloaded function differs only by return type from 'void *(__cdecl *__cdecl op
erator new(void))(unsigned int)'
# re: zouzhenhua
2005-11-03 10:40 by
可能是MFC重载了operator new导致,建议去
www.vckbase.com/bbs
寻求答案。
# re: RSA相关代码,包含无符号大整数运算的实现。
2005-11-05 22:43 by
看到你的留言就来下你的代码了,确实解决了存储问题,而且比我那个考虑的周全许多.
# re: bluefish
2005-11-05 22:57 by
呵呵,过奖,这个代码仅为效率而设计,细节并不是很完整,需要的话还得自己修改。
# re: RSA相关代码,包含无符号大整数运算的实现。
2005-11-23 21:14 by
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-03-01 23:09 by
有没有dsa的实现代码,如果有,非常感谢!
看了你写的rsa的代码,我受益非浅
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-03-01 23:27 by
pAnic大哥,你好!
你那里有没有完整的largeint类的源代码,能否实现如Java中的
BigInteger类型的大整数,最好是通过运算符重载得到的。我自己想写这样一个类,但写不出来,你能否写一个。
# re:wxd
2006-03-03 17:20 by
你可以搜索一下,网上有很多现成的大整数运算类。
一般加密解密库的实现中都会附带这类代码。
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-03-03 20:26 by
有没有dsa算法实现的源代码
# re:wxd
2006-03-04 17:31 by
我上传了一个各种加密资源的压缩包,请点击
这里下载查看。
应该有你需要的资料。
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-03-13 20:25 by
pAnic:
你好!
我点击“这里”报找不到服务器的错?
# re: wxd
2006-03-14 09:22 by
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-03-14 22:14 by
pAnic:
谢谢你的帮助!有什么需要可以和我联系。
email: tcsnwxd@163.net
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-05-13 17:02 by
用代码的时候有-1个错误,提示如下:不知道为什么???请尽快回答。多谢了。
Linking...
RSA.obj : error LNK2001: unresolved external symbol "public: __thiscall panic::CLargeInt::~CLargeInt(void)" (??1CLargeInt@panic@@QAE@XZ)
RSA.obj : error LNK2001: unresolved external symbol "public: static void __cdecl panic::CLargeInt::RSADecode(class panic::CLargeInt const &,class panic::CLargeInt const &,class panic::CLargeInt const &,class panic::CLargeInt &)" (?RSADecode@CLargeIn
t@panic@@SAXABV12@00AAV12@@Z)
RSA.obj : error LNK2001: unresolved external symbol "public: static void __cdecl panic::CLargeInt::RSAEncode(class panic::CLargeInt const &,class panic::CLargeInt const &,class panic::CLargeInt const &,class panic::CLargeInt &)" (?RSAEncode@CLargeIn
t@panic@@SAXABV12@00AAV12@@Z)
RSA.obj : error LNK2001: unresolved external symbol "public: static bool __cdecl panic::CLargeInt::RSACreate(class panic::CLargeInt const &,class panic::CLargeInt const &,class panic::CLargeInt const &,class panic::CLargeInt &,class panic::CLargeInt
&)" (?RSACreate@CLargeInt@panic@@SA_NABV12@00AAV12@1@Z)
RSA.obj : error LNK2001: unresolved external symbol "public: static void __cdecl panic::CLargeInt::CreatePrime(class panic::CLargeInt &)" (?CreatePrime@CLargeInt@panic@@SAXAAV12@@Z)
RSA.obj : error LNK2001: unresolved external symbol "public: static void __cdecl panic::CLargeInt::CreateRandom(class panic::CLargeInt &,unsigned long)" (?CreateRandom@CLargeInt@panic@@SAXAAV12@K@Z)
RSA.obj : error LNK2001: unresolved external symbol "public: __thiscall panic::CLargeInt::CLargeInt(unsigned long)" (??0CLargeInt@panic@@QAE@K@Z)
Debug/RSA.exe : fatal error LNK1120: 7 unresolved externals
执行 link.exe 时出错.
RSA.exe - 1 error(s), 0 warning(s)
# re: bingdeng
2006-05-14 22:39 by
是否没有加:
using namespace panic;
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-05-15 20:56 by
pAnic:
你好,我看代码看不清楚,你能不能把大整数的运算的算法给我写写,好吗?先谢谢你了.你千万要给我说说啊,摆脱了.
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-06-01 19:35 by
您好!源代码运行没问题。可是我用字符串进行调试的时候,就出现问题了,提示程序遇到问题需要关闭,然后我点调试,弹出对话框:提示信息:(对话框)User breakpoint called from code at 0x401885.问题在: __asm int 3;这是怎么回事?谢谢了!
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-06-01 19:49 by
这里应该是缺少一段处理的代码,应该怎么写呢?谢谢啦!
# re: me
2006-06-04 15:55 by
可能是你的字符串格式不对。
代码现在只支持形如:
"0x1234567890ABCDEF"
的字符串。
如果需要支持其他格式,请自行修改CLargeInt的构造函数。
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-07-10 18:52 by
哈哈哈哈,不好意思,这代码看了很多天,只是了解了些皮毛。所以想在这里请教一下。
RSA加密里所说的1024 位加密、2048位加密,是不是只要
CLargeInt::CreateRandom(e,1024);
这句代码里的参数设置成1024、2048就行了?
大素数PQ,在CLargeInt::CreateRandom()函数里的参数,如何设置?
_data 和 _len 这两个变量到底起什么作用?
_data 占了4092个字节的大小,_len=64时,只输出了512个字节。
Encode M->C 和 Decode C->M 时,为什么有时“明文”和“密文”占用相同字节,而有时“密文”是“明文”
的一倍?
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-07-10 18:53 by
才疏学浅,希望不要见怪,先谢谢了。
# re: 1900
2006-07-10 21:18 by
RSA加密里所说的1024 位加密、2048位加密,是不是只要
CLargeInt::CreateRandom(e,1024);
这句代码里的参数设置成1024、2048就行了?
//不是的,2048bit,是指P*Q的积是2048bit,也就是说,当你P和Q都设置为1024的时候,加密强度是2048bit,事实上P和Q的位数可以不等,并且过分接近的P和Q将导致破解难度极大降低。
_data 和 _len 这两个变量到底起什么作用?
_data 占了4092个字节的大小,_len=64时,只输出了512个字节。
//_data和_len其实是模拟了一个动态数组,用来容纳长度不定的大整数。_data 占据的大小是预先设置好的,这个数值在LargeInt.h中有定义:
enum LIMIT{ LENGTH = 0x3FF,SMALLPRIMES = 5000};
LENGTH 可以根据需要修改。
_len是以4字节为单位的目前已经占用的数组的长度,_len = 64就是当前大整数的长度是64*4 = 256字节
输出的字节是以16进制表示的字符串,每个字节需要两个符号来表示,所以长度是256*2 = 512字节。
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-07-11 16:59 by
哦~ 原来是这样,太谢谢了!!!!
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-08-27 13:21 by
pAnic 你好,我又来请教问题了。
我在你这代码的基础上,加了一个MFC的界面。在Debug版本下,程序还一切正常。但是在Release版本下,
就会非法操作。
具体情况是,在我的程序在开始加密时,第一次调用void CLargeInt::RSAEncode( const CLargeInt
&n,const CLargeInt &d,const CLargeInt &m,CLargeInt &c)函数,然后在 void
CLargeInt::ExpMod(const CLargeInt& source,const CLargeInt& exponent,const CLargeInt&
modulo,CLargeInt& result)函数里
while( e._len > 1 || e._data[e._len - 1] > 1 )
{
if( e._data[0] &1 )
{
Mul(r,n,temp);
Div(temp,modulo,temp1,r);
}
RCR(e);
Mul(n,n,temp);
Div(temp,modulo,temp1,n); 《---调用此函数
}
在这个函数里void CLargeInt::Div(const CLargeInt& dividend,const CLargeInt& divisor,CLargeInt&
quotient,CLargeInt& residual)
CLargeInt d,r;
//满位算法
T_DWORD head = divisor._data[divisor._len-1];
__asm
{
pushad;
mov eax,head;
bsr ecx,eax;
mov edx,dword ptr 31;
sub edx,ecx;
mov ecx,edx;
mov eax,dword ptr 0x01;
sal eax,cl;
mov head,eax;
popad;
}
Mul(dividend,head,d);
Mul(divisor,head,r); 《-----调用这个函数 出错
程序运行时报非法操作,“Unhandled exception in 01.exe 0x00000005:Access Violation”点“调试”
,在VC里显示
0040431F mov eax,dword ptr[esi + ecx + 18h]
这一步。
由于CLargeInt::Mul()函数里,差不多全是汇编,而我对汇编不是太懂。所以想请教一下,这个问题该如
何解决。谢谢!
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-10 14:34 by
可能是你的字符串格式不对。
代码现在只支持形如:
"0x1234567890ABCDEF"
的字符串。
如果需要支持其他格式,请自行修改CLargeInt的构造函数。
我改了一下构造函数,但没有改好,可以帮帮忙吗?
比如我想对“111111”加密,只是把1当成普通从字符而不是当成一个数字
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-11 15:29 by
1的ACSII码是多少???
就用它了~~
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-11 18:21 by
1的ACSII码是多少???
就用它了~~
???
难道就用CLargeInt("0x313131313131");
这样不对呀
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-11 18:32 by
哈哈,谢谢1900了,就是用CLargeInt("0x313131313131");
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-13 00:10 by
又有问题要请教了,
哪位大哥可以把这个输出函数该一下嘛?改成返回字符串
void Print() const
{
}
改成:
char* toString() {
}
谢谢了
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-13 22:49 by
怎么没有人回答我的问题呀
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-14 22:27 by
没有人回答那我自己做一个:
//返回16进制字符串
string toString()
{
string s;
char r[257];
char buffer[8];
int j=0;
for( int i = _len-1 ; i >= 0; i--)
{
char str[] = "%08X";
str[2] = '0' + sizeof(T_DWORD) * 2;
if( i != (_len-1) )sprintf( buffer, str,_data[i]);
else sprintf( buffer, str,_data[i]);
s.append(buffer);
}
return s;
}
测试通过,呵呵。
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-14 22:27 by
char r[257];
int j=0;
是多余的:(
# re: RSA相关代码,包含无符号大整数运算的实现。
2006-09-23 20:10 by
那我的那个问题呢? 谁来帮我解决解决~~~~
# re: RSA相关代码,包含无符号大整数运算的实现。
2008-06-04 16:43 by
不知道panic能不能实现下欧几里德算法求解同余方程以及两个largeint之间的求模函数
# re: RSA相关代码,包含无符号大整数运算的实现。
2008-09-16 09:54 by