std::string EnigmaMachine::decrypt(std::string const& cipherString) { // Each pair of cipher characters corresponds to a single char int length(cipherString.size()/2); std::cout << "Cipher string length: " << cipherString.size() << std::endl; std::cout << "Allocation for text: " << length << std::endl; // plaintext will always be equal to or lesser than length of ciphertext BinaryData* plainText = new BinaryData[length]; BinaryData* cipherText = new BinaryData[length]; // Fill the cipherText array with the raw data std::cout << "Binary string:"; for (int i = 0; i < length; ++i) { cipherText[i] = hexToChar(cipherString[2*i], cipherString[2*i+1]); std::cout << " " << charToBin(cipherText[i]) ; } std::cout << std::endl; int pLength(length), fLength(0); EVP_DecryptInit_ex(&m_decryptContext, NULL, NULL, NULL, NULL); EVP_DecryptUpdate(&m_decryptContext, plainText, &pLength, cipherText, length); EVP_DecryptFinal_ex(&m_decryptContext, plainText+pLength, &fLength); length = pLength + fLength; std::string ret((char*)plainText); //std::cout << "Decoded string length: " << length << std::endl; //std::cout << "Decoded string: " << ret << std::endl; delete[] cipherText; delete[] plainText; return ret; }
std::string form_urldecode(const std::string& src) { std::string result; std::string::const_iterator iter; char c; for(iter = src.begin(); iter != src.end(); ++iter) { switch(*iter) { case '+': result.append(1, ' '); break; case '%': // Don't assume well-formed input if(std::distance(iter, src.end()) >= 2 && std::isxdigit(*(iter + 1)) && std::isxdigit(*(iter + 2))) { c = *++iter; result.append(1, hexToChar(c, *++iter)); } // Just pass the % through untouched else { result.append(1, '%'); } break; default: result.append(1, *iter); break; } } return result; }
// URL decoding (%xx) // uses regex pre-compiled on startup String HTTPHeader::decode(const String &s, bool decodeAll) { if (s.length() < 3) { return s; } #ifdef DGDEBUG std::cout << "decoding url" << std::endl; #endif if (!urldecode_re.match(s.c_str())) { return s; } // exit if not found #ifdef DGDEBUG std::cout << "matches:" << urldecode_re.numberOfMatches() << std::endl; std::cout << "removing %XX" << std::endl; #endif int match; int offset; int pos = 0; int size = s.length(); String result; String n; for (match = 0; match < urldecode_re.numberOfMatches(); match++) { offset = urldecode_re.offset(match); if (offset > pos) { result += s.subString(pos, offset - pos); } n = urldecode_re.result(match).c_str(); n.lop(); // remove % result += hexToChar(n, decodeAll); #ifdef DGDEBUG std::cout << "encoded: " << urldecode_re.result(match) << " decoded: " << hexToChar(n) << " string so far: " << result << std::endl; #endif pos = offset + 3; } if (size > pos) { result += s.subString(pos, size - pos); } else { n = "%" + n; } return result; }
/** * @brief QMD5::hexToChars * Converts a string with a 16 hex pair numbers representig chars value to a * array of chars. That is 6465666768 will become to "defgh" * This functions is needed becouse ROS returns 16 chars encoded * that way. Return is a QByteArray becouse it's more useful for * MD5 encoding. * @param s the 32 sized string with the 16 chars encoded. * @return The chars decoded. * @see charsToHex */ QByteArray QMD5::hexToChars(const QString &s) { if( s.count() != 32 ) return s.toLatin1(); QByteArray rtn; rtn.resize(16); for( int i = 0; i < 32; i+=2 ) rtn[i>>1] = hexToChar(s[i+1].toLatin1(), s[i].toLatin1()); return rtn; }
/*! \brief Coloca na pilha TCP os dados que serão enviados do sensor */ static void sendSensorData(void) { unsigned char i; protoBuffer->initString = '<'; //Preenche dados fixos da estrutura protoBuffer->concentrador.concId[0] = hexToChar((MAC_CONC[0] >> 4) & 0x0F); protoBuffer->concentrador.concId[1] = hexToChar(MAC_CONC[0] & 0x0F); protoBuffer->concentrador.concId[2] = hexToChar((MAC_CONC[1] >> 4) & 0x0F); protoBuffer->concentrador.concId[3] = hexToChar(MAC_CONC[1] & 0x0F); protoBuffer->concentrador.concId[4] = hexToChar((MAC_CONC[2] >> 4) & 0x0F); protoBuffer->concentrador.concId[5] = hexToChar(MAC_CONC[2] & 0x0F); protoBuffer->concentrador.concId[6] = hexToChar((MAC_CONC[3] >> 4) & 0x0F); protoBuffer->concentrador.concId[7] = hexToChar(MAC_CONC[3] & 0x0F); protoBuffer->concentrador.tipo = hexToChar(CONC_TIPO); if(sensorGetConcStatus()) { protoBuffer->concentrador.status = '1'; } else { protoBuffer->concentrador.status = '0'; } for(i=0;i<NUM_SENS;i++) { protoBuffer->sensor[i].num = hexToChar(i+1); sensorGetId(i,&(protoBuffer->sensor[i].identificador)); sensorGetTipo(i,&(protoBuffer->sensor[i].tipo)); sensorGetValor(i,&(protoBuffer->sensor[i].valor)); } //Estado dos reles protoBuffer->rele.init = 'R'; for(i=0;i<4;i++) { unsigned char releTmp; releTmp = ioReleStatus(i); if(releTmp != '0') { protoBuffer->rele.estado[i] = releTmp - i; } else { protoBuffer->rele.estado[i] = '0'; } } protoBuffer->endString = '>'; //Envia os dados uip_send(pbuffer,sizeof(DATA_ST)); //Diz que dados já foram enviados sensorSetDataSent(); }
/* If the view has changed, update the contents of the output console accordingly. */ void UsbConsole::onView(QString view) { if(view == currentView) // we haven't changed return; currentView = view; if(!outputConsole->blockCount()) // the console is empty return; QTextCursor c = outputConsole->textCursor(); c.movePosition(QTextCursor::Start); bool keepgoing = true; while(keepgoing) { if(view == "Characters") hexToChar(&c); else if(view == "Hex") charToHex(&c); keepgoing = c.movePosition(QTextCursor::NextBlock); } }
char QMD5::hexToChar(char low, char hi) { return hexToChar(low) + (hexToChar(hi)*16); }
unsigned char getHex(char* a) { return 16*(hexToChar(a[0])) + hexToChar(a[1]); }