В Laravel я использую встроенное шифрование класс для шифрования данных. Вот код Laravel для шифрования:
Код: Выделить всё
use Illuminate\Support\Facades\Crypt;
public function encryptData(Request $request) {
$data = $request->input('data');
$encryptedData = Crypt::encryptString($data);
return response()->json(['encryptedData' => $encryptedData]);
}
Код: Выделить всё
import CryptoJS from 'crypto-js';
// Defining an interface for expected JSON structure in the encrypted data
interface EncryptedDataFormat {
iv: string;
value: string;
}
export const decryptData = (encryptedData: string, key: string): string | null => {
try {
// Base64 decode the encrypted data to get the JSON string
const b64 = CryptoJS.enc.Base64.parse(encryptedData);
const json: EncryptedDataFormat = JSON.parse(b64.toString(CryptoJS.enc.Utf8));
// Extract IV and ciphertext from the parsed JSON
const iv = CryptoJS.enc.Base64.parse(json.iv);
const cipherText = json.value;
// Decrypt the ciphertext using the provided key and IV
const decrypted = CryptoJS.AES.decrypt(cipherText, CryptoJS.enc.Base64.parse(key), {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
// Convert the decrypted data to a UTF-8 string
const decryptedString = decrypted.toString(CryptoJS.enc.Utf8);
if (decryptedString) {
return decryptedString;
}
console.error("Decryption was successful but no data could be extracted.");
return null;
} catch (error) {
console.error("Decryption failed:", error);
return null;
}
};
Что может быть причиной этой несогласованности и как я могу обеспечить надежную расшифровку в React для данных, зашифрованных в Laravel?
Подробнее здесь: https://stackoverflow.com/questions/786 ... ecial-char