设为首页 收藏本站
查看: 567|回复: 0

[经验分享] Diffie-Hellman Key Exchange (DH)源代码

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-11 10:41:10 | 显示全部楼层 |阅读模式
see also:http://www.example-code.com/vcpp/dh_key_exchange.asp#include <CkDh.h>
#include <CkCrypt2.h>
void ChilkatSample(void)
{
//  Create two separate instances of the DH object.
CkDh dhBob;
CkDh dhAlice;
bool success;
//  Unlock the component once at program startup...
success = dhBob.UnlockComponent("Anything for 30-day trial");
if (success != true) {
printf("%s\n",dhBob.lastErrorText());
return;
}
//  The DH algorithm begins with a large prime, P, and a generator, G.
//  These don't have to be secret, and they may be transmitted over an insecure channel.
//  The generator is a small integer and typically has the value 2 or 5.
//  The Chilkat DH component provides the ability to use known
//  "safe" primes, as well as a method to generate new safe primes.
//  This example will use a known safe prime.  Generating
//  new safe primes is a time-consuming CPU intensive task
//  and is normally done offline.
//  Bob will choose to use the 2nd of our 8 pre-chosen safe primes.
//  It is the Prime for the 2nd Oakley Group (RFC 2409) --
//  1024-bit MODP Group.  Generator is 2.
//  The prime is: 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 }
dhBob.UseKnownPrime(2);
//  The computed shared secret will be equal to the size of the prime (in bits).
//  In this case the prime is 1024 bits, so the shared secret will be 128 bytes (128 * 8 = 1024).
//  However, the result is returned as an SSH1-encoded bignum in hex string format.
//  The SSH1-encoding prepends a 2-byte count, so the result is going  to be 2 bytes
//  longer: 130 bytes.  This results in a hex string that is 260 characters long (two chars
//  per byte for the hex encoding).
const char * p;
long g;
//  Bob will now send P and G to Alice.
p = dhBob.p();
g = dhBob.get_G();
//  Alice calls SetPG to set P and G.  SetPG checks
//  the values to make sure it's a safe prime and will
//  return false if not.
success = dhAlice.SetPG(p,g);
if (success != true) {
printf("P is not a safe prime\n");
return;
}
//  Each side begins by generating an "E"
//  value.  The CreateE method has one argument: numBits.
//  It should be set to twice the size of the number of bits
//  in the session key.
//  Let's say we want to generate a 128-bit session key
//  for AES encryption.  The shared secret generated by the Diffie-Hellman
//  algorithm will be longer, so we'll hash the result to arrive at the
//  desired session key length.  However, the length of the session
//  key we'll utlimately produce determines the value that should be
//  passed to the CreateE method.
//  In this case, we'll be creating a 128-bit session key, so pass 256 to CreateE.
//  This setting is for security purposes only -- the value
//  passed to CreateE does not change the length of the shared secret
//  that is produced by Diffie-Hellman.
//  Also, there is no need to pass in a value larger
//  than 2 times the expected session key length.  It suffices to
//  pass exactly 2 times the session key length.
//  Bob generates a random E (which has the mathematical
//  properties required for DH).
const char * eBob;
eBob = dhBob.createE(256);
//  Alice does the same:
const char * eAlice;
eAlice = dhAlice.createE(256);
//  The "E" values are sent over the insecure channel.
//  Bob sends his "E" to Alice, and Alice sends her "E" to Bob.
//  Each side computes the shared secret by calling FindK.
//  "K" is the shared-secret.
const char * kBob;
const char * kAlice;
//  Bob computes the shared secret from Alice's "E":
kBob = dhBob.findK(eAlice);
//  Alice computes the shared secret from Bob's "E":
kAlice = dhAlice.findK(eBob);
//  Amazingly, kBob and kAlice are identical and the expected
//  length (260 characters).  The strings contain the hex encoded bytes of
//  our shared secret:
printf("Bob's shared secret:\n");
printf("%s\n",kBob);
printf("Alice's shared secret (should be equal to Bob's)\n");
printf("%s\n",kAlice);
//  To arrive at a 128-bit session key for AES encryption, Bob and Alice should
//  both transform the raw shared secret using a hash algorithm that produces
//  the size of session key desired.   MD5 produces a 16-byte (128-bit) result, so
//  this is a good choice for 128-bit AES.
//  Here's how you would use Chilkat Crypt (a separate Chilkat component) to
//  produce the session key:
CkCrypt2 crypt;
success = crypt.UnlockComponent("Anything for 30-day trial.");
if (success != true) {
printf("%s\n",crypt.lastErrorText());
return;
}
crypt.put_EncodingMode("hex");
crypt.put_HashAlgorithm("md5");
const char * sessionKey;
sessionKey = crypt.hashStringENC(kBob);
printf("128-bit Session Key:\n");
printf("%s\n",sessionKey);
//  Encrypt something...
crypt.put_CryptAlgorithm("aes");
crypt.put_KeyLength(128);
crypt.put_CipherMode("cbc");
//  Use an IV that is the MD5 hash of the session key...
const char * iv;
iv = crypt.hashStringENC(sessionKey);
//  AES uses a 16-byte IV:
printf("Initialization Vector:\n");
printf("%s\n",iv);
crypt.SetEncodedKey(sessionKey,"hex");
crypt.SetEncodedIV(iv,"hex");
//  Encrypt some text:
const char * cipherText64;
crypt.put_EncodingMode("base64");
cipherText64 = crypt.encryptStringENC("The quick brown fox jumps over the lazy dog");
printf("%s\n",cipherText64);
const char * plainText;
plainText = crypt.decryptStringENC(cipherText64);
printf("%s\n",plainText);
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-112233-1-1.html 上篇帖子: .net中调用exchange服务器发邮件 下篇帖子: SAP Exchange Infrastructure POINTS
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表