How to convert SecByteBlock to string?

后端 未结 2 2121
耶瑟儿~
耶瑟儿~ 2020-12-06 22:27

I\'m having a problem trying to convert SecByteBlock to string. Here\'s my case:

I want to encrypt user access data using AES with static key and dynami

2条回答
  •  忘掉有多难
    2020-12-06 22:41

    I'm having a problem trying to convert SecByteBlock to string

    If the issue is with conversion from SecByteBlock and its byte array to a std::string and its char array, then you should:

    SecByteBlock iv;
    ...
    
    // C-style cast
    std::string token = std::string((const char*)iv.data(), iv.size()) + userAccess;
    

    Or,

    SecByteBlock iv;
    ...
    
    // C++-style cast
    std::string token = std::string(reinterpret_cast(iv.data()), iv.size()) + userAccess;
    

    You can also forgo the assignment, and just initialize and later append:

    SecByteBlock iv;
    ...
    
    std::string token(reinterpret_cast(iv.data()), iv.size());
    ...
    
    std::string userAccess;
    ...
    
    token += userAccess;
    

    The other problem you might have is string to SecByteBlock. You should do this:

    std::string str;
    ...
    
    // C-style cast
    SecByteBlock sbb((const byte*)str.data(), str.size());
    

    Or:

    std::string str;
    ...
    
    // C++-style cast
    SecByteBlock sbb(reinterpret_cast(str.data()), str.size());
    

提交回复
热议问题