Introduction In today's digital world, securing sensitive data during transmission is paramount. Cryptography is the science of securing communication and information by converting it into a format that unauthorized parties cannot understand. It enables confidentiality, integrity, and authentication of data, ensuring safe communication across networks.
Cryptography plays a vital role in achieving secure communication by enabling data encryption and decryption. One of the most powerful cryptographic techniques is asymmetric encryption, which uses public and private keys.
In this blog, we'll explore how public and private key cryptography works and demonstrate how to implement it using WeWeb for the frontend and Xano for the backend.
Encryption ensures that sensitive data is encoded into a secure format that unauthorized parties cannot read. Decryption is the process of converting the encrypted data back into its original format.
Types of Cryptography
Cryptography can be classified into the following types:
Asymmetric encryption solves the problem of securely sharing keys. It is widely used in web communication, including HTTPS.
For our implementation, we'll encrypt data on the WeWeb frontend and decrypt it on the Xano backend.
We will use the RSA algorithm for asymmetric encryption. You can generate a pair of public and private keys using libraries like jsrsasign (used in WeWeb) or tools like OpenSSL.
Generate Keys:
WeWeb supports adding custom JavaScript code via NPM plugins. We'll use the jsrsasign library for encryption and JWT generation.
Install the jsrsasign Library
Add the library via NPM in WeWeb:
npm install jsrsasign.
Generate JWT and Encrypt Data
Here is the custom JavaScript function to generate a JWT in the WeWeb workflow:
import { KJUR } from "jsrsasign";
function generateJWT() {
const oHeader = { alg: 'RS256', typ: 'JWT' };
const currentTime = KJUR.jws.IntDate.get('now');
const oPayload = {
email: variables.email, // Replace with actual variable reference
password: variables.password // Replace with actual variable reference
};
const sHeader = JSON.stringify(oHeader);
const sPayload = JSON.stringify(oPayload);
const privateKey = context.workflow[''].result; // Fetch private key securely
try {
const sJWT = KJUR.jws.JWS.sign(null, sHeader, sPayload, privateKey);
return sJWT;
} catch (error) {
console.error("JWT generation failed:", error);
return null;
}
}const token = generateJWT();
return token;
This function generates a signed JWT using the RS256 algorithm and includes the user email and password as the payload.
Sending JWT to Xano
Send the generated JWT to Xano using WeWeb's built-in API integration.
On the Xano backend, we'll verify and decode the JWT using the JWT Decode function. Xano's built-in JWT decode functionality allows you to provide the RSA public key for verification.
Using Xano's JWT Decode Function
Steps in Xano:
Xano will automatically validate the JWT and extract the payload.
Sample Data Flow
Security Best Practices
By combining the jsrsasign library on the frontend (WeWeb) and Xano's JWT decode functionality with the RSA public key, you can securely implement public and private key cryptography. This approach ensures safe data transmission, protecting sensitive information from unauthorized access.
Key Takeaways:
Next Steps: