Open lnpu opened 4 years ago
i am also getting same error
I also have the same problem. When I encrypt the data and decrypt them at the same time, everything is ok. But if I do some process and try it later I'm getting Error: Malformed UTF-8 data.
same error,anyone can help?
+1,same error,anyone can help?
//加密方法aes
export function Encrypt(word, key = 'share') {
let encJson = CryptoJS.AES.encrypt(JSON.stringify(word), key).toString()
let encData = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encJson))
return encData
}
//解密方法aes
export function Decrypt(word, key = 'share') {
let decData = CryptoJS.enc.Base64.parse(word).toString(CryptoJS.enc.Utf8)
let bytes = CryptoJS.AES.decrypt(decData, key).toString(CryptoJS.enc.Utf8)
return JSON.parse(bytes)
}
thanks @cangyu1993 it was the solution for me =)
Now calculating the hash with the hashing scheme 'HMAC-SHA256' with the secret key 0F5DD14AE2
Resultant hash: c7689cda7474eb1adcd343fd0c0b676bad0ba66361cc46db589bdb0da4c1c867
Merchant should also ensure that UTF-8 encoding is used to convert the input from a printable string to a byte array. Also Note that 7-bit ASCII encoding is unchanged for UTF-8.
How Can I achieve that???
I followed the advice of cangyu1993 but could not resolve the problem!
So I took the following steps.
If there are any comrades who have the same problem, please refer to the following.
Replace this.configService.get('cryptoJs.secretKey')
=> with your private key
The point was to composite and encrypt with CryptoJS.enc.Latin1
public getEncodePath(decodePath: string): string {
const json = CryptoJS.AES.encrypt(
JSON.stringify(decodePath),
this.SECRET_KEY
).toString()
return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Latin1.parse(json))
}
public getDecodePath(encodePath: string): string {
const decodePath = decodeURIComponent(encodePath)
const data = CryptoJS.enc.Base64.parse(decodePath.toString()).toString(
CryptoJS.enc.Latin1
)
const bytes = CryptoJS.AES.decrypt(data, this.SECRET_KEY).toString(
CryptoJS.enc.Utf8
)
return JSON.parse(bytes)
}
solve
//加密方法aes export function Encrypt(word, key = 'share') { let encJson = CryptoJS.AES.encrypt(JSON.stringify(word), key).toString() let encData = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encJson)) return encData } //解密方法aes export function Decrypt(word, key = 'share') { let decData = CryptoJS.enc.Base64.parse(word).toString(CryptoJS.enc.Utf8) let bytes = CryptoJS.AES.decrypt(decData, key).toString(CryptoJS.enc.Utf8) return JSON.parse(bytes) }
I got same error and after some debugging I realized , when we store encrypted data in the local storage it add ""
at the starting and ending of the encrypted string. ""encrypted_string""
. when we try to decrypt our encrypted string that stored in localstorage , it try to decrypt it according to "enrypted_string"
but it receives ""encrypted_string""
. therefore we receive this error from local storage. In otherword, it is because of mismatching our encrypted string.
what i did is replaced extra ""
from string and then decrypted it. it worked for me
let data = // get_data_from_localstroage
let newExactString = data.replaceAll('"', '');
// our remaining decryption process
I got same error and after some debugging I realized , when we store encrypted data in the local storage it add
""
at the starting and ending of the encrypted string.""encrypted_string""
. when we try to decrypt our encrypted string that stored in localstorage , it try to decrypt it according to"enrypted_string"
but it receives""encrypted_string""
. therefore we receive this error from local storage. In otherword, it is because of mismatching our encrypted string.
Same, this changes fixed it. Thanks!! 🎉
logic code:
keyArr : ["59ba0f3b71f1c48f","6c56eb9a9ae3f2e8"]
let keyArr = this.key(timestamp);
console.log("key:",JSON.stringify(keyArr));
let key = CryptoJS.enc.Utf8.parse(keyArr[1]);
let iv = CryptoJS.enc.Utf8.parse(keyArr[0]);
try {
let decrypted = CryptoJS.AES.decrypt(str, key, {iv: iv, padding: CryptoJS.pad.Pkcs7});
return decrypted.toString(CryptoJS.enc.Utf8);
} catch (e) {
console.warn('字符串有问题')
let decrypted = CryptoJS.AES.decrypt(JSON.stringify({str}), key, {iv: iv, padding: CryptoJS.pad.Pkcs7});
console.warn(decrypted);
return decrypted.toString(CryptoJS.enc.Utf8);
}
code: return decodeURIComponent(escape(Latin1.stringify(wordArray)));
file name: core.js
/**
* UTF-8 encoding strategy.
*/
var Utf8 = C_enc.Utf8 = {
/**
* Converts a word array to a UTF-8 string.
*
* @param {WordArray} wordArray The word array.
*
* @return {string} The UTF-8 string.
*
* @static
*
* @example
*
* var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
*/
stringify: function (wordArray) {
try {
return decodeURIComponent(escape(Latin1.stringify(wordArray)));
} catch (e) {
throw new Error('Malformed UTF-8 data');
}
},
/**
* Converts a UTF-8 string to a word array.
*
* @param {string} utf8Str The UTF-8 string.
*
* @return {WordArray} The word array.
*
* @static
*
* @example
*
* var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
*/
parse: function (utf8Str) {
return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
}
};
wordArray = {"words":[662783787,-595780477,1240360354,328894481,-1342870603,-1871986468,-846486436,-215096290,1333282316,-1915982514,2062575374,1034298291,-591074769,-253370965,1586894295,-1401963879,-1518074259,1471480545,-2039534547,1978237172,1799392137,-2044471085,1787303371,-872658888,2007843255,-2098934614,1135756607,-1309123539,257887438,-163396468,310073464,2127234984,-483125692,1080784552,-1167297,-1960255947,1857222978,440329079,-2101739184,-772799216,901863145,466249350,-1055034133,1297680167,-827313189,81078289,-1657331764,-1767931611,2065950318,-602579389,-1047153412,-1544756005,-1647431463,-957191025,-52567983,-448067314,-74470446,-197003447,-1998540919,198439887,-34790368,1417250341,-1919250117,-1597732977,1496890005,-1957752675,1070888491,573373599,-564100808,-1130180434,-106698934,1359825758,-545726935,-1182776072,1493887619,837950103,-173914669,2086114057,-923848470,1305404275,2123822853,1423502819,-1991350542,244009565,-2049121041,218413331,-676293989,1747348094,329248302,-2083349430,391438431,617087811,1585292479,-860695020,-1269856127,917390152,-655057333,252182965,-1488967731,-1110307512,-604248608,-375316483,1677407784,2079512656,-1035726067,-1286901796,481792725,-68277410,-1147243484,-524808900,1032778243,-1867288406,-571954655,-1461048912,-1224771343,-1722347386,-683456034,1421274621,-64476995,-1943143374,1935757138,-966879669,-985370949,-892770129,-786328702,-1040372620,94478989,-128522452,532768305,-1816190862,-1509010159,457803886,756277512,615446020,794592155,-317144349,1109899367,1534625759,-612181140,-385611715,1709570000,1725828409,-536752057,-1790040395,1764166168,-673685667,-1453428972,-1371007422,656683894,-215083255,347828021,913324715,-445049510,1557621552,-736495164,-889375131,1900115530,384765325,-161369511,-465260670,1762183651,-115741735,2120514618,-199633394,2043533075,-122082824,-113326769,-73659976,-266708001,1592987371,1524866047,422710032,1975250024,-975177668,304417658,2053914142,1258753766,-2037631120,-1917215465,1719019706,-918850027,-828250658,13451467,-1347840015,-1032383124,231333942,-1906017283,1821898117,1645564877,-1751009794,-1969773040,-2112685017,1603164900,1259302154,414183528,56697899,-1145998360,-2045070269,-347554193,1967033372,-1569556365,611143531,-311321342,794388132,-402570441,-1447234584,-137555076,1234916331,290737433,-970069184,994184266,1963510239,1537660855,1945412727,-1984393615,22684664,1968027442,-163191927,-1928612519,-1149116191,-711646820,-1681731440,-1952604617,-1631709545,-1895932354,-1742839762,-23660094,1174589704,-202538041,-1098784653,-1468707993,1439849257,1354541128,-1166811143,-350882802,-24369455,1907649906,-963874565,1563000548,-505880305,311388484,-464767342,2106096143,1236794640,-547526738,-1486460599,811950840,1567071867,255294642,1045067958,-1426598964,-1036097619,2059988220,2060636783,-332843326,-1142199428,681468041,-158679105,-1856930878,1370400410,715919790,-1353934296,1131651023,524597104,-122493717,1094188414,-779326858,-1691945027,1032705938,1741858763,426760714,-755055245,587439086,-1592014481,-1402598979,-84447924,1849257554,872926183,1955533781,1766831134,-1280468640,-86652732,965442858,426687053,-628316930,586731920,-1702907573,1643684965,-1664645882,2039530178,-1797724644,1541419958,-61075100,1368180514,852273721,-790023173,-767958405,3668068,-945960479,-1852972610,1541304321,-819812043,-1726197069,1850241568,-1782924713,-1093675030,1479273926,2026593407,834374195,1654405521,-242586815,1311988711,-1373352944,-1841412184,-337341756,1678758831,-2047888978,-1055108605,-989562122,1533238900,1394850067,-1851908779,1158409690,1629737671,-1292719205,-1104179113,-60409186,1881017509,-1486770236,287295909,-732729686,1075095461,1946341988,305628998,392842715,-1510754665,56402499,-896433242,703533365,-1027840030,733574520,2123383830,1618688771,1810374755,-1378795465,749413994,1458813427,1260903128,972369881,-1757643026,1447894861,-505047702,1553663496,-474885427,-1556856082,-1760028461,-143889988,-79284535,1787476558,1936396162,227049626,-742542581,-546649067,2044767836,-259553045,1413667698,-184056105,-811608936,323103536,201904533,-1902226784,1956638498,1121199911,-625530300,-485503716,600943647,935061702,1201690289,-825465527,1631373512,-314493819,-599010437,222787795,968750905,-2096627163,-512813865,-1531408645,1006231206,-1062361889,697262973,391683053,811592427,-2127082292,-1186910964,-1253312212,-331282737,1619718628,167020057,-249470469,-233377289,-1565951880,523342705,-447243966,400698928,1121234817,1219379136,2007232399,-914092936,1409726492,1422375827,1213224194,292710033,1406867605,1515647925,1184152265,-1500576823,1994671286,1146572266,-742125557,-2011493195,245254937,-1843635556,-1991418638,-1743219463,-1463691455,2142934773,-1074514688,-539894646,1318065745,1811947425,-1303638496,-1868962495,877880890,1228419466,768886283,-2126067246,638839166,-1733989123,309752910,-746051849,-2069089683,807674871,578646677,93622016,-1308821587,-1037360601,1952858891,1333805153,-673916265,365873514,-749448947,-407940647,-956577597,-442094285,972574823,-723839379,-1568008543,-1074855716,875358696,1245501696,-2011303448,1041167876,-912666939,-893575501,-1696790057,-633159015,321524444,-1059875841,207900067,-2067788889,1314396833,-1016202553,-238499297,2059983926,-970559884,899210353,90018110,-2141124767,1736640982,-588871599,-654489174,-745846782,1632116080,-322030226,-1590550873,-1630790794,237145449,1140401218,-45506606,1929992924,-1108545703,-2032272005,-42064344,-980628469,-619278010,-627872133,1614587973,1360859741,2035579647,-1306140354,-502466155,-563942498,-1274897766,1257041172,1798863548,-310624852,1684259091,-625573519,-1101586051,-670330110,898478279,71280037,-2064724816,-2010893010,-897121481,-928005204,1012624926,-696264960,-415745171,898337690,1096916841,-1872260691,-933081310,-2032488014,1238100263,-108972002,66076488,1077291522,1427741037,227299186,612349535,670915050,1490016465,2076445148,826564635,2076814501,-472614241,-1257407304,-411423691,-857157619,-1674297334,-495713349,-391813898,1955772495,375905657,-1486320500,1396784072,-306448695,-581110473,-544921067,537404145,1035276008,281137779,2147181195,748231236,1445235448,532486756,-1109923778,-1836868832,-251307704,-1871067865,-1603199142,-1271044966,2094454215,265702944,-1818475622,-1106350586,2072087142,-1739496823,-242689362,157933862,-757301992,1525014092,1388863003,-1803154660,1937300857,766026534,349671397,-412485100,-1681317576,726195843,-173633747,-943622299,297644056,1630074104,652610047,248308555,1101706167,-237569051,-538152042,855985645,-664503411,-1802862854,1823998309,348278952,-20251700,-1792574769,1166043474,-162533144,1910070564,331460278,763588402,-74451571,691940942,-855574347,-379160322,-1983275907,170150172,652637490,1371210442,-599077212,570324662,-1908987821,-1758729792,-2054016057,-1128766732,-1739352096,-1874752970,1533420303,-902341836,957933238,-1121040897,-1791506832,-1348264171,1417604891,2011963109,-933863045,1330755824],"sigBytes":2272}
Could you please try this?
import * as CryptoJS from 'crypto-js';
export const decrypt = (secret: string, encryptedString: string): string => {
const keyWordArray = CryptoJS.enc.Utf8.parse(secret);
const ivWordArray = CryptoJS.enc.Utf8.parse(''.padStart(16, '\0'));
const cipherTextBase64 = encryptedString;
const decryptedWordArray = CryptoJS.AES.decrypt(cipherTextBase64, keyWordArray, {
mode: CryptoJS.mode.CTR,
iv: ivWordArray,
padding: CryptoJS.pad.NoPadding,
});
const decryptedString = decryptedWordArray.toString(CryptoJS.enc.Utf8);
return removeEscapeSequences(decryptedString);
};
export const removeEscapeSequences = (text: string): string => {
return text.replace(/[^ -~]+/g, '');
}
Hope it helps.
For me it was a problem with length of the key. As far as I know for 128bit u need 16 symbols key.
I got same error and after some debugging I realized , when we store encrypted data in the local storage it add
""
at the starting and ending of the encrypted string.""encrypted_string""
. when we try to decrypt our encrypted string that stored in localstorage , it try to decrypt it according to"enrypted_string"
but it receives""encrypted_string""
. therefore we receive this error from local storage. In otherword, it is because of mismatching our encrypted string. what i did is replaced extra""
from string and then decrypted it. it worked for melet data = // get_data_from_localstroage let newExactString = data.replaceAll('"', ''); // our remaining decryption process
Thanks junaid, was stuck for 3 hrs!
I faced this issue while decrypting the file content.
If you are reading the content from the file, it should be converted to string as well.
const data = readFileSync(filePath, { encoding: "utf8" });
Initially the error was due to,
CryptoJS.AES.decrypt(data, secretKey).toString(CryptoJS.enc.Utf8);
Later, I changed this to fix the error as below,
CryptoJS.AES.decrypt(data.toString(), secretKey).toString(CryptoJS.enc.Utf8);
I faced the same error 'Melformed UTF-8 data'. And I found another solution to create an AES128-EBC hash .You guys can try my solution using 'crypto' package:
//Encode to base64
const createAES128Hash = (data: string, key: string) => {
const cipher = createCipheriv('aes-128-ecb', key, null); // createCipheriv imported from 'crypto'
return Buffer.concat([cipher.update(data), cipher.final()]).toString('base64'); //return base64 string
};
//Decode from base64
const decryptAES128Hash = (base64String: string, key: string) => {
const cipher = createDecipheriv('aes-128-ecb', key, null); // createDecipheriv imported from 'crypto'
const originalData = Buffer.concat([cipher.update(Buffer.from(base64String, 'base64')), cipher.final()]).toString('utf8');
return originalData;
};
While decrypting the URL, I encountered the same error: 'Malformed UTF-8 data.' If anyone knows a solution, kindly let us know."
decryptAES = (payload, key) => {
const encryptedData = CryptoJS.enc.Base64.parse(payload);
const iv = CryptoJS.lib.WordArray.create(encryptedData.words.slice(0, 4));
const ciphertext = CryptoJS.lib.WordArray.create(encryptedData.words.slice(4));
const decrypted = CryptoJS.AES.decrypt({ ciphertext: ciphertext }, key, {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: iv
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
I store the data in localstorage when encrypt(data, ducument.querySelector('.pwd').value), then i try to call decrypt(localStorage.data, ducument.querySelector('.pwd').value), it show the error: Error: Malformed UTF-8 data
But when I try this encrypt(data, '123') and decrypt(data, '123'), it is successful. and why?
How to fix it?
cryptojs "version": "4.0.0",
code: