資源簡介
ELGamal加解密(c語言實現)
ELGamal是非對稱加密算法,和RSA類似
ELGamal密碼體制是T.ElGamal在1985年提出的公鑰密碼體制。

代碼片段和文件信息
#include?
#include?
#include?“elgamal.h“
#include?
using?namespace?std;
//!?宏定義,固定素數P
#define?PRIME_P 9871
//!?宏定義,固定本原元素
#define?PRIME_G 7527
//!?宏定義,加密隨機值
#define?RANDOM 1795
/******************************************************************************/
// 名稱:IsNumber
// 功能:判斷數字字符數組
//??參數:strNumber:字符數組
// 返回:數字字組數組返回true,否則返回false;
/******************************************************************************/
bool?IsNumber(?const?char?*strNumber?)
{
int?i;
if(?!strNumber?)
return?false;
for?(?i?=?0?;?i? {
if?(?strNumber[i]?‘0‘?||?strNumber[i]?>?‘9‘?)
return?false;
}
return?true;
}
/******************************************************************************/
// 名稱:PublicKey
// 功能:由私鑰生成公鑰
//??參數:xKey:私鑰X(整數)
// 返回:公鑰yKey(整數)
//??備注:固定素數P與本原元素
/******************************************************************************/
unsigned?int?PublicKey(?unsigned?int?xKey?)
{
unsigned?int?yKey;
//!?私鑰取值應在區間[0?PRIME_P)
if(?xKey?>=?PRIME_P?)
return?0;
yKey?=?PRIME_G;
if(?xKey?!=?0?)
{
for(?unsigned?int?i=1;?i yKey?=?(?yKey?*?PRIME_G)?%?PRIME_P;
}
else
yKey?=?1;
return?yKey;
}
/******************************************************************************/
// 名稱:ELGamal_encrypt
// 功能:加密
//??參數:yKey:加密公鑰;mw:指向明文緩沖區;clw:密文輸出c1;c2w:密文輸出c2(保存密文)
// 返回:加密成功返回true,否則返回false
//??備注:固定素數P、本原元素和加密隨機值
/******************************************************************************/
bool?ELGamal_encrypt(?int?yKey?char?*?mw?int?&c1w?int?*&c2w?)
{
int?i;
std::string?mTem(mw);
int?mLong?=?(int)mTem.length();
char?*m?=?new?char[mLong];
for(?i?=?0;?i? m[i]?=?mTem.at(i);
if(?!(?RANDOM>0?&&?RANDOM return?false;
int?c1?=?PRIME_G;
int?k?=?yKey;
for(?i?=?1;?i? {
c1?=?(?c1?*?PRIME_G?)?%?PRIME_P;
k?=?(?k?*?yKey?)?%?PRIME_P;
}
for(?i?=?0;?i? c2w[i]?=?(?m[i]?*?k?)?%?PRIME_P;
c1w?=?c1;
//!?別忘記釋放內存空間
delete?[]?m;
return?true;
}
/******************************************************************************/
// 名稱:ELGamal_decrypt
// 功能:解密
//??參數:xKey:解密私鑰;cw:指向密文緩沖區;clw:密文輸出;c2w:密文輸出
// ??mw:指向明文緩沖區;cLong:密文c2中數據段(以“”間隔)
// 返回:解密成功返回true,否則返回false
//??備注:固定素數P和本原元素
/******************************************************************************/
bool?ELGamal_decrypt(?int?xKey?int?c1w?int?*c2w?char?*mw?int?cLong?)
{
int?kb?=?c1w;
if?(?xKey?!=?0?)
{
for(?int?i?=?1;?i? kb?=?(?kb?*?c1w?)?%?PRIME_P;
}
else
{
kb?=?1;
}
int?kReciprocal?=?1;
int?rslt?=?0;
for?(?kReciprocal;?rslt?!=?1;?kReciprocal++?)
{
rslt?=?(?kb?*?kReciprocal?)?%?PRIME_P;//求k的倒數kReciprocal
}
kReciprocal--;
for(?int?i?=?0;?i? {
mw[i]?=?(?kReciprocal?*?c2w[i]?)?%?PRIME_P;
}
return?true;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-12-06?23:57??ELGamal\
?????文件????????3253??2009-12-10?18:55??ELGamal\ELGamal.cpp
?????文件?????????258??2009-12-10?18:58??ELGamal\ELGamal.h
?????文件????????4800??2009-12-10?18:56??ELGamal\main.cpp
?????文件????????1494??2016-12-10?18:17??ELGamal.txt
- 上一篇:RSA加解密c語言實現.zip
- 下一篇:delphi經典編程入門
評論
共有 條評論