• 01 生成公钥和私钥

    /

    01 生成公钥和私钥

    #include <botan/auto_rng.h>
    <!--more-->
    #include <botan/rsa.h>
    #include <botan/pkcs8.h>
    #include <botan/x509_key.h>

    #include <iostream>

    int main() {
    try {
    Botan::AutoSeeded_RNG rng;

    // 生成 RSA 密钥对
    Botan::RSA_PrivateKey private_key(rng, 2048); // 2048 是密钥位数,你可以根据需要调整
    Botan::RSA_PublicKey public_key = private_key;

    // 输出公钥和私钥
    std::cout << "Public Key: " << Botan::X509::PEM_encode(public_key) << std::endl;
    std::cout << "Private Key: " << Botan::PKCS8::PEM_encode(private_key) << std::endl;

    }
    catch (std::exception& e) {
    std::cerr << "Error: " << e.what() << std::endl;
    return 1;
    }

    return 0;
    }

  • 02 保存公钥和私钥

    /

    02 保存公钥和私钥

    #include <botan/auto_rng.h>
    <!--more-->
    #include <botan/rsa.h>
    #include <botan/pkcs8.h>
    #include <botan/x509_key.h>
    #include <iostream>
    #include <fstream>

    void save_key_to_file(const std::string& filename, const std::string& key_data) {
    std::ofstream file(filename);
    if (file.is_open()) {
    file << key_data;
    file.close();
    std::cout << "Key saved to file: " << filename << std::endl;
    }
    else {
    throw std::runtime_error("Unable to open file for writing: " + filename);
    }
    }

    int main() {
    try {
    Botan::AutoSeeded_RNG rng;

    // 生成 RSA 密钥对
    Botan::RSA_PrivateKey private_key(rng, 2048); // 2048 是密钥位数,你可以根据需要调整
    Botan::RSA_PublicKey public_key = private_key;

    // 将公钥和私钥编码为 PEM 格式
    std::string public_key_pem = Botan::X509::PEM_encode(public_key);
    std::string private_key_pem = Botan::PKCS8::PEM_encode(private_key);

    // 保存公钥和私钥到文件
    save_key_to_file("public_key.pem", public_key_pem);
    save_key_to_file("private_key.pem", private_key_pem);

    }
    catch (std::exception& e) {
    std::cerr << "Error: " << e.what() << std::endl;
    return 1;
    }

    return 0;
    }

  • 1-12-2 创建自己的异常类

    1.12-2 创建自己的异常类

    #include <iostream>
    <!--more-->
    #include <exception>
    using namespace std;
    class MyClass
    {
    class Myexception : public logic_error
    {
    public:
    Myexception(const char *s) : logic_error(s) {}
    };

    public:
    void ThrowExcept() throw(Myexception)
    {
    throw Myexception("my exception");
    }
    };

    int main()
    {
    try
    {
    MyClass mc;
    mc.ThrowExcept();
    }
    catch (const std::exception &e)
    {
    std::cerr << e.what() << '\n';
    }
    }
  • 1-12-5 异常恢复

    1.12-5 异常恢复

    #include <iostream>
    <!--more-->
    #include <exception>
    using namespace std;

    class newBad
    {
    static int i;
    static void *p;

    public:
    void *operator new(size_t sz)
    {
    p = ::operator new(sz);
    cout << "The newBad object number: " << i + 1 << " has been created" << endl;
    if (++i >= 11)
    {
    throw out_of_range("the 11st object created failed.");
    }
    return p;
    }

    static void releaseMemory()
    {
    if (p)
    {
    ::operator delete(p);
    cout << "the " << i << "st object was freed" << endl;
    i--;
    }
    }
    };

    int newBad::i = 0;
    void *newBad::p = nullptr;

    int main(int argc, char const *argv[])
    {
    while (true)
    {
    try
    {
    newBad *mp = new newBad;
    }
    catch (const std::exception &e)
    {
    std::cerr << e.what() << '\n';
    newBad::releaseMemory();
    }
    }
    return 0;
    }

  • 1-12-6 栈反解时再次异常

    1.12-6 栈反解时再次异常

    #include <iostream>
    <!--more-->
    #include <exception>
    using namespace std;

    class BadIdea
    {
    public:
    ~BadIdea()
    {
    throw logic_error("This is a bad exception");
    }
    };

    void my_terminate()
    {
    cout << "This is a bad Idea" << endl;
    exit(0);
    }
    void *psource = (void *)set_terminate(my_terminate);

    int main(int argc, char const *argv[])
    {
    try
    {
    BadIdea ba;
    throw logic_error("test");
    }
    catch (const std::exception &e)
    {
    std::cerr << e.what() << '\n';
    }

    return 0;
    }

  • 1-12-7 异常正常析构的证明

    1.12-7 异常正常析构的证明

    #include <iostream>
    <!--more-->
    #include <exception>

    using namespace std;
    class MyException : public logic_error
    {
    public:
    MyException(const char *s) : logic_error(s) {}
    ~MyException() { cout << "the Exception has been destructed." << endl; }
    };
    class test
    {
    public:
    void throwException() { throw MyException("hello, this is MyException."); }
    };

    int main(int argc, char const *argv[])
    {
    try
    {
    test t;
    t.throwException();
    }
    catch (const std::exception &e)
    {
    std::cerr << e.what() << '\n';
    }

    return 0;
    }

  • 3-7-1 逆转字符串

    3.7-1 逆转字符串

    #include <string>
    <!--more-->
    #include <iostream>
    using namespace std;

    void reverse(string &s)
    {
    int size = s.size();
    for (size_t i = 0; i < size / 2; i++)
    {
    char temp = s[i];
    s[i] = s[size - 1 - i];
    s[size - 1 - i] = temp;
    }
    }
    int main()
    {
    string s("123456");
    cout << s << endl;
    reverse(s);
    cout << s << endl;
    }