Skip to content

Commit

Permalink
Merge pull request #44 from piyushbhavsar12/dev
Browse files Browse the repository at this point in the history
Encryption Decryption API Update
  • Loading branch information
piyushbhavsar12 authored Jan 2, 2025
2 parents 8375d15 + a3c4b67 commit 4c6cc80
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const AADHAAR_GREETING_MESSAGE = (
) => `Beneficiary Name - ${BeneficiaryName}
Beneficiary Location - ${StateName}, ${DistrictName}, ${SubDistrictName}, ${VillageName}
Registration Number - ${Reg_No}
Registration Date - ${moment(DateOfRegistration).format('DD-MM-YYYY')}
Registration Date - ${moment(DateOfRegistration).format('M/D/YYYY h:mm:ss A')}
Last Installment Status - ${LatestInstallmentPaid==0?"No":addOrdinalSuffix(LatestInstallmentPaid)} Installment payment done
eKYC - ${eKYC_Status=='Y'?'Done':'Not Done'}
`
152 changes: 118 additions & 34 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const fetch = require("./fetch");
import { ConstraintMetadata } from "class-validator/types/metadata/ConstraintMetadata";
import { randomBytes, createCipheriv, createDecipheriv } from 'crypto';
const crypto = require('crypto');

const fetch = require("node-fetch");
const { Headers } = fetch;
const { Logger } = require('@nestjs/common');
const { HttpService } = require('@nestjs/axios');
Expand Down Expand Up @@ -151,29 +155,108 @@ export const wordToNumber = (input, type = "benId") => {
}
};

export const getUniqueKey = (maxSize = 15): string => {
const chars = "abcdefghijklmnopqrstuvwxyz";
const data = new Uint8Array(maxSize);
crypto.getRandomValues(data);

const result = Array.from(data)
.map(byte => chars[byte % chars.length])
.join('')
.toUpperCase();
console.log(result)
return result;
}


//Encryption Method
export function encrypt(textToEncrypt: string, key: string): string {
try {
// Ensure the key is 16 bytes (AES-128)
const keyBytes = Buffer.alloc(16, 0); // Create a 16-byte buffer filled with zeros
const pwdBytes = Buffer.from(key, 'utf8');
pwdBytes.copy(keyBytes, 0, 0, Math.min(pwdBytes.length, keyBytes.length));

const iv = keyBytes; // Use the same value for IV and key
const cipher = crypto.createCipheriv('aes-128-cbc', keyBytes, iv);

// Encrypt the plaintext
const encrypted = Buffer.concat([
cipher.update(Buffer.from(textToEncrypt, 'utf8')),
cipher.final(),
]);

// Return the encrypted text as Base64
return encrypted.toString('base64');
} catch (error) {
console.error("Error while encrypting the message:", error);
return "Error while encrypting the message."
}
}




//Decryption Method
function decrypt(textToDecrypt: string, key: string): string {
try {
const keyBytes = Buffer.alloc(16); // Create a buffer of 16 bytes for the key
const pwdBytes = Buffer.from(key, 'utf-8'); // Convert the key to bytes
const len = Math.min(pwdBytes.length, keyBytes.length);
pwdBytes.copy(keyBytes, 0, 0, len); // Copy the key into the buffer

const encryptedData = Buffer.from(textToDecrypt, 'base64'); // Convert the encrypted text from Base64 to bytes

// Initialize the cipher configuration
const decipher = createDecipheriv('aes-128-cbc', keyBytes, keyBytes);
decipher.setAutoPadding(false); // Set auto padding to false

// Decrypt the data
let decrypted = decipher.update(encryptedData);
decrypted = Buffer.concat([decrypted, decipher.final()]);

// Convert the decrypted data to a UTF-8 string
let decryptedText = decrypted.toString('utf-8');

// Trim the decrypted text to remove padding and get the JSON object
const lastIndex = decryptedText.lastIndexOf('}');
const trimmedText = lastIndex !== -1 ? decryptedText.substring(0, lastIndex + 1) : decryptedText;

return trimmedText;
} catch (error) {
console.error("Error while decrypting the message:", error);
return "Error while decrypting the message."
}
}

export const encryptRequest = async (text: string) => {
try {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
logger.log("text: ", text);
var raw = JSON.stringify({
EncryptedRequest: text,
});
console.log("text to encrypt is : ", text);
// var myHeaders = new Headers();
// myHeaders.append("Content-Type", "application/json");
// console.log("text: ", text);
// var raw = JSON.stringify({
// EncryptedRequest: text,
// });

var requestOptions: any = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
// var requestOptions: any = {
// method: "POST",
// headers: myHeaders,
// body: raw,
// redirect: "follow",
// };

let response: any = await fetch(
`${process.env.PM_KISAN_ENC_DEC_API}/EncryptedRequest`,
requestOptions
);
response = await response.json();
return response;
// let response: any = await fetch(
// `${process.env.PM_KISAN_ENC_DEC_API}/EncryptedRequest`,
// requestOptions
// );

// Extract Token from the parsed text

// response = await response.json();

} catch (error) {
console.error("Error while encrypting the message:", error);
return {
error: "Error while encrypting the message.",
};
Expand All @@ -182,25 +265,26 @@ export const encryptRequest = async (text: string) => {

export const decryptRequest = async (text: string, token: string) => {
try {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

console.log("the text for decryption is : ", text);
console.log("the token for decryption is : ", token);
var raw = JSON.stringify({
DecryptedRequest: `${text}@${token}`,
});
// text = "jhp8OW+FdOFZJck8eIm6mx1DSWwPghgYKFRwu7e+Ppj72A++R10Vaa7p7+KtLTQtnaK2mZv3I8TwiJo+pr1jjnrh/2cRjlK23REX2mJf10osnDpD2AFI8ihoFb/ShNAReW4Jj5fqVGdPYVX8peWn51Cu2iD0WouyOHrl9OwZ4b8="
// var requestOptions: any = {
// method: "POST",
// headers: myHeaders,
// body: raw,
// redirect: "follow",
// };

var requestOptions: any = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};

let response = await fetch(
`${process.env.PM_KISAN_ENC_DEC_API}/DecryptedRequest`,
requestOptions
);
response = await response.json();
// let response = await fetch(
// `${process.env.PM_KISAN_ENC_DEC_API}/DecryptedRequest`,
// requestOptions
// );
let response = await decrypt(text, token);
// response = await response.json();
console.log("the response from decrypt request: ", response);
return response;
} catch (error) {
return {
Expand Down
117 changes: 82 additions & 35 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Injectable, Logger } from "@nestjs/common";
import { PrismaService } from "../../global-services/prisma.service";
import { ConfigService } from "@nestjs/config";
import axios from "axios";
import { decryptRequest, encryptRequest } from "../../common/utils";
import { decryptRequest, encrypt, encryptRequest } from "../../common/utils";
import { Message } from "@prisma/client";
import { MonitoringService } from "../monitoring/monitoring.service";
import { getUniqueKey } from "../../common/utils";

@Injectable()
export class UserService {
Expand All @@ -19,15 +20,25 @@ export class UserService {

async sendOTP(mobileNumber: string, type: string = "Mobile"): Promise<any> {
try {
let encryptedData = await encryptRequest(
`{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"Token\":\"${this.configService.get(
"PM_KISSAN_TOKEN"
)}\"}`
);
this.logger.log("encrypted data: ", encryptedData);
let data = JSON.stringify({
EncryptedRequest: `${encryptedData.d.encryptedvalu}@${encryptedData.d.token}`,
});
// let encryptedData = await encryptRequest(
// `{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"Token\":\"${this.configService.get(
// "PM_KISSAN_TOKEN"
// )}\"}`
// );
let key = getUniqueKey();
let requestData = `{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"Token\":\"${this.configService.get("PM_KISSAN_TOKEN")}\"}`;
console.log("Request data: ", requestData);
let encrypted_text = await encrypt(requestData, key); //without @

console.log("encrypted text without @: ", encrypted_text);
// let data = JSON.stringify({
// EncryptedRequest: `${encryptedData.d.encryptedvalu}@${encryptedData.d.token}`,
// });
let data = {
"EncryptedRequest":`${encrypted_text}@${key}`
};

console.log("(in sendOTP)the data in the data var is as: ", data);

let config = {
method: "post",
Expand All @@ -38,16 +49,18 @@ export class UserService {
},
data: data,
};

let response: any = await axios.request(config);
this.logger.log("sendOTP", response.status);
if (response.status >= 200 && response.status < 300) {
response = await response.data;
let decryptedData: any = await decryptRequest(
response.d.output,
encryptedData.d.token
key
);
response.d.output = JSON.parse(decryptedData.d.decryptedvalue);
const parsedData = JSON.parse(decryptedData); // Convert JSON string to an object
// const values = parsedData.Values; // Access the Values property
// console.log("Values:", values);
response.d.output = parsedData;
response["status"] =
response.d.output.Rsponce != "False" ? "OK" : "NOT_OK";
return response;
Expand All @@ -62,7 +75,7 @@ export class UserService {
};
}
} catch (error) {
this.logger.error(error);
console.error("Error in sendOTP:", error.message, error.response?.data || error);
return {
d: {
output: {
Expand All @@ -80,15 +93,33 @@ export class UserService {
type: string = "Mobile"
): Promise<any> {
try {
let encryptedData = await encryptRequest(
`{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"OTP\":\"${otp}\",\"Token\":\"${this.configService.get(
"PM_KISSAN_TOKEN"
)}\"}`
);
let data = JSON.stringify({
EncryptedRequest: `${encryptedData.d.encryptedvalu}@${encryptedData.d.token}`,
});
// let encryptedData = await encryptRequest(
// `{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"OTP\":\"${otp}\",\"Token\":\"${this.configService.get(
// "PM_KISSAN_TOKEN"
// )}\"}`
// );
// const requestData = `{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"Token\":\"${this.configService.get("PM_KISSAN_TOKEN")}\"}`;
let requestData = `{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"OTP\":\"${otp}\",\"${this.configService.get("PM_KISSAN_TOKEN")}\"}`;
console.log("Request data: ", requestData);
let key = getUniqueKey();
// const requestData = JSON.stringify({
// Types: type,
// Values: mobileNumber,
// Token: ""
// });
let encrypted_text = await encrypt(requestData, key); //without @


console.log("encrypted text without @: ", encrypted_text);

// let data = JSON.stringify({
// EncryptedRequest: `${encryptedData.d.encryptedvalu}@${encryptedData.d.token}`,
// });
let data = {
// EncryptedRequest: `${encryptedData}`
"EncryptedRequest": `${encrypted_text}@${key}`,
};
console.log("(inside verifyOTP)the data in the data var is : ", data);
let config = {
method: "post",
maxBodyLength: Infinity,
Expand All @@ -107,10 +138,11 @@ export class UserService {
response = await response.data;
let decryptedData: any = await decryptRequest(
response.d.output,
encryptedData.d.token
key
);
this.logger.log(decryptedData);
response.d.output = JSON.parse(decryptedData.d.decryptedvalue);
console.log("Response of VerifyOTP",response);
console.log("Response from decryptedData(verifyOTP)",decryptedData);
// response.d.output = JSON.parse(decryptedData);
response["status"] =
response.d.output.Rsponce != "False" ? "OK" : "NOT_OK";
return response;
Expand Down Expand Up @@ -143,14 +175,27 @@ export class UserService {
): Promise<any> {
let res: any;
try {
let encryptedData = await encryptRequest(
`{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"Token\":\"${this.configService.get(
"PM_KISSAN_TOKEN"
)}\"}`
);
let data = JSON.stringify({
EncryptedRequest: `${encryptedData.d.encryptedvalu}@${encryptedData.d.token}`,
});
// let encryptedData = await encryptRequest(
// `{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"Token\":\"${this.configService.get(
// "PM_KISSAN_TOKEN"
// )}\"}`
// );
const requestData = `{\"Types\":\"${type}\",\"Values\":\"${mobileNumber}\",\"Token\":\"${this.configService.get("PM_KISSAN_TOKEN")}\"}`;
console.log("Request data: ", requestData);
let key = getUniqueKey();
// const requestData = JSON.stringify({
// Types: type,
// Values: mobileNumber,
// Token: ""
// });
let encrypted_text = await encrypt(requestData, key); //without @
console.log("encrypted text without @: ", encrypted_text);
// let data = JSON.stringify({
// EncryptedRequest: `${encryptedData.d.encryptedvalu}@${encryptedData.d.token}`,
// });
let data = {
"EncryptedRequest": `${encrypted_text}@${key}`,
};

let config = {
method: "post",
Expand All @@ -169,9 +214,11 @@ export class UserService {
res = await res.data;
let decryptedData: any = await decryptRequest(
res.d.output,
encryptedData.d.token
key
);
res.d.output = JSON.parse(decryptedData.d.decryptedvalue);
console.log("Response of getUserData",res);
console.log("decrypted data(from getUserData): ", decryptedData);
res.d.output = JSON.parse(decryptedData);
res["status"] = res.d.output.Rsponce != "False" ? "OK" : "NOT_OK";
} else {
this.monitoringService.incrementUnableToGetUserDetailsCount();
Expand Down
Loading

0 comments on commit 4c6cc80

Please sign in to comment.