#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[nbOctet]; fichier.read(tableau, nbOctet); fichier.close(); text.assign(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 = "testingEntree.txt"; const std::string outputFile2 = "testingSortie2.txt"; std::string text; if (!fileToString(inputFile, text)) { std::cerr << "Impossible d'ouvrir le fichier en lecture !" << std::endl; return 1; } //emplacement pour chiffrage if (!stringToFile(outputFile2, restoreFormat(chiffrement))) { std::cout << "Erreur sur fichier de sortie" << std::endl; return 3; } return 0; }
c'est 4 GO pour du 32 bits
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionconst std::string inputFile = "image.jpg"; const std::string outputFile1 = "testingSortie1.txt"; const std::string outputFile2 = "imagesortie.jpg";
const std::string inputFile = "image.txt"; const std::string outputFile1 = "testingSortie1.txt"; const std::string outputFile2 = "imagesortie.txt";
const std::string inputFile = "testingEntree.txt"; const std::string outputFile1 = "testingSortie1.txt"; const std::string outputFile2 = "testingSortie.txt";
const std::string inputFile = "son.txt"; const std::string outputFile1 = "testingSortie1.txt"; const std::string outputFile2 = "sonSortie.txt";
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); delete[] tableau; return true; }
et de lui donner une taille supérieur à sa contenance de 1 (pour y mettre le fameux 0 terminal).
Une erreur on ne peut plus classique.
Mais il reste encore un problème : les fichiers de sortie font tous 4 octets.
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 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]; fichier.read(tableau, nbOctet); fichier.close(); text.assign(tableau, nbOctet); delete[] tableau; return true; }
pour chiffrer un fichier, on ne passe jamais pas sa représentation textuelle binaire, mais directement en affectant son contenu (qui est binaire par nature)
Merci, merci et encore merci à vous 2 !!!
Je suis sur que ça m'arrivera encore dans 10 ans :p
les caractères non affichables sont alors remplacer par des espaces et je perds de l'information car pour déchiffrer (de l'espace ou caractère non affichable) il y a perd d'information. Non?
Ce fut un plaisir
Aucune différence, à part deux étapes inutiles en moins... Gain de perf assuré.
applyXor(text); // chiffre applyXor(text); // déchiffre
binaire -> action -> binaire
applyXor(text); // chiffre applyXor(text); // déchiffre
#include int main() { unsigned char c = 6; // ou 0000 0110 unsigned char inverse_c ~c; // ic 1111 1001 => 249 unsigned char b 6 & 2; // 0000 0100> 2 unsigned char z c | 1; // 0000 0111> 7 std::cout << "c = " << static_cast(c) << std::endl << "inverse_c = " << static_cast(inverse_c) << std::endl << "b = " << static_cast(b) << std::endl << "z = " << static_cast(z) << std::endl; return 0; }