Skip to content

Commit

Permalink
resolved: encrypt decrypt errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavanakarwade committed Dec 6, 2024
1 parent fb02c0f commit 803fe1c
Showing 1 changed file with 30 additions and 45 deletions.
75 changes: 30 additions & 45 deletions src/api/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,61 +226,46 @@ export const passwordEncryption = (password: string): string => {
return encryptedPassword
}

// export const encryptData = (value: any): string => {
export const encryptData = (value: any): string => {

// const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`

// try {
// if (typeof (value) !== 'string') {
// value = JSON.stringify(value)
// }
// return CryptoJS.AES.encrypt(value, CRYPTO_PRIVATE_KEY).toString();
// } catch (error) {
// // Handle encryption error
// console.error('Encryption error:', error);
// return '';
// }
// }

export const encryptData = (value: any): string => {
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`

let encJson = CryptoJS.AES.encrypt(JSON.stringify((value) !== 'string'),CRYPTO_PRIVATE_KEY).toString()
let encData = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encJson))
return encData
try {
if (typeof (value) !== 'string') {
value = JSON.stringify(value)
}
return CryptoJS.AES.encrypt(value, CRYPTO_PRIVATE_KEY).toString();
} catch (error) {
// Handle encryption error
console.error('Encryption error:', error);
return '';
}
}

// export const decryptData = (value: string): string => {
// const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`;

// try {
// // Ensure input is valid and not empty
// if (!value || typeof value !== "string") {
// throw new Error("Invalid input for decryption");
// }
export const decryptData = (value: string): string => {
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`;

// const bytes = CryptoJS.AES.decrypt(value, CRYPTO_PRIVATE_KEY);
// const decryptedText = bytes.toString(CryptoJS.enc.Utf8);
try {
// Ensure input is valid and not empty
if (!value || typeof value !== "string") {
throw new Error("Invalid input for decryption");
}

// // Ensure the output is valid UTF-8
// if (!decryptedText) {
// throw new Error("Decryption failed or returned empty result");
// }
const bytes = CryptoJS.AES.decrypt(value, CRYPTO_PRIVATE_KEY);
const decryptedText = bytes.toString(CryptoJS.enc.Utf8);

// return decryptedText;
// } catch (error) {
// console.error("Decryption error:", error);
// return ''; // Return a fallback value to prevent crashes
// }
// };
// Ensure the output is valid UTF-8
if (!decryptedText) {
throw new Error("Decryption failed or returned empty result");
}

export const decryptData = (value: string): string => {
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`;
return decryptedText;
} catch (error) {
console.error("Decryption error:", error);
return ''; // Return a fallback value to prevent crashes
}
};

let decData = CryptoJS.enc.Base64.parse(value).toString(CryptoJS.enc.Utf8)
let bytes = CryptoJS.AES.decrypt(decData, CRYPTO_PRIVATE_KEY).toString(CryptoJS.enc.Utf8)
return bytes;
}

export const setToLocalStorage = async (key: string, value: any) =>{
// If passed value is object then checked empty object
Expand Down

0 comments on commit 803fe1c

Please sign in to comment.