Skip to content

Commit 36c07e8

Browse files
committed
feat: add encryption decryption
1 parent e740a33 commit 36c07e8

20 files changed

+358
-254
lines changed

android/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
3030
SHARED
3131
./cpp/mediator.cpp
3232
./cpp/crypto.cpp
33+
./cpp/decryptor.cpp
3334
./cpp-adapter.cpp
3435
)
3536

@@ -52,6 +53,8 @@ if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
5253
POSITION_INDEPENDENT_CODE ON
5354
)
5455

56+
find_package(openssl REQUIRED CONFIG)
57+
5558
target_link_libraries(
5659
${PACKAGE_NAME}
5760
ReactAndroid::turbomodulejsijni
@@ -61,6 +64,7 @@ if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
6164
ReactAndroid::reactnativejni
6265
ReactAndroid::react_nativemodule_core
6366
android
67+
openssl::crypto
6468
)
6569
else()
6670
add_library(
@@ -69,6 +73,7 @@ else()
6973
../../react-native/ReactCommon/jsi/jsi/jsi.cpp
7074
./cpp/mediator.cpp
7175
./cpp/crypto.cpp
76+
./cpp/decryptor.cpp
7277
./cpp-adapter.cpp
7378
)
7479

@@ -86,8 +91,11 @@ else()
8691
POSITION_INDEPENDENT_CODE ON
8792
)
8893

94+
find_package(openssl REQUIRED CONFIG)
95+
8996
target_link_libraries(
9097
${PACKAGE_NAME}
9198
android
99+
openssl::crypto
92100
)
93101
endif()

android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,5 @@ dependencies {
164164
{
165165
implementation 'com.facebook.react:react-native:+'
166166
}
167+
implementation 'com.android.ndk.thirdparty:openssl:1.1.1l-beta-1'
167168
}

android/cpp/crypto.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11

2-
#include "crypto.h"
2+
#include "crypto.h"
33
#include <string>
4-
#include "encrypt.h"
4+
#include "decryptor.h"
55

66
using namespace std;
77

88
Crypto::Crypto() {
99

1010
}
1111

12+
string Crypto::getJniJsonStringyfyData(string key) {
13+
std::string base64Secret = "U2FsdGVkX1+uvOTVCval0JYJjna6el2v8OeJ5vxwRgB7YF3NnYJkIRK5NvygXy1y7LDt08z9Ub5oX/TmMr36VsWRjbaXf6cpgGNQq1eZ2Yq2yZAo/IQUWu0yCkDIri0hAesHSWaYALnma8qqbDeRfJlyoVXO73M37HdRsr85vo4=";
14+
std::string password = "asdf@1234";
15+
bool binary = false;
16+
std::string plaintext = decryptor::dec(base64Secret, password,binary);
1217

13-
std::string Crypto::getJniJsonStringyfyData(string key) {
14-
std::string jsonStringyfyData= "{\"secure1\":\"dev secure1 value 11 1200 11\",\"secure2\":\"dev secure2 value\",\"secure3\":\"dev secure3 value\"}";
1518
string hash;
16-
int len=jsonStringyfyData.length();
17-
char cahrtot[len+1];
18-
strcpy(cahrtot,jsonStringyfyData.c_str());
19-
hash=SHA256(cahrtot);
20-
string halfString=hash.substr(hash.length()/2);
19+
string halfString=base64Secret.substr(base64Secret.length()/2);
2120
if(key==halfString)
2221
{
23-
return jsonStringyfyData;
22+
return plaintext;
2423
}
2524
else
2625
{
2726
return "";
2827
}
29-
3028
}
3129

android/cpp/decryptor.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//
2+
// Created by Numan on 31/05/2023.
3+
//
4+
5+
#include "decryptor.h"
6+
7+
#include <stdexcept>
8+
#include <algorithm>
9+
#include <iterator>
10+
#include <openssl/evp.h>
11+
#include <openssl/aes.h>
12+
#include <openssl/bio.h>
13+
#include <openssl/buffer.h>
14+
#include <openssl/err.h>
15+
#include <openssl/md5.h>
16+
#include <openssl/rand.h>
17+
18+
std::vector<uint8_t> decryptor::base64_decode(const std::string& input) {
19+
BIO* b64 = BIO_new(BIO_f_base64());
20+
BIO* bmem = BIO_new_mem_buf(input.c_str(), input.length());
21+
bmem = BIO_push(b64, bmem);
22+
23+
BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
24+
std::vector<uint8_t> output(input.length());
25+
26+
int decoded_length = BIO_read(bmem, output.data(), output.size());
27+
BIO_free_all(bmem);
28+
29+
if (decoded_length < 0) {
30+
throw std::runtime_error("Base64 decoding error.");
31+
}
32+
33+
output.resize(decoded_length);
34+
return output;
35+
}
36+
37+
struct OpenSSLKeyResult {
38+
std::vector<uint8_t> key;
39+
std::vector<uint8_t> iv;
40+
};
41+
42+
decryptor::OpenSSLKeyResult decryptor::openSSLKey(const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt) {
43+
OpenSSLKeyResult result;
44+
45+
const EVP_CIPHER* cipher = EVP_get_cipherbyname("aes-256-cbc");
46+
std::vector<uint8_t> key(EVP_CIPHER_key_length(cipher));
47+
std::vector<uint8_t> iv(EVP_CIPHER_iv_length(cipher));
48+
49+
EVP_BytesToKey(
50+
cipher,
51+
EVP_md5(),
52+
salt.data(),
53+
password.data(),
54+
password.size(),
55+
1,
56+
key.data(),
57+
iv.data()
58+
);
59+
60+
result.key = key;
61+
result.iv = iv;
62+
63+
return result;
64+
}
65+
66+
std::vector<uint8_t> decryptor::rawDecrypt(const std::vector<uint8_t>& input, const std::vector<uint8_t>& key, const std::vector<uint8_t>& iv) {
67+
std::vector<uint8_t> output(input.size() + EVP_MAX_BLOCK_LENGTH);
68+
int output_length = 0;
69+
70+
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
71+
EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key.data(), iv.data());
72+
73+
EVP_DecryptUpdate(ctx, output.data(), &output_length, input.data(), input.size());
74+
75+
int final_length = 0;
76+
EVP_DecryptFinal_ex(ctx, output.data() + output_length, &final_length);
77+
78+
EVP_CIPHER_CTX_free(ctx);
79+
80+
output.resize(output_length + final_length);
81+
return output;
82+
}
83+
84+
std::string decryptor::dec(const std::string& input, const std::string& password, bool binary) {
85+
std::vector<uint8_t> cryptArr = base64_decode(input);
86+
std::vector<uint8_t> salt(cryptArr.begin() + 8, cryptArr.begin() + 16);
87+
88+
OpenSSLKeyResult pbe = openSSLKey(std::vector<uint8_t>(password.begin(), password.end()), salt);
89+
std::vector<uint8_t>& key = pbe.key;
90+
std::vector<uint8_t>& iv = pbe.iv;
91+
92+
cryptArr = std::vector<uint8_t>(cryptArr.begin() + 16, cryptArr.end());
93+
std::vector<uint8_t> decrypted = rawDecrypt(cryptArr, key, iv);
94+
95+
std::string result(decrypted.begin(), decrypted.end());
96+
return result;
97+
}

android/cpp/decryptor.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by Numan on 31/05/2023.
3+
//
4+
5+
#ifndef EXAMPLE_DECRYPTOR_H
6+
#define EXAMPLE_DECRYPTOR_H
7+
#include <string>
8+
#include <vector>
9+
10+
class decryptor {
11+
public:
12+
static std::string dec(const std::string& input, const std::string& password, bool binary);
13+
static std::vector<uint8_t> base64_decode(const std::string& input);
14+
struct OpenSSLKeyResult {
15+
std::vector<uint8_t> key;
16+
std::vector<uint8_t> iv;
17+
};
18+
static OpenSSLKeyResult openSSLKey(const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt);
19+
static std::vector<uint8_t> rawDecrypt(const std::vector<uint8_t>& input, const std::vector<uint8_t>& key, const std::vector<uint8_t>& iv);
20+
private:
21+
22+
23+
};
24+
25+
26+
#endif //EXAMPLE_DECRYPTOR_H

android/cpp/encrypt.h

Lines changed: 0 additions & 167 deletions
This file was deleted.

android/src/main/java/com/reactnativekeysjsi/PrivateKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
package com.reactnativekeysjsi;
44

55
public class PrivateKey {
6-
public static String privatekey="a581400cb48dad0a0aae5c777081f859";
6+
public static String privatekey="WRjbaXf6cpgGNQq1eZ2Yq2yZAo/IQUWu0yCkDIri0hAesHSWaYALnma8qqbDeRfJlyoVXO73M37HdRsr85vo4=";
77
}

0 commit comments

Comments
 (0)