ArticleZip > Encrypt In Javascript Decrypt In Php Using Public Key Cryptography

Encrypt In Javascript Decrypt In Php Using Public Key Cryptography

Encrypting and decrypting data securely is crucial when working with sensitive information in web applications. One effective way to achieve this is by using public key cryptography. In this article, we will explore how to encrypt data in JavaScript and then decrypt it in PHP using this powerful encryption technique.

Public key cryptography involves the use of a pair of cryptographic keys - a public key for encryption and a private key for decryption. This ensures that data encrypted with the public key can only be decrypted with the corresponding private key. By leveraging this mechanism, we can securely transmit sensitive data over the internet.

To get started, we need to generate a public and private key pair. We will first generate a key pair in JavaScript using the Web Crypto API. This API provides a secure environment for generating keys and performing cryptographic operations. Here's an example of generating a key pair in JavaScript:

Javascript

window.crypto.subtle.generateKey(
    {
        name: "RSA-OAEP",
        modulusLength: 2048,
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: { name: "SHA-256" },
    },
    true,
    ["encrypt", "decrypt"]
).then(function(keyPair) {
    // Use keyPair.publicKey for encryption and keyPair.privateKey for decryption
    console.log("Key pair generated successfully");
}).catch(function(err) {
    console.error("Error generating key pair:", err);
});

Next, let's encrypt the data using the public key generated in JavaScript. We will then transmit this encrypted data to a PHP backend for decryption. Here's how you can encrypt data in JavaScript:

Javascript

window.crypto.subtle.encrypt(
    {
        name: "RSA-OAEP"
    },
    publicKey,
    plainText // Data to be encrypted
).then(function(encrypted) {
    // Transmit encrypted data to PHP backend
    console.log("Data encrypted successfully");
}).catch(function(err) {
    console.error("Error encrypting data:", err);
});

On the PHP side, we will decrypt the data using the private key. PHP provides cryptographic functions that support RSA encryption and decryption. Here's an example of decrypting the data in PHP:

Php

$encryptedData = // Receive encrypted data from JavaScript
$privateKey = // Load private key here

openssl_private_decrypt(base64_decode($encryptedData), $decryptedData, $privateKey, OPENSSL_PKCS1_OAEP_PADDING);

if ($decryptedData !== false) {
    // Process decrypted data
    echo "Data decrypted successfully: " . $decryptedData;
} else {
    echo "Error decrypting data";
}

By following these steps, you can securely encrypt data in JavaScript using the public key and then decrypt it in PHP using the private key. This approach ensures that sensitive information remains protected during transmission between the client and server. Public key cryptography is a powerful tool for securing data communication and should be implemented wherever data security is a priority.

×