【问题标题】:Equivalent of Java's String.getBytes() in C++? [duplicate]等效于 C++ 中的 Java 的 String.getBytes()? [复制]
【发布时间】:2021-04-26 21:16:01
【问题描述】:

我需要从服务器传递给我的 Java 加密字符串在 C++ 中实现一个解密方法。 Java加密方法是这样的:

 public String crypt (String strMsg)throws CryptUtilException{
    byte bytePosition;
    int intMsgLen = strMsg.length();
    int intKeyLen = strKey.length();
    byte[] aByteMsg = strMsg.getBytes();
    byte[] aByteOutMsg = new byte[intMsgLen];

    if (intMsgLen==0) {
        throw new CryptUtilException("In String is null.");
    } else{
        for (int i=0; i<intMsgLen; i++) {
            bytePosition = (byte) (i%intKeyLen);
            aByteOutMsg[i] = (byte)((aByteMsg[i] - iCharOffSet) + (byte) strKey.charAt(bytePosition) + bytePosition);
            if (aByteOutMsg [i] < 0) aByteOutMsg [i] = (byte) (aByteOutMsg [i] + 128);
        }

        String strOutMsg = new String (aByteOutMsg);
        return strOutMsg;
    }
}

我的同事使用该代码进行加密,使用以下代码解密字符串,并建议我将此 Java 代码复制到我的 C++ 项目中:

public String decrypt (String strMsg)throws CryptUtilException{
    byte bytePosition;
    int intMsgLen = strMsg.length();
    int intKeyLen = strKey.length();
    byte[] aByteMsg = strMsg.getBytes();
    byte[] aByteOutMsg = new byte[intMsgLen];

    if (intMsgLen==0) {
        throw new CryptUtilException("In String is null.");
    } else{
        for (int i=0; i<intMsgLen; i++) {
            bytePosition = (byte) (i%intKeyLen);
            aByteOutMsg[i] = (byte)((aByteMsg[i] + iCharOffSet) - (byte) strKey.charAt(bytePosition) - bytePosition);
            if (aByteOutMsg [i] < 0) aByteOutMsg [i] = (byte) (aByteOutMsg [i] + 128);
        }

        String strOutMsg = new String (aByteOutMsg);
        return strOutMsg;
    }
}

我很难做到这一点,因为我找不到复制 Java getBytes() 方法的有效方法。 此外,我有一个非常旧的 C++ 版本,为此我不能使用“byte”或“wstring_convert”方法,而且我不在 Windows 操作系统上工作。

【问题讨论】:

  • 您可以使用operator[] 和字符串来获取给定位置的字符。
  • C++ 不是 Java。在 C++ 中,std::string 的内容可以直接访问,就像它是一个容器一样,因为 std::string 实现了与随机访问容器相同的方法。所以:访问字符串中的字节就像访问std::vector 的内容一样。如果有人想将字符串的内容提取到std::vector 中,那么只需使用重载构造函数构造std::vector,该构造函数采用开始和结束序列迭代器,然后传入std::string 的@ 987654331@ 和end()。但这并不会真正起到多大作用。
  • 换句话说,您的aByteMsgaByteOutMsg 变量在C++ 版本中是不必要的。只需直接从字符串中读取和写入“字节”。另请注意,与 Java 不同,C++ 字符串是可变的。

标签: java c++ linux string encryption


【解决方案1】:

char/unsigned char 类型等效于 Java 或其他语言中的 byte

std::string 有方法c_str(),它返回char*,长度为std::string::size()char*是一个c-string,可以认为是底层字符串的完整的二进制表示。

基本上,

std::string s;

const char* s_data = s.c_str();
// do what you want with s_data

如果你只想访问字符,使用 operator[] 就足够了:

char p = s[i]; // process that p as you want!

here 提供的信息应该足以与字符串进行任何类型的交互。

【讨论】:

  • 在 C++ 中,char、signed char 和 unsigned char 是 3 种不同的类型——最接近 Java(有符号)字节的通常是“signed char”,而不是“char”或“unsigned char”。
  • 不知道,谢谢你的提示 :-)
【解决方案2】:

Java byte[] 的关闭 C++ 等效项是 std::bytestd::vector。您可以从std::string strMsg 构造它,例如:

std::vector<std::byte> bytes(strMsg.begin(), strMsg.end());

【讨论】:

  • std::byte 是 c++17 特定的。
  • @LeontyevGeorgiy 是这样吗? AFAIR 它也在 c++20 中
  • >> 我有一个非常旧的 C++ 版本。
猜你喜欢
  • 1970-01-01
  • 2014-05-16
  • 2013-12-02
  • 2012-12-13
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多