main cryptage et decryptage séparer avec generation de json contenant les clef aes tous semble fonctionne

This commit is contained in:
antoine 2016-03-06 20:20:50 +01:00
parent 103fb7d640
commit 32df0c3a32
17 changed files with 7809 additions and 58 deletions

View File

@ -2,12 +2,12 @@
<project version="4">
<component name="CMakeRunConfigurationManager" shouldGenerate="true" assignedExecutableTargets="true" buildAllGenerated="true">
<generated>
<config projectName="TP6-7" targetName="clean-all" />
<config projectName="aes" targetName="aes" />
<config projectName="aes" targetName="crypto.ex" />
<config projectName="aes" targetName="clean-all" />
<config projectName="aes" targetName="crypto" />
<config projectName="aes" targetName="aes" />
<config projectName="aes" targetName="clean-all" />
<config projectName="aes" targetName="main" />
<config projectName="aes" targetName="crypto.ex" />
<config projectName="TP6-7" targetName="clean-all" />
<config projectName="TP6-7" targetName="ArrayGenerique" />
<config projectName="TP6-7" targetName="dequeSTL" />
</generated>
@ -19,7 +19,7 @@
</component>
<component name="ChangeListManager">
<list default="true" id="3856e2dc-6394-46af-bb28-0bc107cd1b44" name="Default" comment="">
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/aes.cpp" afterPath="$PROJECT_DIR$/aes.cpp" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/makefile" afterPath="$PROJECT_DIR$/makefile" />
</list>
<ignored path="aes.iws" />
@ -40,7 +40,7 @@
<favorites_list name="aes" />
</component>
<component name="FileEditorManager">
<leaf>
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file leaf-file-name="CMakeLists.txt" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
@ -54,18 +54,8 @@
<file leaf-file-name="aes.cpp" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/aes.cpp">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.25684932">
<caret line="10" column="0" selection-start-line="10" selection-start-column="0" selection-end-line="10" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="main.cpp" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/main.cpp">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="9" column="25" selection-start-line="9" selection-start-column="25" selection-end-line="9" selection-end-column="25" />
<state vertical-scroll-proportion="0.051369864">
<caret line="4" column="0" selection-start-line="4" selection-start-column="0" selection-end-line="4" selection-end-column="0" />
<folding />
</state>
</provider>
@ -179,7 +169,7 @@
<property name="settings.editor.selected.configurable" value="configurable.group.appearance" />
<property name="settings.editor.splitter.proportion" value="0.2" />
<property name="js-jscs-nodeInterpreter" value="/usr/bin/node" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../mif31/ArrayGenerique/CMakeLists.txt" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../mif31/mif31_interpol" />
</component>
<component name="RunManager" selected="Application.crypto">
<configuration default="true" type="CMakeGoogleTestRunConfigurationType" factoryName="Google Test" PASS_PARENT_ENVS="FALSE" PROJECT_NAME="aes" TARGET_NAME="aes" CONFIG_NAME="Debug" TEST_MODE="SUITE_TEST">
@ -194,6 +184,11 @@
<method />
</configuration>
<configuration default="true" type="js.build_tools.gulp" factoryName="Gulp.js">
<node-options />
<gulpfile />
<tasks />
<arguments />
<envs />
<method />
</configuration>
<configuration default="true" type="js.build_tools.npm" factoryName="npm">
@ -232,24 +227,24 @@
<frame x="10" y="22" width="1346" height="703" extended-state="0" />
<editor active="false" />
<layout>
<window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.1376488" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Messages" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.3289689" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="CMake" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.3289689" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.39934534" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.3289689" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.1376488" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="CMake" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.3289689" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="true" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
<window_info id="Messages" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.3289689" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.39934534" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.3289689" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
</layout>
</component>
<component name="Vcs.Log.UiProperties">
@ -268,18 +263,18 @@
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/file.txt">
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<caret line="4" column="42" selection-start-line="4" selection-start-column="42" selection-end-line="4" selection-end-column="42" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/makefile">
<entry file="file://$PROJECT_DIR$/aes.cpp">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.025684932">
<caret line="1" column="58" selection-start-line="1" selection-start-column="58" selection-end-line="1" selection-end-column="58" />
<state vertical-scroll-proportion="0.0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
@ -292,14 +287,21 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/main.cpp">
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="9" column="25" selection-start-line="9" selection-start-column="25" selection-end-line="9" selection-end-column="25" />
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/makefile">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.025684932">
<caret line="1" column="58" selection-start-line="1" selection-start-column="58" selection-end-line="1" selection-end-column="58" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
@ -310,8 +312,16 @@
</entry>
<entry file="file://$PROJECT_DIR$/aes.cpp">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.25684932">
<caret line="10" column="0" selection-start-line="10" selection-start-column="0" selection-end-line="10" selection-end-column="0" />
<state vertical-scroll-proportion="0.051369864">
<caret line="4" column="0" selection-start-line="4" selection-start-column="0" selection-end-line="4" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/aes.hpp">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>

70
aes.hpp
View File

@ -9,6 +9,7 @@
#include <algorithm>
#include <typeinfo>
#include <regex>
#include <fstream>
#include <openssl/evp.h>
#include <openssl/pem.h>
@ -16,6 +17,8 @@
#include <openssl/err.h>
#include <openssl/rand.h>
#include "json/json.h"
#define FAILURE -1
#define SUCCESS 0
@ -33,17 +36,24 @@ protected:
private:
std::string filename;
std::string generateNewFilename();
std::string getFilePath();
std::string extension = ".new";
public:
aes() {};
aes(std::string _filename);
static std::string writeFile(unsigned char *file, size_t fileLength, const char* Filename);
std::string writeFile(unsigned char *file, size_t fileLength);
static int readFile(unsigned char** file, const char* filename);
int readFile(unsigned char **file);
std::string exportKey(const char* filename);
std::string exportKey();
void importKey(const char* filename);
std::string PrintAesKey();
void setAesIV(unsigned char* _aesIV);
void setAesKey(unsigned char* _aesKey);
void setAesIV(const unsigned char* _aesIV);
void setAesKey(const unsigned char* _aesKey);
unsigned char* getAesKey();
unsigned char* getAesIV();
@ -102,10 +112,13 @@ void aes::init_all() {
free(aesPass);
free(aesSalt);
}
int aes::readFile(unsigned char** file) {
return readFile(file, filename.c_str());
}
// peut être déporté le buffer lut avec methode pour travaillé dessus
int aes::readFile(unsigned char** file) {
FILE *fd = fopen(filename.c_str(), "rb");
int aes::readFile(unsigned char** file, const char* filename) {
FILE *fd = fopen(filename, "rb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
@ -136,15 +149,23 @@ int aes::readFile(unsigned char** file) {
return fileLength;
}
std::string aes::generateNewFilename() {
std::string aes::getFilePath() {
std::regex e("\\b(.*[(\\.txt)]*)\\.[^(txt)].*");
filename = std::regex_replace(filename, e,"$1");
return filename+extension;
return filename;
}
std::string aes::writeFile(unsigned char *file, size_t fileLength) {
std::string aes::generateNewFilename() {
return aes::getFilePath()+extension;
}
std::string aes::writeFile(unsigned char *file, size_t fileLength) {
std::string newFile = generateNewFilename();
FILE *fd = fopen(newFile.c_str(), "wb");
return writeFile(file, fileLength, newFile.c_str());
}
std::string aes::writeFile(unsigned char *file, size_t fileLength, const char* Filename) {
FILE *fd = fopen(Filename, "wb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
@ -158,20 +179,20 @@ std::string aes::writeFile(unsigned char *file, size_t fileLength) {
}
fclose(fd);
return newFile;
return Filename;
}
void aes::setExtension(std::string _ex) {
extension = _ex;
}
void aes::setAesKey(unsigned char* _aesKey) {
void aes::setAesKey(const unsigned char* _aesKey) {
if(aesKey == NULL)
aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
memcpy(aesKey , _aesKey, AES_KEYLEN/8);
}
void aes::setAesIV(unsigned char* _aesIV) {
void aes::setAesIV(const unsigned char* _aesIV) {
if(aesIV == NULL)
aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
memcpy(aesIV , _aesIV, AES_KEYLEN/8);
@ -203,6 +224,33 @@ std::string aes::PrintAesKey() {
return res;
}
void aes::importKey(const char* filename) {
Json::Value root;
std::ifstream keyfile(filename, std::ifstream::binary);
keyfile >> root;
setAesKey(reinterpret_cast<const unsigned char*>(root["key"].asString().c_str()));
setAesIV(reinterpret_cast<const unsigned char*>(root["IV"].asString().c_str()));
}
std::string aes::exportKey() {
return exportKey((getFilePath()+".key").c_str());
}
std::string aes::exportKey(const char* filename) {
// export the tuples aeskey and aesIV to json file
Json::Value root;
root["key"] = std::string(reinterpret_cast<char *>(aesKey));
root["IV"] = std::string(reinterpret_cast<char *>(aesIV));
std::ofstream out(filename);
out << root;
out.close();
return std::string(filename);
}
void aes::clear_all() {
free(aesIV);
free(aesKey);

BIN
bin/crypt.ex Executable file

Binary file not shown.

BIN
bin/decrypt.ex Executable file

Binary file not shown.

4
data/aesKey.key Normal file
View File

@ -0,0 +1,4 @@
{
"IV" : "<22>e‡£|<7C>-}“õ:šåº¿‹@¼3†Ægñ¼\u0002Ï\u0006/ÒU",
"key" : "|\u0006[QGµåÿu°¤aJnóf®'gØ\u0011/nÝ`\u0015ûþdd»/"
}

View File

1
data/file.dec Normal file
View File

@ -0,0 +1 @@
toto

1
data/file.enc Normal file
View File

@ -0,0 +1 @@
?aîÈ×Y|°AöòÑï³

View File

@ -9,12 +9,12 @@ private:
public:
decrypt(std::string filename, unsigned char* aesKey=0, unsigned char* aesIV=0);
decrypt(const decrypt& a);
void init_all(bool initSuper=true);
int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char** decMsg);
~decrypt();
};
decrypt::decrypt(std::string filename, unsigned char* aesKey, unsigned char* aesIV) : aes(filename) {
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
if(aesDecryptCtx == NULL) {

289
json/json-forwards.h Normal file
View File

@ -0,0 +1,289 @@
/// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/).
/// It is intended to be used with #include "json/json-forwards.h"
/// This header provides forward declaration for all JsonCpp types.
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: LICENSE
// //////////////////////////////////////////////////////////////////////
/*
The JsonCpp library's source code, including accompanying documentation,
tests and demonstration applications, are licensed under the following
conditions...
The author (Baptiste Lepilleur) explicitly disclaims copyright in all
jurisdictions which recognize such a disclaimer. In such jurisdictions,
this software is released into the Public Domain.
In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is
released under the terms of the MIT License (see below).
In jurisdictions which recognize Public Domain property, the user of this
software may choose to accept it either as 1) Public Domain, 2) under the
conditions of the MIT License (see below), or 3) under the terms of dual
Public Domain/MIT License conditions described here, as they choose.
The MIT License is about as close to Public Domain as a license can get, and is
described in clear, concise terms at:
http://en.wikipedia.org/wiki/MIT_License
The full text of the MIT License follows:
========================================================================
Copyright (c) 2007-2010 Baptiste Lepilleur
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
========================================================================
(END LICENSE TEXT)
The MIT license is compatible with both the GPL and commercial
software, affording one all of the rights of Public Domain with the
minor nuisance of being required to keep the above copyright notice
and license text in the source code. Note also that by accepting the
Public Domain "license" you can re-license your copy using whatever
license you like.
*/
// //////////////////////////////////////////////////////////////////////
// End of content of file: LICENSE
// //////////////////////////////////////////////////////////////////////
#ifndef JSON_FORWARD_AMALGATED_H_INCLUDED
# define JSON_FORWARD_AMALGATED_H_INCLUDED
/// If defined, indicates that the source file is amalgated
/// to prevent private header inclusion.
#define JSON_IS_AMALGAMATION
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: include/json/config.h
// //////////////////////////////////////////////////////////////////////
// Copyright 2007-2010 Baptiste Lepilleur
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
#ifndef JSON_CONFIG_H_INCLUDED
#define JSON_CONFIG_H_INCLUDED
#include <stddef.h>
/// If defined, indicates that json library is embedded in CppTL library.
//# define JSON_IN_CPPTL 1
/// If defined, indicates that json may leverage CppTL library
//# define JSON_USE_CPPTL 1
/// If defined, indicates that cpptl vector based map should be used instead of
/// std::map
/// as Value container.
//# define JSON_USE_CPPTL_SMALLMAP 1
// If non-zero, the library uses exceptions to report bad input instead of C
// assertion macros. The default is to use exceptions.
#ifndef JSON_USE_EXCEPTION
#define JSON_USE_EXCEPTION 1
#endif
/// If defined, indicates that the source file is amalgated
/// to prevent private header inclusion.
/// Remarks: it is automatically defined in the generated amalgated header.
// #define JSON_IS_AMALGAMATION
#ifdef JSON_IN_CPPTL
#include <cpptl/config.h>
#ifndef JSON_USE_CPPTL
#define JSON_USE_CPPTL 1
#endif
#endif
#ifdef JSON_IN_CPPTL
#define JSON_API CPPTL_API
#elif defined(JSON_DLL_BUILD)
#if defined(_MSC_VER)
#define JSON_API __declspec(dllexport)
#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
#endif // if defined(_MSC_VER)
#elif defined(JSON_DLL)
#if defined(_MSC_VER)
#define JSON_API __declspec(dllimport)
#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
#endif // if defined(_MSC_VER)
#endif // ifdef JSON_IN_CPPTL
#if !defined(JSON_API)
#define JSON_API
#endif
// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for
// integer
// Storages, and 64 bits integer support is disabled.
// #define JSON_NO_INT64 1
#if defined(_MSC_VER) // MSVC
# if _MSC_VER <= 1200 // MSVC 6
// Microsoft Visual Studio 6 only support conversion from __int64 to double
// (no conversion from unsigned __int64).
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
// Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255'
// characters in the debug information)
// All projects I've ever seen with VS6 were using this globally (not bothering
// with pragma push/pop).
# pragma warning(disable : 4786)
# endif // MSVC 6
# if _MSC_VER >= 1500 // MSVC 2008
/// Indicates that the following function is deprecated.
# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))
# endif
#endif // defined(_MSC_VER)
#ifndef JSON_HAS_RVALUE_REFERENCES
#if defined(_MSC_VER) && _MSC_VER >= 1600 // MSVC >= 2010
#define JSON_HAS_RVALUE_REFERENCES 1
#endif // MSVC >= 2010
#ifdef __clang__
#if __has_feature(cxx_rvalue_references)
#define JSON_HAS_RVALUE_REFERENCES 1
#endif // has_feature
#elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc)
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L)
#define JSON_HAS_RVALUE_REFERENCES 1
#endif // GXX_EXPERIMENTAL
#endif // __clang__ || __GNUC__
#endif // not defined JSON_HAS_RVALUE_REFERENCES
#ifndef JSON_HAS_RVALUE_REFERENCES
#define JSON_HAS_RVALUE_REFERENCES 0
#endif
#ifdef __clang__
#elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc)
# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
# define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
# elif (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
# define JSONCPP_DEPRECATED(message) __attribute__((__deprecated__))
# endif // GNUC version
#endif // __clang__ || __GNUC__
#if !defined(JSONCPP_DEPRECATED)
#define JSONCPP_DEPRECATED(message)
#endif // if !defined(JSONCPP_DEPRECATED)
#if __GNUC__ >= 6
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
#endif
namespace Json {
typedef int Int;
typedef unsigned int UInt;
#if defined(JSON_NO_INT64)
typedef int LargestInt;
typedef unsigned int LargestUInt;
#undef JSON_HAS_INT64
#else // if defined(JSON_NO_INT64)
// For Microsoft Visual use specific types as long long is not supported
#if defined(_MSC_VER) // Microsoft Visual Studio
typedef __int64 Int64;
typedef unsigned __int64 UInt64;
#else // if defined(_MSC_VER) // Other platforms, use long long
typedef long long int Int64;
typedef unsigned long long int UInt64;
#endif // if defined(_MSC_VER)
typedef Int64 LargestInt;
typedef UInt64 LargestUInt;
#define JSON_HAS_INT64
#endif // if defined(JSON_NO_INT64)
} // end namespace Json
#endif // JSON_CONFIG_H_INCLUDED
// //////////////////////////////////////////////////////////////////////
// End of content of file: include/json/config.h
// //////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: include/json/forwards.h
// //////////////////////////////////////////////////////////////////////
// Copyright 2007-2010 Baptiste Lepilleur
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
#ifndef JSON_FORWARDS_H_INCLUDED
#define JSON_FORWARDS_H_INCLUDED
#if !defined(JSON_IS_AMALGAMATION)
#include "config.h"
#endif // if !defined(JSON_IS_AMALGAMATION)
namespace Json {
// writer.h
class FastWriter;
class StyledWriter;
// reader.h
class Reader;
// features.h
class Features;
// value.h
typedef unsigned int ArrayIndex;
class StaticString;
class Path;
class PathArgument;
class Value;
class ValueIteratorBase;
class ValueIterator;
class ValueConstIterator;
} // namespace Json
#endif // JSON_FORWARDS_H_INCLUDED
// //////////////////////////////////////////////////////////////////////
// End of content of file: include/json/forwards.h
// //////////////////////////////////////////////////////////////////////
#endif //ifndef JSON_FORWARD_AMALGATED_H_INCLUDED

2080
json/json.h Normal file

File diff suppressed because it is too large Load Diff

5207
jsoncpp.cpp Normal file

File diff suppressed because it is too large Load Diff

44
mainCrypt.cpp Normal file
View File

@ -0,0 +1,44 @@
#include "aes.hpp"
#include "crypt.hpp"
int main(int argc, char* argv[]) {
if(argc != 2) {
std::cerr << "No file argument supplied.\n";
return 1;
}
char* filename = argv[1];
aes* O = new crypt(filename);
// Read the file to encrypt
unsigned char* file;
// readFile fait l'aloccation mémoire !!! pensé au free
size_t fileLength = O->readFile(&file);
printf("%d bytes to be encrypted\n", (int)fileLength);
// Encrypt the file
unsigned char *encryptedFile;
int encryptedFileLength;
if((encryptedFileLength = (dynamic_cast<crypt*>(O))->aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
fprintf(stderr, "Encryption failed\n");
return 1;
}
printf("%d bytes encrypted\n", encryptedFileLength);
std::cerr << "mainCrypt.ex : " << O->PrintAesKey() << std::endl;
std::string keyFilename = O->exportKey();
// std::string keyFilename = O->writeFile(O->getAesKey(), (O->PrintAesKey()).length(), "aesKey.key");
printf("Encrypt key written to \"%s\"\n", keyFilename.c_str());
// Write the encrypted file to its own file
std::string encryptedFilename = O->writeFile(encryptedFile, encryptedFileLength);
printf("Encrypted message written to \"%s\"\n", encryptedFilename.c_str());
free(encryptedFile);
free(file);
delete O;
return 0;
}

38
mainDecrypt.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "aes.hpp"
#include "decrypt.hpp"
int main(int argc, char* argv[]) {
if(argc != 3) {
std::cerr << "An input file is missing.\n ./Decrypt.ex fileToDecrypt AeskeyForDecrypt\n";
return 1;
}
size_t fileLength;
unsigned char* fileEncrypted;
unsigned char* decryptedFile;
aes* O2 = new decrypt(argv[1]);
O2->importKey(argv[2]);
fileLength = O2->readFile(&fileEncrypted);
std::cerr << "byte to be decrypted : " << fileLength << std::endl;
int decryptedFileLength = (dynamic_cast<decrypt*>(O2))->aesDecrypt(fileEncrypted, fileLength, &decryptedFile);
if( decryptedFileLength == -1) {
fprintf(stderr, "Decryption failed\n");
return 1;
}
printf("%d bytes decrypted\n", (int)decryptedFileLength);
std::cerr << O2->PrintAesKey() << std::endl;
// Write the decrypted file to its own file
std::string decryptedFilename = O2->writeFile(decryptedFile, decryptedFileLength);
printf("Decrypted file written to \"%s\"\n", decryptedFilename.c_str());
free(decryptedFile);
free(fileEncrypted);
delete O2;
return 0;
}

View File

@ -1,11 +1,40 @@
main:
@g++ -Wall -Wextra -ggdb -o crypto.ex aes.cpp -lcrypto --std=c++14
EXEC=bin/crypto.ex
SRC=aes.cpp
main: aes.cpp jsoncpp.o
@echo "compilation de aes"
@g++ -Wall -Wextra -ggdb -o $(EXEC) $(SRC) obj/jsoncpp.o -lcrypto --std=c++14
jsoncpp.o: jsoncpp.cpp json/json.h json/json-forwards.h
@echo "compilation de jsoncpp"
@g++ -I ../lib/jsoncpp-master/include/ -Wall -Wextra -std=c++11 -c -ggdb jsoncpp.cpp -o obj/jsoncpp.o
run2:
@./bin/crypt.ex data/file && ./bin/decrypt.ex data/file.enc data/aesKey.key
run:
@./crypto.ex file
@./$(EXEC) file
decrypt.ex: mainDecrypt.cpp jsoncpp.o aes.hpp
@echo "compilation de decrypt.ex"
@g++ -Wall -Wextra -ggdb -o bin/decrypt.ex mainDecrypt.cpp obj/jsoncpp.o -lcrypto --std=c++14
crypt.ex: mainCrypt.cpp jsoncpp.o aes.hpp
@echo "compilation de crypt.ex"
@g++ -Wall -Wextra -ggdb -o bin/crypt.ex mainCrypt.cpp obj/jsoncpp.o -lcrypto --std=c++14
valgrind:
@valgrind --leak-check=full --track-origins=yes ./crypto.ex file
@valgrind --leak-check=full --track-origins=yes ./$(EXEC) file
clean:
@rm *.ex
clean: cleanO cleanB
cleanO:
@rm obj/*.o
cleanB:
@rm bin/*.ex
cleanD:
@rm data/*.enc
@rm data/*.dec
@rm data/*.key

BIN
obj/jsoncpp.o Normal file

Binary file not shown.