@Integration
How can we transmit an RSA-encrypted Aadhaar number to PHP using the following function, given that the encryption process is successful and validated in Postman?
we had create below function
function handleEncryption() {
const publicKey = MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAstWB95C5pHLXiYW59qyO 4Xb+59KYVm9Hywbo77qETZVAyc6VIsxU+UWhd/k/YtjZibCznB+HaXWX9TVTFs9N wgv7LRGq5uLczpZQDrU7dnGkl/urRA8p0Jv/f8T0MZdFWQgks91uFffeBmJOb58u 68ZRxSYGMPe4hb9XXKDVsgoSJaRNYviH7RgAI2QhTCwLEiMqIaUX3p1SAc178ZlN 8qHXSSGXvhDR1GKM+y2DIyJqlzfik7lD14mDY/I4lcbftib8cv7llkybtjX1Aayf Zp4XpmIXKWv8nRM488/jOAF81Bi13paKgpjQUUuwq9tb5Qd/DChytYgBTBTJFe7i rDFCmTIcqPr8+IMB7tXA3YXPp3z605Z6cGoYxezUm2Nz2o6oUmarDUntDhq/PnkN ergmSeSvS8gD9DHBuJkJWZweG3xOPXiKQAUBr92mdFhJGm6fitO5jsBxgpmulxpG 0oKDy9lAOLWSqK92JMcbMNHn4wRikdI9HSiXrrI7fLhJYTbyU3I4v5ESdEsayHXu iwO/1C8y56egzKSw44GAtEpbAkTNEEfK5H5R0QnVBIXOvfeF4tzGvmkfOO6nNXU3 o/WAdOyV3xSQ9dqLY5MEL4sJCGY1iJBIAQ452s8v0ynJG5Yq+8hNhsCVnklCzAls IzQpnSVDUVEzv17grVAw078CAwEAAQ==
;
var adharvalue = document.getElementById(“Aadhaarnumber”).value;
//alert(adharvalue);
const originalNumber = adharvalue;
if (adharvalue != “”) {
if (validateAadhar(adharvalue))
{
var verifybtn = document.getElementById(“verifyotpbtn”);
var getotpbtn = document.getElementById(“getotpbtn”);
var otpelementadharr = document.getElementById(“getotpaadhaar”);
const Aadhaarnumber = document.getElementById(‘Aadhaarnumber’);
Aadhaarnumber.setAttribute(‘disabled’, ‘’);
getotpbtn.setAttribute(‘disabled’, ‘’);
getotpbtn.classList.add(‘loading-button’);
const plaintext = adharvalue;
encryptWithPublicKey(publicKey, originalNumber.toString())
.then((encryptedData) => {
console.log("Original number: " + originalNumber);
console.log("Encrypted data: " + encryptedData);
// You can perform additional actions with the encrypted data here
//alert("Encrypted data: " + encryptedData);
const xhr = new XMLHttpRequest();
xhr.open(‘POST’, ‘abharegistersendotp.php’, true);
xhr.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
// Handle a successful response from the server
console.log(“XHR Success:”, xhr.responseText);
// You may want to handle the response accordingly
} else {
// Handle errors
console.error(“XHR Error:”, xhr.statusText);
}
}
};
const postData = ‘encryptedData=’ + encodeURIComponent(encryptedData);
xhr.send(postData);
})
.catch((error) => {
console.error(error);
// Handle errors appropriately
alert(“Encryption failed. Check the console for details.”);
});
}
else
{
alertmessage(‘danger’, ‘Invalid Aadhaar Number. Please enter a 12-digit number.’);
}
}
else {
alertmessage(‘danger’,‘Aadhaar Number Should not be blank.’);
}
}
async function encryptWithPublicKey(publicKey, message) {
const importedKey = await crypto.subtle.importKey(
"spki",
pemToBinary(publicKey),
{ name: "RSA-OAEP", hash: "SHA-1" },
false,
["encrypt"]
);
const encodedMessage = new TextEncoder().encode(message);
const encryptedData = await crypto.subtle.encrypt(
{ name: "RSA-OAEP" },
importedKey,
encodedMessage
);
const encryptedBase64 = btoa(String.fromCharCode(...new Uint8Array(encryptedData)));
return encryptedBase64;
}
function pemToBinary(pem) {
const lines = pem.split("\n").filter((line) => !line.includes("-----") && line.trim() !== "");
const encoded = lines.join("");
return Uint8Array.from(atob(encoded), (c) => c.charCodeAt(0));
}