Im trying to hide a map token i have , i dont want to store to store it locally in the client so im trying to encrypt it with AES ECB and send it back to the user in order to decrypt it and use it.
Im using crypto-js in node server and encrypt package in flutter app , here is the code :
Node Server :
const express = require('express');
const router = express.Router();
const crypto = require("crypto-js");
const Map_Token_Controller = {
async map_token(req, res) {
try {
const token = "ASDFGHJKLASDFGHJ";
const secretKey = "ASDFGHJKLASDFGHJ";
const iv = "ASDFGHJKLASDFGHJ";
const ciphertext = crypto.AES.encrypt(token, secretKey, { mode: crypto.mode.ECB, padding: crypto.pad.Pkcs7 }).toString();
res.status(200).json(ciphertext);
} catch (error) {
res.status(500).json({ error: error.message });
}
},
};
module.exports = Map_Token_Controller;
Flutter app :
//here i call the functions
Future<void> fetch_token() async {
try {
String token = await mapToken();
String token2 = await decryptMessage(token);
// print(token.runtimeType);
} catch (error) {
print('Error occurred: $error');
}
}
//API call
Future<String> mapToken() async {
try {
final response = await http.get(
Uri.parse('${dotenv.env['baseUrl']}/map_token'),
headers: requestHeaders,
);
if (response.statusCode == 200) {
final data = response.body;
print('Encrypted token: $data');
return data;
} else {
throw Exception('Failed to fetch encrypted data');
}
} catch (error) {
throw Exception('Error occurred: $error');
}
}
//here i made the decrypt
Future<String> decryptMessage(String token) async {
try {
//Key
//var iv = encrypt.IV.fromUtf8('ASDFGHJKLASDFGHJ');
var key = encrypt.Key.fromUtf8('ASDFGHJKLASDFGHJ');
final encrypter = encrypt.Encrypter(
encrypt.AES(key, mode: encrypt.AESMode.ecb, padding: 'PKCS7'));
//Decode
final decryptedToken = encrypter.decrypt(
encrypt.Encrypted.fromUtf8(token),
);
print(decryptedToken);
return decryptedToken;
} catch (error) {
throw Exception('Error occurred: $error');
}
}
I have tried to encoded and send it as base64 i have tried everything i dont know what else im doing wrong , i set padding also to null i have tried with iv and mode CBC also,still nothing .