Work to improve encryption and decryption
This commit is contained in:
@@ -487,7 +487,8 @@ $effect(() => {
|
||||
// }
|
||||
|
||||
async function handle_decrypt_string(encrypted_string: string, passcode: string) {
|
||||
// log_lvl = 1;
|
||||
log_lvl = 1;
|
||||
|
||||
if (log_lvl) {
|
||||
console.log(`TEST: handle_decrypt_string: ${passcode}`, encrypted_string);
|
||||
}
|
||||
@@ -500,26 +501,33 @@ async function handle_decrypt_string(encrypted_string: string, passcode: string)
|
||||
return false;
|
||||
}
|
||||
|
||||
let combined_data = encrypted_string;
|
||||
let [encryption_iv_hex, encrypted_base64_string] = combined_data.split(':');
|
||||
encryption_iv = new Uint8Array(encryption_iv_hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
||||
let decrypted_string_2 = await ae_util.decrypt_wrapper(encrypted_string, passcode);
|
||||
if (log_lvl) {
|
||||
console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`);
|
||||
console.log('TEST: Decrypted string:', decrypted_string_2);
|
||||
}
|
||||
|
||||
// Decrypt the string using the journal key
|
||||
let decrypted_string = '';
|
||||
try {
|
||||
decrypted_string = await ae_util.decrypt_content(encrypted_base64_string, encryption_iv, passcode);
|
||||
} catch (error) {
|
||||
console.error('Error decrypting string:', error);
|
||||
alert('Failed to decrypt string. Please check the passcode.');
|
||||
return;
|
||||
}
|
||||
if (log_lvl) {
|
||||
console.log('Decrypted string:', decrypted_string);
|
||||
}
|
||||
return decrypted_string;
|
||||
return decrypted_string_2;
|
||||
|
||||
// let combined_data = encrypted_string;
|
||||
// let [encryption_iv_hex, encrypted_base64_string] = combined_data.split(':');
|
||||
// encryption_iv = new Uint8Array(encryption_iv_hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
||||
// if (log_lvl) {
|
||||
// console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`);
|
||||
// }
|
||||
|
||||
// // Decrypt the string using the journal key
|
||||
// let decrypted_string = '';
|
||||
// try {
|
||||
// decrypted_string = await ae_util.decrypt_content(encrypted_base64_string, encryption_iv, passcode);
|
||||
// } catch (error) {
|
||||
// console.error('Error decrypting string:', error);
|
||||
// alert('Failed to decrypt string. Please check the passcode.');
|
||||
// return;
|
||||
// }
|
||||
// if (log_lvl) {
|
||||
// console.log('Decrypted string:', decrypted_string);
|
||||
// }
|
||||
// return decrypted_string;
|
||||
}
|
||||
|
||||
async function handle_encrypt_string(text_string: string, passcode: string) {
|
||||
@@ -536,16 +544,24 @@ async function handle_encrypt_string(text_string: string, passcode: string) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let combined_data_2 = await ae_util.encrypt_wrapper(text_string, passcode);
|
||||
if (log_lvl) {
|
||||
console.log('TEST: Encrypted string:', combined_data_2);
|
||||
}
|
||||
return combined_data_2;
|
||||
|
||||
// Encrypt the string using the journal key
|
||||
let encrypted_base64 = await ae_util.encrypt_content(text_string, passcode);
|
||||
let encrypted_base64_string = encrypted_base64.base64;
|
||||
let encryption_iv = encrypted_base64.iv;
|
||||
console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`);
|
||||
// let encrypted_base64 = await ae_util.encrypt_content(text_string, passcode);
|
||||
// let encrypted_base64_string = encrypted_base64.base64;
|
||||
// let encryption_iv = encrypted_base64.iv;
|
||||
// console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`);
|
||||
|
||||
// const combined_data = ae_util.combine_iv_and_base64(encrypted_base64_string, encryption_iv);
|
||||
|
||||
// Combine the IV and encrypted content
|
||||
const combined_data = Array.from(encryption_iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + encrypted_base64_string;
|
||||
// const combined_data = Array.from(encryption_iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + encrypted_base64_string;
|
||||
|
||||
return combined_data;
|
||||
// return combined_data;
|
||||
}
|
||||
|
||||
// return new_string and cut_string
|
||||
@@ -1290,8 +1306,9 @@ function handle_cut_string(old_string: string) {
|
||||
// decrypted_content = result;
|
||||
// console.log('TEST: Decrypted content:', decrypted_content);
|
||||
// });
|
||||
decrypted_content = await ae_util.decrypt_wrapper(tmp_entry_obj?.content_encrypted, journal_key);
|
||||
|
||||
decrypted_content = await handle_decrypt_string(tmp_entry_obj?.content_encrypted, journal_key);
|
||||
// decrypted_content = await handle_decrypt_string(tmp_entry_obj?.content_encrypted, journal_key);
|
||||
tmp_entry_obj.content = decrypted_content;
|
||||
console.log('TEST: Decrypted content:', decrypted_content);
|
||||
} else {
|
||||
@@ -1453,7 +1470,8 @@ function handle_cut_string(old_string: string) {
|
||||
|
||||
// !tmp_entry_obj?.history &&
|
||||
if (tmp_entry_obj?.history_encrypted) {
|
||||
decrypted_history = await handle_decrypt_string(tmp_entry_obj.history_encrypted, journal_key);
|
||||
decrypted_history = await ae_util.decrypt_wrapper(tmp_entry_obj?.history_encrypted, journal_key);
|
||||
// decrypted_history = await handle_decrypt_string(tmp_entry_obj.history_encrypted, journal_key);
|
||||
console.log('Decrypted history:', decrypted_history);
|
||||
tmp_entry_obj.history = decrypted_history;
|
||||
}
|
||||
@@ -1489,7 +1507,9 @@ function handle_cut_string(old_string: string) {
|
||||
let history_cleaned: null|string = null;
|
||||
let history_md_html: null|string = null;
|
||||
|
||||
decrypted_history = await handle_decrypt_string(tmp_entry_obj.history_encrypted, journal_key);
|
||||
decrypted_history = await ae_util.decrypt_wrapper(tmp_entry_obj?.history_encrypted, journal_key);
|
||||
|
||||
// decrypted_history = await handle_decrypt_string(tmp_entry_obj.history_encrypted, journal_key);
|
||||
console.log('Decrypted history:', decrypted_history);
|
||||
tmp_entry_obj.history = decrypted_history;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user