2

I need to implement a decrypt method in c++ from a Java encrypted string passed to me from a server. The Java encrypted method is this:

 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;
    }
}

My work colleague use that code for encrypt and the following code for decrypt a string and suggest my to replicate this Java code into my C++ project:

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;
    }
}

I have a lot of difficulty doing it because I don't find a valid method for replicate the Java getBytes() method. In addition, I have a very old version of C++ and for this I can't use the "byte" or the "wstring_convert" method and I'm not working on windows OS.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • You could use `operator[]` with the string to get a character at a given position. – IlCapitano Jan 22 '21 at 11:52
  • https://stackoverflow.com/questions/505021/get-bytes-from-stdstring-in-c – Ilya Mezhov Jan 22 '21 at 11:53
  • 3
    C++ is not Java. In C++, a `std::string`'s contents are accessed directly as if it was a container, since `std::string` implements the same methods as random access containers. So: access the bytes in the string as if you were to access the contents of a `std::vector`. If one had an itch to extract the contents of a string into a `std::vector` anyway, one would simply construct the `std::vector` using the overloaded constructor that takes a beginning and ending sequence iterator, and then pass in `std::string`'s `begin()` and `end()`. But that would not really accomplish much. – Sam Varshavchik Jan 22 '21 at 11:54
  • Put it another way your `aByteMsg` and `aByteOutMsg` variables are unnecessary in the C++ version. Just read and write 'bytes' directly from the string. Also note C++ strings are mutable, unlike Java. – john Jan 22 '21 at 11:56

2 Answers2

0

char/unsigned char types are an equivalent of byte in Java or other languages.

std::string has method c_str(), which returns char* with the length of std::string::size(). char*, being a c-string, can be considered as a full and complete binary representation of the underlying string.

Basically,

std::string s;

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

If all you want is an access to characters, using operator[] is enough:

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

Information, provided here, should be enough for any kind of interaction with strings.

Leontyev Georgiy
  • 1,295
  • 11
  • 24
-3

The closes C++ equivalent of Java byte[] is std::vector of std::byte. You can construct it from std::string strMsg like:

std::vector<std::byte> bytes(strMsg.begin(), strMsg.end());
majkrzak
  • 1,332
  • 3
  • 14
  • 30