【问题标题】:Convert encoded std::string from Base16 to Base10? [closed]将编码的 std::string 从 Base16 转换为 Base10? [关闭]
【发布时间】:2017-07-11 10:14:30
【问题描述】:

我有一个std::string,其中有一个用 Base16 编码的大整数:

bbb91c1c95b656f386b19ab284b9c0f66598e7761cd71569734bb72b6a7153b77613a6cef8e63
e9bd9bb1e0e53a0fd8fa2162b160fcb7b461689afddf098bfc32300cf6808960127f1d9f0e287
f948257f7e0574b56585dd1efe1192d784b9c93f9c2215bd4867062ea30f034265374fa013ab4
5af06cd8554fd55f1c442c2ed

我想要一个std::string,它带有一个用 Base10 编码的大整数:

13182363340585954094154991955162141609757130565683854218475776626603716062690
50741824486137510938646762753180989129520441058729412931959771922633699694948
46611764803267065720664398942078304585998290003537553345030144535441671492050
01138054588415687622649540474976282005406232907125282540703919964112809484362
9

如何将字符串从 Base16 转换为 Base10?

【问题讨论】:

  • 好的,我现在添加它!
  • 它们是字符串,从 rsa 密钥生成中获得 n,将 n 从 CryptoPP::Integer 转换为字符串(感谢您的帮助),在 base64 中加密 n(始终为字符串:这里是代码 -> std ::string hexencoded; CryptoPP::StringSource ss(CryptoppIntegerToString(n), true, new CryptoPP::Base64Encoder(newCryptoPP::StringSink(hexencoded), false)); 一旦我有了 Base64 编码的字符串,我就使用 RC6 和一个包装器,接下来,将它存储到注册表中,想解密它,我拿了字符串,解密它,但我得到了十六进制,这是我的问题,想将它转换为十进制TY
  • 没问题 Jww 我现在就编辑我的问题!

标签: c++ windows hex decimal crypto++


【解决方案1】:

由于您不需要库,因此需要一些代码。这不是一个微不足道的问题,但也不是太复杂。让我们从my Bignum class from another answer 开始,并为其添加几个函数。

class Bignum
{
    //...

    Bignum& operator+=(int rhs)
    {
        assert(rhs >= 0 && rhs <= 999999999);
        uint32_t carry = rhs;
        for (size_t i = 0; i < parts.size(); i++)
        {
            uint32_t sum = parts[i] + carry;
            parts[i] = (uint32_t)(sum % 1000000000UL);
            carry = (uint32_t)(sum / 1000000000UL);
        }
        if (carry != 0)
            parts.push_back(carry);
        return *this;
    }

    void FromHex(const char * pString)
    {
        while (*pString != 0)
        {
            char ch = toupper(*pString++);
            assert((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F'));
            int digit = (ch <= '9') ? (ch - '0') : (ch - 'A' + 10);
            *this *= 16;
            *this += digit;
        }
    }

查看整个过程:http://coliru.stacked-crooked.com/a/cb5061a00c945875

【讨论】:

  • 嗨,马克,提前谢谢,我说错了,我可以接受一个库,但我不想要大型库,因为我在 MultiThread (Visual Studio /MT) 中编译,而像 osstream 这样的库需要很多时间的地方。
  • @123problem 没问题,如果您得到更好的答案,请接受它。
  • 现在就试试你的答案,谢谢你花时间帮助我:)
  • @123problem 我刚刚做了一个小修改,一定要尝试最新的(尽管没关系)。
  • @123problem 我已经给了你 90% 的解决方案,也许你可以解决剩下的?
【解决方案2】:

将编码的 std::string 从 Base16 转换为 Base10?

以下内容应该适合您。下面的代码向您展示了如何使用 C 风格的字符串来实现这一点,这很容易概念化。您在Convert CryptoPP::Integer to LPCTSTR 的上一个问题有参考。

#include <iostream>
#include <string>
using namespace std;

#include "cryptlib.h"
#include "integer.h"
using namespace CryptoPP;

int main(int argc, char* argv[])
{
  string s2, s1 =
      "bbb91c1c95b656f386b19ab284b9c0f66598e7761cd71569734bb72b6a7153b77613a6cef8e63"
      "e9bd9bb1e0e53a0fd8fa2162b160fcb7b461689afddf098bfc32300cf6808960127f1d9f0e287"
      "f948257f7e0574b56585dd1efe1192d784b9c93f9c2215bd4867062ea30f034265374fa013ab4"
      "5af06cd8554fd55f1c442c2ed";

  // Append 'h' to indicate Base16
  // Integer n((s1 + "h").c_str());

  // Prepend '0x' to indicate Base16
  Integer n(("0x" + s1).c_str());

  // Convert to Base10
  s2 = IntToString<Integer>(n, 10);
  cout << s2 << endl;

  return 0;
}

上面的代码向您展示了如何使用 C 风格的字符串来实现,这很容易概念化。另一种方法是使用 Crypto++ Pipeline 将 ASCII 字符串转换为大端字节数组。

#include <iostream>
#include <string>
using namespace std;

#include "cryptlib.h"
#include "integer.h"
#include "filters.h"
#include "hex.h"
using namespace CryptoPP;

int main(int argc, char* argv[])
{
  string s3, s2, s1 =
      "bbb91c1c95b656f386b19ab284b9c0f66598e7761cd71569734bb72b6a7153b77613a6cef8e63"
      "e9bd9bb1e0e53a0fd8fa2162b160fcb7b461689afddf098bfc32300cf6808960127f1d9f0e287"
      "f948257f7e0574b56585dd1efe1192d784b9c93f9c2215bd4867062ea30f034265374fa013ab4"
      "5af06cd8554fd55f1c442c2ed";

  // Use a HexDecoder to convert to big-endian array
  StringSource ss(s1, true, new HexDecoder(new StringSink(s2)));

  // Use big-endian array to construct n
  Integer n((const byte*)s2.data(), s2.size());

  // Convert to Base10
  s3 = IntToString<Integer>(n, 10);
  cout << s3 << endl;

  return 0;
}

这是使用 Crypto++ Pipeline 执行转换的另一种方法。

#include <iostream>
#include <string>
using namespace std;

#include "cryptlib.h"
#include "integer.h"
#include "filters.h"
#include "hex.h"
using namespace CryptoPP;

int main(int argc, char* argv[])
{
  string s2, s1 =
      "bbb91c1c95b656f386b19ab284b9c0f66598e7761cd71569734bb72b6a7153b77613a6cef8e63"
      "e9bd9bb1e0e53a0fd8fa2162b160fcb7b461689afddf098bfc32300cf6808960127f1d9f0e287"
      "f948257f7e0574b56585dd1efe1192d784b9c93f9c2215bd4867062ea30f034265374fa013ab4"
      "5af06cd8554fd55f1c442c2ed";

  // Use a source to convert to big-endian array
  StringSource ss(s1, true, new HexDecoder);

  // Use big-endian array to construct n
  Integer n;
  n.Decode(ss, ss.MaxRetrievable());

  // Convert to Base10
  s2 = IntToString<Integer>(n, 10);
  cout << s2 << endl;

  return 0;
}

如果您对将 ASCII 字符串转换为字节数组以进行内部表示的算法感兴趣,请参阅StringToInteger in integer.cpp。它反复除以基数(2、8、10、16 等)。

【讨论】:

    猜你喜欢
    • 2020-03-28
    • 2016-06-10
    • 2013-09-26
    • 1970-01-01
    • 2017-03-13
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    相关资源
    最近更新 更多