Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- / шифрует строку XOR-шифрованием с ключом 0xff
- bool encryptFile(const char *filePath, const char *dirPath, uint8_t *key, bool isRemove)
- {
- FILE *inFile = NULL;
- FILE *outFile = NULL;
- inFile = fopen(filePath, "rb");
- if (inFile == NULL) {
- LOGE("Error: cannot open file %s", filePath);
- return false;
- }
- char *newFile = NULL;
- // выделяем имя файла из абсолютного пути
- char *pathBuf = strdup(filePath);
- char *fileName = basename(pathBuf);
- newFile = (char *)calloc(strlen(dirPath) + strlen(fileName) + strlen("_encrypted") + 1, sizeof(char));
- strcat(strcat(strcat(newFile, dirPath), fileName), "_encrypted");
- outFile = fopen(newFile, "wb");
- if (outFile == NULL) {
- LOGE("Error: cannot open file %s", newFile);
- return false;
- }
- //temp
- //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 };
- struct AES_ctx ctx;
- AES_init_ctx(&ctx, key);
- // считывание данных блоками, AES-шифрование, запись шифротекста в новый файл
- size_t readCtr = 0;
- uint8_t buf[AES_BLOCKLEN] = {0};
- while (readCtr = fread(buf, sizeof(char), AES_BLOCKLEN, inFile)) {
- for (size_t i = 0; i < readCtr; i++) {
- AES_ECB_encrypt(&ctx, buf);
- }
- fwrite(buf, sizeof(char), readCtr, outFile);
- }
- fclose(inFile);
- fclose(outFile);
- free(newFile);
- free(pathBuf);
- if (isRemove)
- remove(filePath);
- return true;
- }
- bool decryptFile(const char *filePath, const char *dirPath, uint8_t * key, bool isRemove) {
- FILE *inFile = NULL;
- FILE *outFile = NULL;
- inFile = fopen(filePath, "rb");
- if (inFile == NULL) {
- LOGE("Error: cannot open file %s", filePath);
- return false;
- }
- char *newFile = NULL;
- // выделяем имя файла из абсолютного пути
- char *pathBuf = strdup(filePath);
- char *fileName = basename(pathBuf);
- // память под имя файла без _encrypted
- char *cutFileName = (char *)calloc(strlen(fileName) - strlen("_encrypted"), sizeof(char));
- strncpy(cutFileName, fileName, strlen(fileName) - strlen("_encrypted"));
- // новая память под путь: папка decrypt + имя файла
- newFile = (char *)calloc(strlen(dirPath) + strlen(cutFileName), sizeof(char));
- strcat(strcat(newFile, dirPath), cutFileName);
- free(cutFileName);
- outFile = fopen(newFile, "wb");
- if (outFile == NULL) {
- LOGE("Error: cannot open file %s", newFile);
- return false;
- }
- //temp
- //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 };
- struct AES_ctx ctx;
- AES_init_ctx(&ctx, key);
- // считывание данных блоками, AES-шифрование, запись шифротекста в новый файл
- size_t readCtr = 0;
- uint8_t buf[AES_BLOCKLEN] = {0};
- while (readCtr = fread(buf, sizeof(char), AES_BLOCKLEN, inFile)) {
- for (size_t i = 0; i < readCtr; i++) {
- AES_ECB_decrypt(&ctx, buf);
- }
- fwrite(buf, sizeof(char), readCtr, outFile);
- }
- fclose(inFile);
- fclose(outFile);
- free(newFile);
- free(pathBuf);
- if (isRemove)
- remove(filePath);
- return true;
- }
- extern "C" JNIEXPORT jboolean JNICALL
- Java_io_nemiron_fileencryptor_utils_CipherData_decryptFile(JNIEnv *env, jobject, jstring key, jstring filePath, jstring dirPath) {
- const char *nativeFilePath = env->GetStringUTFChars(filePath, JNI_FALSE);
- const char *nativeDirPath = env->GetStringUTFChars(dirPath, JNI_FALSE);
- uint8_t * nativeKey = (uint8_t*)env->GetStringUTFChars(key, JNI_FALSE);
- bool check = decryptFile(nativeFilePath, nativeDirPath, nativeKey, false);
- env->ReleaseStringUTFChars(filePath, nativeFilePath);
- env->ReleaseStringUTFChars(dirPath, nativeDirPath);
- if (!check)
- return JNI_FALSE;
- return JNI_TRUE;
- }
- extern "C" JNIEXPORT jboolean JNICALL
- Java_io_nemiron_fileencryptor_utils_CipherData_encryptFile(JNIEnv *env, jobject, jstring key, jstring filePath, jstring dirPath, jboolean isRemove) {
- const char *nativeFilePath = env->GetStringUTFChars(filePath, JNI_FALSE);
- const char *nativeDirPath = env->GetStringUTFChars(dirPath, JNI_FALSE);
- uint8_t * nativeKey = (uint8_t*)env->GetStringUTFChars(key, JNI_FALSE);
- bool check = encryptFile(nativeFilePath, nativeDirPath, nativeKey, isRemove);
- env->ReleaseStringUTFChars(filePath, nativeFilePath);
- env->ReleaseStringUTFChars(dirPath, nativeDirPath);
- if (!check)
- return JNI_FALSE;
- return JNI_TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement