passage en objet fini mais segfault

This commit is contained in:
antoine 2016-01-20 20:25:02 +01:00
parent ce9c44583c
commit a96729198f
15 changed files with 978 additions and 229 deletions

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
aes

12
.idea/aes.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/CMakeLists.txt" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/aes.cpp" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/aes.hpp" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/encodings.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/aes.iml" filepath="$PROJECT_DIR$/.idea/aes.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

320
.idea/workspace.xml Normal file
View File

@ -0,0 +1,320 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="main" />
<config projectName="TP6-7" targetName="ArrayGenerique" />
<config projectName="TP6-7" targetName="dequeSTL" />
</generated>
</component>
<component name="CMakeSettings" AUTO_RELOAD="true" GENERATION_PASS_SYSTEM_ENVIRONMENT="true">
<ADDITIONAL_GENERATION_ENVIRONMENT>
<envs />
</ADDITIONAL_GENERATION_ENVIRONMENT>
</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$/makefile" afterPath="$PROJECT_DIR$/makefile" />
</list>
<ignored path="aes.iws" />
<ignored path=".idea/workspace.xml" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ChangesViewManager" flattened_view="true" show_ignored="false" />
<component name="CreatePatchCommitExecutor">
<option name="PATCH_PATH" value="" />
</component>
<component name="ExecutionTargetManager" SELECTED_TARGET="default_target" />
<component name="FavoritesManager">
<favorites_list name="aes" />
</component>
<component name="FileEditorManager">
<leaf>
<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">
<state vertical-scroll-proportion="0.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>
</file>
<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" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="aes.hpp" pinned="false" current-in-tab="false">
<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>
</entry>
</file>
</leaf>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="HighlightingSettingsPerFile">
<setting file="file://$PROJECT_DIR$/aes.cpp" root0="SKIP_INSPECTION" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/CMakeLists.txt" />
<option value="$PROJECT_DIR$/main.cpp" />
<option value="$PROJECT_DIR$/aes.cpp" />
</list>
</option>
</component>
<component name="JsBuildToolGruntFileManager" detection-done="true" />
<component name="JsBuildToolPackageJson" detection-done="true" />
<component name="JsGulpfileManager">
<detection-done>true</detection-done>
</component>
<component name="ProjectFrameBounds">
<option name="x" value="10" />
<option name="y" value="22" />
<option name="width" value="1346" />
<option name="height" value="703" />
</component>
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State>
<id />
</State>
<State>
<id>C/C++</id>
</State>
</expanded-state>
<selected-state>
<State>
<id>Unused codeC/C++</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1">
<flattenPackages />
<showMembers />
<showModules />
<showLibraryContents />
<hideEmptyPackages />
<abbreviatePackageNames />
<autoscrollToSource />
<autoscrollFromSource />
<sortByType />
<manualOrder />
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="aes" />
<option name="myItemType" value="com.jetbrains.cidr.projectView.CidrFilesViewHelper$MyProjectTreeStructure$1" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="aes" />
<option name="myItemType" value="com.jetbrains.cidr.projectView.CidrFilesViewHelper$MyProjectTreeStructure$1" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="aes" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
</subPane>
</pane>
</panes>
</component>
<component name="PropertiesComponent">
<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" />
</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">
<envs />
<method />
</configuration>
<configuration default="true" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS="FALSE" PROJECT_NAME="aes" TARGET_NAME="aes" CONFIG_NAME="Debug">
<envs />
<method />
</configuration>
<configuration default="true" type="JavascriptDebugType" factoryName="JavaScript Debug">
<method />
</configuration>
<configuration default="true" type="js.build_tools.gulp" factoryName="Gulp.js">
<method />
</configuration>
<configuration default="true" type="js.build_tools.npm" factoryName="npm">
<command value="run-script" />
<scripts />
<envs />
<method />
</configuration>
<configuration default="false" name="Build All" type="CMakeRunConfiguration" factoryName="Application" WORKING_DIR="" PASS_PARENT_ENVS="FALSE" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="aes" RUN_TARGET_NAME="crypto.ex" EXPLICIT_BUILD_TARGET_NAME="all">
<envs />
<method />
</configuration>
<configuration default="false" name="crypto" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="file.txt" WORKING_DIR="file://$PROJECT_DIR$" PASS_PARENT_ENVS="FALSE" PROJECT_NAME="aes" TARGET_NAME="crypto.ex" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="aes" RUN_TARGET_NAME="crypto.ex">
<envs />
<method />
</configuration>
<list size="2">
<item index="0" class="java.lang.String" itemvalue="Application.Build All" />
<item index="1" class="java.lang.String" itemvalue="Application.crypto" />
</list>
</component>
<component name="ShelveChangesManager" show_recycled="false" />
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="3856e2dc-6394-46af-bb28-0bc107cd1b44" name="Default" comment="" />
<created>1453148077173</created>
<option name="number" value="Default" />
<updated>1453148077173</updated>
</task>
<servers />
</component>
<component name="ToolWindowManager">
<frame x="10" y="22" width="1346" height="703" extended-state="0" />
<editor active="false" />
<layout>
<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="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="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="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="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" />
</layout>
</component>
<component name="Vcs.Log.UiProperties">
<option name="RECENTLY_FILTERED_USER_GROUPS">
<collection />
</option>
<option name="RECENTLY_FILTERED_BRANCH_GROUPS">
<collection />
</option>
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/file.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" />
<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" />
<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>
</entry>
<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" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.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$/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>
</component>
</project>

8
CMakeLists.txt Normal file
View File

@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.3)
project(aes)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -ggdb -lcrypto")
include_directories("/usr/include/openssl/")
set(SOURCE_FILES aes.cpp)
add_executable(crypto.ex ${SOURCE_FILES})

285
aes.cpp
View File

@ -1,256 +1,83 @@
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#define FAILURE -1
#define SUCCESS 0
#define AES_KEYLEN 256
#define AES_ROUNDS 6
int aesDecrypt(EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV, unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg) {
size_t decLen = 0;
size_t blockLen = 0;
*decMsg = (unsigned char*)malloc(encMsgLen);
if(*decMsg == NULL) return FAILURE;
if(!EVP_DecryptInit_ex(aesDecryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
return FAILURE;
}
if(!EVP_DecryptUpdate(aesDecryptCtx, (unsigned char*)*decMsg, (int*)&blockLen, encMsg, (int)encMsgLen)) {
return FAILURE;
}
decLen += blockLen;
if(!EVP_DecryptFinal_ex(aesDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
return FAILURE;
}
decLen += blockLen;
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
return (int)decLen;
}
int aesEncrypt(EVP_CIPHER_CTX* aesEncryptCtx, unsigned char* aesKey, unsigned char* aesIV, const unsigned char *msg, size_t msgLen, unsigned char **encMsg) {
size_t blockLen = 0;
size_t encMsgLen = 0;
*encMsg = (unsigned char*)malloc(msgLen + AES_BLOCK_SIZE);
if(encMsg == NULL) return FAILURE;
if(!EVP_EncryptInit_ex(aesEncryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
return FAILURE;
}
if(!EVP_EncryptUpdate(aesEncryptCtx, *encMsg, (int*)&blockLen, (unsigned char*)msg, msgLen)) {
return FAILURE;
}
encMsgLen += blockLen;
if(!EVP_EncryptFinal_ex(aesEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen)) {
return FAILURE;
}
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
return encMsgLen + blockLen;
}
void clear_all(EVP_CIPHER_CTX* aesEncryptCtx, EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV) {
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
free(aesEncryptCtx);
free(aesDecryptCtx);
free(aesIV);
free(aesKey);
}
void init_all(EVP_CIPHER_CTX* aesEncryptCtx, EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV) {
EVP_CIPHER_CTX_init(aesEncryptCtx);
EVP_CIPHER_CTX_init(aesDecryptCtx);
unsigned char *aesPass = (unsigned char*)malloc(AES_KEYLEN/8);
unsigned char *aesSalt = (unsigned char*)malloc(8);
if(aesKey == NULL || aesIV == NULL || aesPass == NULL || aesSalt == NULL) {
exit(FAILURE);
}
#define USE_PBKDF
#ifdef USE_PBKDF
std::cerr << "utilisation de USE_PBKDF" << std::endl;
// Get some random data to use as the AES pass and salt
if(RAND_bytes(aesPass, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
if(RAND_bytes(aesSalt, 8) == 0) {
exit(FAILURE);
}
if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, AES_KEYLEN/8, AES_ROUNDS, aesKey, aesIV) == 0) {
exit(FAILURE);
}
#else
if(RAND_bytes(aesKey, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
if(RAND_bytes(aesIV, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
#endif
free(aesPass);
free(aesSalt);
}
void writeFile(char *filename, unsigned char *file, size_t fileLength) {
FILE *fd = fopen(filename, "wb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}
size_t bytesWritten = fwrite(file, 1, fileLength, fd);
if(bytesWritten != fileLength) {
fprintf(stderr, "Failed to write file\n");
exit(1);
}
fclose(fd);
}
int readFile(char *filename, unsigned char **file) {
FILE *fd = fopen(filename, "rb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}
// Determine size of the file
fseek(fd, 0, SEEK_END);
size_t fileLength = ftell(fd);
fseek(fd, 0, SEEK_SET);
// Allocate space for the file
*file = (unsigned char*)malloc(fileLength);
if(*file == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
// Read the file into the buffer
size_t bytesRead = fread(*file, 1, fileLength, fd);
if(bytesRead != fileLength) {
fprintf(stderr, "Error reading file\n");
exit(1);
}
fclose(fd);
return fileLength;
}
#include "aes.hpp"
#include "crypt.hpp"
#include "decrypt.hpp"
int main(int argc, char* argv[]) {
if(argc != 2) {
fprintf(stderr, "No file argument supplied.\n");
std::cerr << "No file argument supplied.\n";
return 1;
}
EVP_CIPHER_CTX* aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX* aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
if(aesDecryptCtx == NULL || aesEncryptCtx == NULL) {
return FAILURE;
}
unsigned char* aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
unsigned char* aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
init_all(aesEncryptCtx, aesDecryptCtx, aesKey, aesIV);
//***************************************************
char* filename = argv[1];
aes O = crypt(filename);
// Read the file to encrypt
unsigned char *file;
size_t fileLength = readFile(filename, &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 = aesEncrypt(aesEncryptCtx, aesKey, aesIV, (const unsigned char*)file, fileLength, &encryptedFile)) == -1) {
fprintf(stderr, "Encryption failed\n");
return 1;
}
printf("%d bytes encrypted\n", encryptedFileLength);
// Append .enc to the filename
char *encryptedFilename = (char*)malloc(strlen(filename) + 5);
if(encryptedFilename == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}
sprintf(encryptedFilename, "%s.enc", filename);
// Write the encrypted file to its own file
writeFile(encryptedFilename, encryptedFile, encryptedFileLength);
std::cerr << "aesKey : ";
for (int i = 0; i < 32; i++) {
std::cerr << std::hex << aesKey[i];
}
std::cerr << std::endl;
crypt temp = static_cast<crypt>(O);
// if((encryptedFileLength = (temp).aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
// fprintf(stderr, "Encryption failed\n");
// return 1;
// }
// printf("%d bytes encrypted\n", encryptedFileLength);
printf("Encrypted message written to \"%s\"\n", encryptedFilename);
// // Append .enc to the filename
// char *encryptedFilename = (char*)malloc(strlen(filename) + 5);
// if(encryptedFilename == NULL) {
// fprintf(stderr, "Failed to allocate memory\n");
// return 1;
// }
// sprintf(encryptedFilename, "%s.enc", filename);
// // Write the encrypted file to its own file
// writeFile(encryptedFilename, encryptedFile, encryptedFileLength);
// std::cerr << "aesKey : ";
// for (int i = 0; i < 32; i++) {
// std::cerr << std::hex << aesKey[i];
// }
// std::cerr << std::endl;
// printf("Encrypted message written to \"%s\"\n", encryptedFilename);
free(file);
//***************************************************
// //***************************************************
fileLength = readFile(encryptedFilename, &file);
// fileLength = readFile(encryptedFilename, &file);
// Decrypt the encrypted file
unsigned char *decryptedFile;
int decryptedFileLength;
if((decryptedFileLength = aesDecrypt(aesDecryptCtx, aesKey, aesIV, file, fileLength, &decryptedFile)) == -1) {
fprintf(stderr, "Decryption failed\n");
return 1;
}
printf("%d bytes decrypted\n", (int)decryptedFileLength);
// // Decrypt the encrypted file
// unsigned char *decryptedFile;
// int decryptedFileLength;
// if((decryptedFileLength = aesDecrypt(aesDecryptCtx, aesKey, aesIV, file, fileLength, &decryptedFile)) == -1) {
// fprintf(stderr, "Decryption failed\n");
// return 1;
// }
// printf("%d bytes decrypted\n", (int)decryptedFileLength);
// Append .dec to the filename
char *decryptedFilename = (char*)malloc(strlen(filename) + 5);
if(decryptedFilename == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}
sprintf(decryptedFilename, "%s.dec", filename);
// // Append .dec to the filename
// char *decryptedFilename = (char*)malloc(strlen(filename) + 5);
// if(decryptedFilename == NULL) {
// fprintf(stderr, "Failed to allocate memory\n");
// return 1;
// }
// sprintf(decryptedFilename, "%s.dec", filename);
// Write the decrypted file to its own file
writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
printf("Decrypted file written to \"%s\"\n", decryptedFilename);
// // Write the decrypted file to its own file
// writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
// printf("Decrypted file written to \"%s\"\n", decryptedFilename);
free(decryptedFile);
free(file);
// free(decryptedFile);
// free(file);
//***************************************************
clear_all(aesEncryptCtx, aesDecryptCtx, aesKey, aesIV);
return 0;
}

256
aes.cpp.save Normal file
View File

@ -0,0 +1,256 @@
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#define FAILURE -1
#define SUCCESS 0
#define AES_KEYLEN 256
#define AES_ROUNDS 6
int aesDecrypt(EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV, unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg) {
size_t decLen = 0;
size_t blockLen = 0;
*decMsg = (unsigned char*)malloc(encMsgLen);
if(*decMsg == NULL) return FAILURE;
if(!EVP_DecryptInit_ex(aesDecryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
return FAILURE;
}
if(!EVP_DecryptUpdate(aesDecryptCtx, (unsigned char*)*decMsg, (int*)&blockLen, encMsg, (int)encMsgLen)) {
return FAILURE;
}
decLen += blockLen;
if(!EVP_DecryptFinal_ex(aesDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
return FAILURE;
}
decLen += blockLen;
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
return (int)decLen;
}
int aesEncrypt(EVP_CIPHER_CTX* aesEncryptCtx, unsigned char* aesKey, unsigned char* aesIV, const unsigned char *msg, size_t msgLen, unsigned char **encMsg) {
size_t blockLen = 0;
size_t encMsgLen = 0;
*encMsg = (unsigned char*)malloc(msgLen + AES_BLOCK_SIZE);
if(encMsg == NULL) return FAILURE;
if(!EVP_EncryptInit_ex(aesEncryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
return FAILURE;
}
if(!EVP_EncryptUpdate(aesEncryptCtx, *encMsg, (int*)&blockLen, (unsigned char*)msg, msgLen)) {
return FAILURE;
}
encMsgLen += blockLen;
if(!EVP_EncryptFinal_ex(aesEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen)) {
return FAILURE;
}
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
return encMsgLen + blockLen;
}
void clear_all(EVP_CIPHER_CTX* aesEncryptCtx, EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV) {
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
free(aesEncryptCtx);
free(aesDecryptCtx);
free(aesIV);
free(aesKey);
}
void init_all(EVP_CIPHER_CTX* aesEncryptCtx, EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV) {
EVP_CIPHER_CTX_init(aesEncryptCtx);
EVP_CIPHER_CTX_init(aesDecryptCtx);
unsigned char *aesPass = (unsigned char*)malloc(AES_KEYLEN/8);
unsigned char *aesSalt = (unsigned char*)malloc(8);
if(aesKey == NULL || aesIV == NULL || aesPass == NULL || aesSalt == NULL) {
exit(FAILURE);
}
#define USE_PBKDF
#ifdef USE_PBKDF
std::cerr << "utilisation de USE_PBKDF" << std::endl;
// Get some random data to use as the AES pass and salt
if(RAND_bytes(aesPass, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
if(RAND_bytes(aesSalt, 8) == 0) {
exit(FAILURE);
}
if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, AES_KEYLEN/8, AES_ROUNDS, aesKey, aesIV) == 0) {
exit(FAILURE);
}
#else
if(RAND_bytes(aesKey, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
if(RAND_bytes(aesIV, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
#endif
free(aesPass);
free(aesSalt);
}
void writeFile(char *filename, unsigned char *file, size_t fileLength) {
FILE *fd = fopen(filename, "wb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}
size_t bytesWritten = fwrite(file, 1, fileLength, fd);
if(bytesWritten != fileLength) {
fprintf(stderr, "Failed to write file\n");
exit(1);
}
fclose(fd);
}
int readFile(char *filename, unsigned char **file) {
FILE *fd = fopen(filename, "rb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}
// Determine size of the file
fseek(fd, 0, SEEK_END);
size_t fileLength = ftell(fd);
fseek(fd, 0, SEEK_SET);
// Allocate space for the file
*file = (unsigned char*)malloc(fileLength);
if(*file == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
// Read the file into the buffer
size_t bytesRead = fread(*file, 1, fileLength, fd);
if(bytesRead != fileLength) {
fprintf(stderr, "Error reading file\n");
exit(1);
}
fclose(fd);
return fileLength;
}
int main(int argc, char* argv[]) {
if(argc != 2) {
fprintf(stderr, "No file argument supplied.\n");
return 1;
}
EVP_CIPHER_CTX* aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX* aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
if(aesDecryptCtx == NULL || aesEncryptCtx == NULL) {
return FAILURE;
}
unsigned char* aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
unsigned char* aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
init_all(aesEncryptCtx, aesDecryptCtx, aesKey, aesIV);
//***************************************************
char* filename = argv[1];
// Read the file to encrypt
unsigned char *file;
size_t fileLength = readFile(filename, &file);
printf("%d bytes to be encrypted\n", (int)fileLength);
// Encrypt the file
unsigned char *encryptedFile;
int encryptedFileLength;
if((encryptedFileLength = aesEncrypt(aesEncryptCtx, aesKey, aesIV, (const unsigned char*)file, fileLength, &encryptedFile)) == -1) {
fprintf(stderr, "Encryption failed\n");
return 1;
}
printf("%d bytes encrypted\n", encryptedFileLength);
// Append .enc to the filename
char *encryptedFilename = (char*)malloc(strlen(filename) + 5);
if(encryptedFilename == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}
sprintf(encryptedFilename, "%s.enc", filename);
// Write the encrypted file to its own file
writeFile(encryptedFilename, encryptedFile, encryptedFileLength);
std::cerr << "aesKey : ";
for (int i = 0; i < 32; i++) {
std::cerr << std::hex << aesKey[i];
}
std::cerr << std::endl;
printf("Encrypted message written to \"%s\"\n", encryptedFilename);
free(file);
//***************************************************
fileLength = readFile(encryptedFilename, &file);
// Decrypt the encrypted file
unsigned char *decryptedFile;
int decryptedFileLength;
if((decryptedFileLength = aesDecrypt(aesDecryptCtx, aesKey, aesIV, file, fileLength, &decryptedFile)) == -1) {
fprintf(stderr, "Decryption failed\n");
return 1;
}
printf("%d bytes decrypted\n", (int)decryptedFileLength);
// Append .dec to the filename
char *decryptedFilename = (char*)malloc(strlen(filename) + 5);
if(decryptedFilename == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}
sprintf(decryptedFilename, "%s.dec", filename);
// Write the decrypted file to its own file
writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
printf("Decrypted file written to \"%s\"\n", decryptedFilename);
free(decryptedFile);
free(file);
//***************************************************
clear_all(aesEncryptCtx, aesDecryptCtx, aesKey, aesIV);
return 0;
}

143
aes.hpp
View File

@ -0,0 +1,143 @@
#ifndef _AES
#define _AES
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#define FAILURE -1
#define SUCCESS 0
#define AES_KEYLEN 256
#define AES_ROUNDS 6
class aes {
protected:
unsigned char* aesKey;
unsigned char* aesIV;
private:
std::string filename;
public:
aes(std::string _filename);
aes(const aes& _e);
void init_all();
void writeFile(unsigned char *file, size_t fileLength);
int readFile(unsigned char **file);
void clear_all();
~aes();
};
aes::aes(std::string _filename) {
filename = _filename;
aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
}
aes::aes(const aes& _e) : filename(_e.filename) {
std::cerr << "salut\n";
}
void aes::init_all() {
unsigned char *aesPass = (unsigned char*)malloc(AES_KEYLEN/8);
unsigned char *aesSalt = (unsigned char*)malloc(8);
if(aesKey == NULL || aesIV == NULL || aesPass == NULL || aesSalt == NULL) {
exit(FAILURE);
}
#define USE_PBKDF
#ifdef USE_PBKDF
std::cerr << "utilisation de USE_PBKDF" << std::endl;
// Get some random data to use as the AES pass and salt
if(RAND_bytes(aesPass, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
if(RAND_bytes(aesSalt, 8) == 0) {
exit(FAILURE);
}
if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, AES_KEYLEN/8, AES_ROUNDS, aesKey, aesIV) == 0) {
exit(FAILURE);
}
#else
if(RAND_bytes(aesKey, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
if(RAND_bytes(aesIV, AES_KEYLEN/8) == 0) {
exit(FAILURE);
}
#endif
free(aesPass);
free(aesSalt);
}
// 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");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}
// Determine size of the file
fseek(fd, 0, SEEK_END);
size_t fileLength = ftell(fd);
fseek(fd, 0, SEEK_SET);
// Allocate space for the file
*file = (unsigned char*)malloc(fileLength);
if(*file == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
// Read the file into the buffer
size_t bytesRead = fread(*file, 1, fileLength, fd);
if(bytesRead != fileLength) {
fprintf(stderr, "Error reading file\n");
exit(1);
}
fclose(fd);
return fileLength;
}
void aes::writeFile(unsigned char *file, size_t fileLength) {
FILE *fd = fopen(filename.c_str(), "wb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}
size_t bytesWritten = fwrite(file, 1, fileLength, fd);
if(bytesWritten != fileLength) {
fprintf(stderr, "Failed to write file\n");
exit(1);
}
fclose(fd);
}
void aes::clear_all() {
free(aesIV);
free(aesKey);
}
aes::~aes() {}
#endif

75
crypt.hpp Normal file
View File

@ -0,0 +1,75 @@
#ifndef _CRYPT
#define _CRYPT
#include "aes.hpp"
class crypt : public aes {
private:
EVP_CIPHER_CTX* aesEncryptCtx;
public:
crypt(std::string filename);
crypt(const aes& a);
void init_all();
int aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg);
~crypt();
};
crypt::crypt(std::string filename) : aes(filename) {
aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
if(aesEncryptCtx == NULL) {
exit(FAILURE);
}
init_all();
}
crypt::crypt(const aes& _a) : aes(_a) {
aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
if(aesEncryptCtx == NULL) {
exit(FAILURE);
}
int res = EVP_CIPHER_CTX_copy(aesEncryptCtx, static_cast<crypt>(_a).aesEncryptCtx);
if(!res)
exit(FAILURE);
// *aesEncryptCtx = (*(static_cast<crypt>(_a)).aesEncryptCtx);
}
void crypt::init_all() {
aes::init_all();
EVP_CIPHER_CTX_init(aesEncryptCtx);
}
int crypt::aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg) {
size_t blockLen = 0;
size_t encMsgLen = 0;
*encMsg = (unsigned char*)malloc(msgLen + AES_BLOCK_SIZE);
if(encMsg == NULL) return FAILURE;
if(!EVP_EncryptInit_ex(aesEncryptCtx, EVP_aes_256_cbc(), NULL, aes::aesKey, aes::aesIV)) {
return FAILURE;
}
if(!EVP_EncryptUpdate(aesEncryptCtx, *encMsg, (int*)&blockLen, (unsigned char*)msg, msgLen)) {
return FAILURE;
}
encMsgLen += blockLen;
if(!EVP_EncryptFinal_ex(aesEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen)) {
return FAILURE;
}
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
return encMsgLen + blockLen;
}
crypt::~crypt() {
aes::clear_all();
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
free(aesEncryptCtx);
}
#endif

BIN
crypto.ex

Binary file not shown.

71
decrypt.hpp Normal file
View File

@ -0,0 +1,71 @@
#ifndef _DECRYPT
#define _DECRYPT
#include "aes.hpp"
class decrypt : public aes {
private:
EVP_CIPHER_CTX* aesDecryptCtx;
public:
decrypt(std::string filename);
decrypt(const aes& a);
void init_all();
int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg);
~decrypt();
};
decrypt::decrypt(std::string filename) : aes(filename) {
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
if(aesDecryptCtx == NULL) {
exit(FAILURE);
}
init_all();
}
decrypt::decrypt(const aes& _a) : aes(_a) {
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
if(aesDecryptCtx == NULL) {
exit(FAILURE);
}
*aesDecryptCtx = (*(static_cast<decrypt>(_a)).aesDecryptCtx);
}
void decrypt::init_all() {
aes::init_all();
EVP_CIPHER_CTX_init(aesDecryptCtx);
}
int decrypt::aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg) {
size_t decLen = 0;
size_t blockLen = 0;
*decMsg = (unsigned char*)malloc(encMsgLen);
if(*decMsg == NULL) return FAILURE;
if(!EVP_DecryptInit_ex(aesDecryptCtx, EVP_aes_256_cbc(), NULL, aes::aesKey, aes::aesIV)) {
return FAILURE;
}
if(!EVP_DecryptUpdate(aesDecryptCtx, (unsigned char*)*decMsg, (int*)&blockLen, encMsg, (int)encMsgLen)) {
return FAILURE;
}
decLen += blockLen;
if(!EVP_DecryptFinal_ex(aesDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
return FAILURE;
}
decLen += blockLen;
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
return (int)decLen;
}
decrypt::~decrypt() {
aes::clear_all();
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
free(aesDecryptCtx);
}
#endif

View File

@ -1,5 +1,7 @@
main:
@g++ -Wall -Wextra -ggdb -o crypto.ex aes.cpp -lcrypto
run:
@./crypto.ex file.txt
clean: