#include #include <fstream> #include <sstream> #include static const unsigned int SHIFT = 8; std::string formatCharToByte(unsigned char value) { const int shift = SHIFT - 1; const unsigned int mask = 1 << shift; std::ostringstream buff; for (int i = 1; i <= shift + 1; ++i) { buff << (value & mask ? '1' : '0'); value <<= 1; } return buff.str(); } unsigned char formatByteToChar(const std::string& binFormat) { unsigned char res = 0; const int size = binFormat.size() - 1; for (int i = 0; i <= size; i++) { int b = 1; int n = binFormat[i] - '0'; b <<= size - i; res += n * b; } return res; } bool fileToString(const std::string& filename, std::string& text) { std::ifstream fichier(filename.c_str(), std::ios::binary); if (!fichier) return false; fichier.seekg(0, std::ios_base::end); const int nbOctet = fichier.tellg(); fichier.seekg(0, std::ios_base::beg); char* tableau = new char[nbOctet + 1]; fichier.read(tableau, nbOctet); fichier.close(); tableau[nbOctet] = 0; text.assign(tableau, nbOctet); delete[] tableau; return true; } bool stringToFile(const std::string& filename, const std::string& text) { std::ofstream file(filename.c_str(), std::ios::binary); if (!file) return false; file.write(text.c_str(), text.size()); file.close(); return true; } std::string binaryFormat(const std::string& text) { std::ostringstream buff; for (size_t i = 0; i < text.size(); ++i) buff << formatCharToByte(text[i]); return buff.str(); } std::string restoreFormat(const std::string& text) { std::ostringstream buff; for (size_t i = 0; i < text.size(); i += SHIFT) buff << formatByteToChar(text.substr(i, SHIFT)); return buff.str(); } int main() { const std::string inputFile = "Entree.xyz"; const std::string outputFile2 = "Sortie.xyz"; std::string text; if (!fileToString(inputFile, text)) { std::cerr << "Impossible d'ouvrir le fichier en lecture !" << std::endl; return 1; } //entrée std::string chiffrement= binaryFormat(text); //manipulation sur chiffrement ... //sortie if (!stringToFile(outputFile2, restoreFormat(chiffrement))) { std::cout << "Erreur sur fichier de sortie" << std::endl; return 3; } return 0; } // http://www.cppfrance.com/forum/sujet-METTRE-OCTETS-FICHIER-DANS-STRING_1547281.aspx
il est optimale de passer caractère après caractère de char en binaire[..] Quand j'aurai besoin de réel performance
58 (décimal) 3A (Héxadécimal) 72 (octal) == 111010 (en binaire) == ':' (caractère)
#include int main() { int x = 97; std::cout << "x int: " << x << std::endl << "x char: " << static_cast<char>(x) << std::endl << "x double: " << static_cast<double>(x) << std::endl; int y = 0x61; std::cout << "y int: " << y << std::endl << "y char: " << static_cast<char>(y) << std::endl << "y double: " << static_cast<double>(y) << std::endl; int z = 'a'; std::cout << "z int: " << z << std::endl << "z char: " << static_cast<char>(z) << std::endl << "z double: " << static_cast<double>(z) << std::endl; int a = 0b01100001; std::cout << "a int: " << a << std::endl << "a char: " << static_cast<char>(a) << std::endl << "a double: " << static_cast<double>(a) << std::endl; return 0; }
Vouloir passer par du texte, est une aberration pour deux
je veux est une variable string qui contient TOUTES les données binaires de mon fichier
void applyInvert(std::string& text) { const unsigned int size = text.size(); for (unsigned int i = 0; i < size; ++i) text[i] = ~text[i]; }
faut un string hautement performant avec les 0 et 1 d'un fichier d'entrée pour le chiffrement.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionAvantage(s):
Étant débutant en c++, je vois que les manipulations d'un string en c++ pour faire mon chiffrement.
Alors, j'aurai plus qu'à construire une fonction qui, à une valeur d'un décimal (qui correspond à un char ), renvoie la donnée binaire
#include void chiffrer(std::string& text) { const unsigned int size = text.size(); for (unsigned int i = 0; i < size; ++i) text[i] = ~text[i]; } int main() { std::string s = "Une phrase a chiffrer"; std::cout << "Clair: " << s << std::endl; chiffrer(s); std::cout << "Chiffrée: " << s << std::endl; chiffrer(s); std::cout << "Déchiffrée: " << s << std::endl; return 0; }
Et si je passais par du "bitset" ? http://www.cplusplus.com/reference/stl/bitset/
Je ne sais pas pourquoi mais je le pense : le langage Shell serait intéressant pour ouvrir un fichier et manipuler ses bits directement ?