7

你知道在 C++ 或 C 中演示公钥加密 (PPKE) 的教程吗?

我正在尝试了解它是如何工作的,并最终使用 Crypto++ 使用公共私钥创建我自己的加密。也许有一个 Crypto++ PPKE 教程?

也许有人可以解释公钥和私钥之间的关系(如果有的话)?谁能建议一些我可以使用的非常简单的公钥和私钥值(例如'char * 32','char / 32')来创建我的简单PPKE程序来理解这个概念?

4

2 回答 2

10

www.muppetlabs.com/~breadbox/txt/rsa.html

This article is very well written for programmers who want to understand RSA but do not have solid math background. It is the only article that actually makes me understand RSA. It doesn't contain C or C++ code but once you understand how it works, you should be able to write your own. (Even though I agree with others that it is not recommended, it should be still helpful to cleraly understand RSA) Hope it helps!

于 2012-04-04T05:25:06.297 回答
8

这是我一段时间前写的 RSA 的玩具版本。使它成为玩具的原因是它只使用 32 位数字。为了提供任何有意义的安全级别,您需要支持更大的数学数字(典型的密钥范围是 1024-4096 位左右,尽管后者可能没有多大作用)。

尽管如此,这确实实现了真正的 RSA 算法。插入 bignum 包所需的时间相对较少,因此此代码可以使用实际大小的 RSA 密钥(尽管大多数其他实现可能更快)。

#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <functional>

const int e_key = 47;
const int d_key = 15;
const int n = 391;

struct crypt : std::binary_function<int, int, int> {
    int operator()(int input, int key) const { 
        int result = 1;
        for (int i=0; i<key; i++) {
            result *= input;
            result %= n;
        }
        return result;
    }
};

int main() {
    std::string msg = "Drink more Ovaltine.";
    std::vector<int> encrypted;

    std::transform(msg.begin(), msg.end(),  
        std::back_inserter(encrypted),
        std::bind2nd(crypt(), e_key));

    std::transform(encrypted.begin(), encrypted.end(), 
        std::ostream_iterator<char>(std::cout, ""), 
        std::bind2nd(crypt(), d_key));
    std::cout << "\n";

    return 0;
}

当然,这仅涵盖加密和解密本身——距离成为一个完整的安全系统还有很长的路要走。

Much like the comments have noted, this is intended purely to support understanding of the algorithm. I've never put it to serious use, and probably never will. Although supporting real key sizes would be fairly trivial, it's open to question whether I'll ever even do that -- if I did, somebody might mistake it for something that should be used on real data, which I don't really intend.

于 2012-04-04T05:18:14.103 回答