【问题标题】:Web assembly: Using crypto++ library with emscriptenWeb 程序集:将 crypto++ 库与 emscripten 一起使用
【发布时间】:2022-12-30 18:09:59
【问题描述】:

我计划将 crypto++ 库用于 Web 应用程序,而 wasm 似乎非常适合它。

我的 cpp 代码:

#include <string>
using std::string;


#include "cryptopp/cryptlib.h"
#include "cryptopp/rsa.h"
#include "cryptopp/files.h"
#include "cryptopp/osrng.h"

using CryptoPP::RSA;
using CryptoPP::InvertibleRSAFunction;
using CryptoPP::RSAES_OAEP_SHA_Encryptor;
using CryptoPP::RSAES_OAEP_SHA_Decryptor;
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::PK_EncryptorFilter;
using CryptoPP::PK_DecryptorFilter;
using CryptoPP::FileSink;
using CryptoPP::FileSource;
using CryptoPP::AutoSeededRandomPool;
using CryptoPP::DecodingResult;
using CryptoPP::SHA1;

using namespace CryptoPP;

extern "C" {
    char* generateKeys() {
        AutoSeededRandomPool rng;

        InvertibleRSAFunction parameters;
        parameters.GenerateRandomWithKeySize(rng, 1024);

        RSA::PrivateKey privateKey(parameters);
        RSA::PublicKey publicKey(parameters);
        string pubKey;
        publicKey.Save(StringSink(pubKey).Ref());
        privateKey.Save(FileSink("privkey.der", true).Ref());
        int n = pubKey.length();
        char* char_array = new char[n + 1];
        strcpy(char_array, pubKey.c_str());
        return char_array;
    }
}
extern "C" {
    char* encrypt(string pubKey, string plain) {
        AutoSeededRandomPool rng;
        string cipher;
        RSA::PublicKey publicKey;
        publicKey.Load(StringSource(pubKey, true, NULL).Ref());
        RSAES_OAEP_SHA_Encryptor e(publicKey);
        StringSource(plain, true,
            new PK_EncryptorFilter(rng, e,
                new StringSink(cipher)
            ) // PK_EncryptorFilter
        ); // StringSource
        int n = cipher.length();
        char* char_array = new char[n + 1];
        strcpy(char_array, cipher.c_str());
        return char_array;
    }
}

extern "C" {
    char* decrypt(const char* filename, string cipher) {
        AutoSeededRandomPool rng;
        RSA::PrivateKey privateKey;
        string recovered;
        privateKey.Load(FileSource(filename, true).Ref());

        RSAES_OAEP_SHA_Decryptor d(privateKey);

        StringSource(cipher, true,
            new PK_DecryptorFilter(rng, d,
                new StringSink(recovered)
            ) // PK_EncryptorFilter
        ); // StringSource
        int n = recovered.length();
        char* char_array = new char[n + 1];
        strcpy(char_array, recovered.c_str());
        return char_array;
    }
}

我推荐 emscripten documentation 在 emscripten 中使用库 我使用 cryptlib.cpp 文件创建了 cryptlib.a 并像这样使用 emcc 编译它

emcc -c -o cryptlib.o cryptlib.cpp
ar rcs cryptlib.a cryptlib.o

最后我也像这样创建了 Source.o emcc -c -o Source.o Source.cpp

我发现这是获取 html 和 js 文件的命令 emcc Source.o cryptlib.a -o source.html -sEXPORTED_FUNCTIONS=_generateKeys -sEXPORTED_RUNTIME_METHODS=ccall,cwrap

我收到这样的 wasm-ld 错误 Error screenshots

我究竟做错了什么?我还想公开源代码的其他功能,但我只测试了其中一个功能。

【问题讨论】:

    标签: c++ webassembly emscripten crypto++


    【解决方案1】:

    您是否尝试过使用 em++ 而不是 emcc 来编译 cpp 文件?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      相关资源
      最近更新 更多