Advertisement
saleks28

Untitled

Sep 14th, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.19 KB | None | 0 0
  1. / шифрует строку XOR-шифрованием с ключом 0xff
  2. bool encryptFile(const char *filePath, const char *dirPath, uint8_t *key, bool isRemove)
  3. {
  4.     FILE *inFile = NULL;
  5.     FILE *outFile = NULL;
  6.     inFile = fopen(filePath, "rb");
  7.  
  8.     if (inFile == NULL) {
  9.         LOGE("Error: cannot open file %s", filePath);
  10.         return false;
  11.     }
  12.  
  13.     char *newFile = NULL;
  14.     // выделяем имя файла из абсолютного пути
  15.     char *pathBuf = strdup(filePath);
  16.     char *fileName = basename(pathBuf);
  17.  
  18.     newFile = (char *)calloc(strlen(dirPath) + strlen(fileName) + strlen("_encrypted") + 1, sizeof(char));
  19.     strcat(strcat(strcat(newFile, dirPath), fileName), "_encrypted");
  20.  
  21.     outFile = fopen(newFile, "wb");
  22.     if (outFile == NULL) {
  23.         LOGE("Error: cannot open file %s", newFile);
  24.         return false;
  25.     }
  26.  
  27.     //temp
  28.  
  29.     //uint8_t key[16] = { (uint8_t) 0x2b, (uint8_t) 0x7e, (uint8_t) 0x15, (uint8_t) 0x16, (uint8_t) 0x28, (uint8_t) 0xae, (uint8_t) 0xd2, (uint8_t) 0xa6, (uint8_t) 0xab, (uint8_t) 0xf7, (uint8_t) 0x15, (uint8_t) 0x88, (uint8_t) 0x09, (uint8_t) 0xcf, (uint8_t) 0x4f, (uint8_t) 0x3c };
  30.  
  31.     struct AES_ctx ctx;
  32.     AES_init_ctx(&ctx, key);
  33.  
  34.     // считывание данных блоками, AES-шифрование, запись шифротекста в новый файл
  35.     size_t readCtr = 0;
  36.     uint8_t buf[AES_BLOCKLEN] = {0};
  37.     while (readCtr = fread(buf, sizeof(char), AES_BLOCKLEN, inFile)) {
  38.         for (size_t i = 0; i < readCtr; i++) {
  39.             AES_ECB_encrypt(&ctx, buf);
  40.         }
  41.         fwrite(buf, sizeof(char), readCtr, outFile);
  42.     }
  43.  
  44.     fclose(inFile);
  45.     fclose(outFile);
  46.     free(newFile);
  47.     free(pathBuf);
  48.  
  49.     if (isRemove)
  50.         remove(filePath);
  51.  
  52.     return true;
  53. }
  54.  
  55. bool decryptFile(const char *filePath, const char *dirPath, uint8_t * key, bool isRemove) {
  56.     FILE *inFile = NULL;
  57.     FILE *outFile = NULL;
  58.     inFile = fopen(filePath, "rb");
  59.  
  60.     if (inFile == NULL) {
  61.         LOGE("Error: cannot open file %s", filePath);
  62.         return false;
  63.     }
  64.  
  65.     char *newFile = NULL;
  66.     // выделяем имя файла из абсолютного пути
  67.     char *pathBuf = strdup(filePath);
  68.     char *fileName = basename(pathBuf);
  69.  
  70.     // память под имя файла без _encrypted
  71.     char *cutFileName = (char *)calloc(strlen(fileName) - strlen("_encrypted"), sizeof(char));
  72.     strncpy(cutFileName, fileName, strlen(fileName) - strlen("_encrypted"));
  73.     // новая память под путь: папка decrypt + имя файла
  74.     newFile = (char *)calloc(strlen(dirPath) + strlen(cutFileName), sizeof(char));
  75.     strcat(strcat(newFile, dirPath), cutFileName);
  76.     free(cutFileName);
  77.  
  78.     outFile = fopen(newFile, "wb");
  79.     if (outFile == NULL) {
  80.         LOGE("Error: cannot open file %s", newFile);
  81.         return false;
  82.     }
  83.  
  84.     //temp
  85.  
  86.     //uint8_t key[16] = { (uint8_t) 0x2b, (uint8_t) 0x7e, (uint8_t) 0x15, (uint8_t) 0x16, (uint8_t) 0x28, (uint8_t) 0xae, (uint8_t) 0xd2, (uint8_t) 0xa6, (uint8_t) 0xab, (uint8_t) 0xf7, (uint8_t) 0x15, (uint8_t) 0x88, (uint8_t) 0x09, (uint8_t) 0xcf, (uint8_t) 0x4f, (uint8_t) 0x3c };
  87.  
  88.     struct AES_ctx ctx;
  89.     AES_init_ctx(&ctx, key);
  90.  
  91.     // считывание данных блоками, AES-шифрование, запись шифротекста в новый файл
  92.     size_t readCtr = 0;
  93.     uint8_t buf[AES_BLOCKLEN] = {0};
  94.     while (readCtr = fread(buf, sizeof(char), AES_BLOCKLEN, inFile)) {
  95.         for (size_t i = 0; i < readCtr; i++) {
  96.             AES_ECB_decrypt(&ctx, buf);
  97.         }
  98.         fwrite(buf, sizeof(char), readCtr, outFile);
  99.     }
  100.  
  101.     fclose(inFile);
  102.     fclose(outFile);
  103.     free(newFile);
  104.     free(pathBuf);
  105.  
  106.     if (isRemove)
  107.         remove(filePath);
  108.  
  109.     return true;
  110.  
  111. }
  112.  
  113. extern "C" JNIEXPORT jboolean JNICALL
  114. Java_io_nemiron_fileencryptor_utils_CipherData_decryptFile(JNIEnv *env, jobject, jstring key, jstring filePath, jstring dirPath) {
  115.     const char *nativeFilePath = env->GetStringUTFChars(filePath, JNI_FALSE);
  116.     const char *nativeDirPath = env->GetStringUTFChars(dirPath, JNI_FALSE);
  117.     uint8_t * nativeKey = (uint8_t*)env->GetStringUTFChars(key, JNI_FALSE);
  118.  
  119.     bool check = decryptFile(nativeFilePath, nativeDirPath, nativeKey, false);
  120.     env->ReleaseStringUTFChars(filePath, nativeFilePath);
  121.     env->ReleaseStringUTFChars(dirPath, nativeDirPath);
  122.  
  123.     if (!check)
  124.         return  JNI_FALSE;
  125.     return JNI_TRUE;
  126. }
  127.  
  128. extern "C" JNIEXPORT jboolean JNICALL
  129. Java_io_nemiron_fileencryptor_utils_CipherData_encryptFile(JNIEnv *env, jobject, jstring key, jstring filePath, jstring dirPath, jboolean isRemove) {
  130.  
  131.     const char *nativeFilePath = env->GetStringUTFChars(filePath, JNI_FALSE);
  132.     const char *nativeDirPath = env->GetStringUTFChars(dirPath, JNI_FALSE);
  133.     uint8_t * nativeKey = (uint8_t*)env->GetStringUTFChars(key, JNI_FALSE);
  134.  
  135.     bool check = encryptFile(nativeFilePath, nativeDirPath, nativeKey, isRemove);
  136.  
  137.     env->ReleaseStringUTFChars(filePath, nativeFilePath);
  138.     env->ReleaseStringUTFChars(dirPath, nativeDirPath);
  139.  
  140.     if (!check)
  141.         return JNI_FALSE;
  142.     return JNI_TRUE;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement