0

I've got an array of n string elements encrypted with CryptoJS : [krypt1, krypt2, krypt3, ...]

The keydecrypt is the same for each element.

I try to decrypt each element of the array and return an array of string decrypted elements like this [dekrypt1, dekrypt2, dekrypt3, ...]

My code is:

var urltodecrypt = this.url.chunk;
function decrypteach(x) {
    return CryptoJS.AES.decrypt(x.toString(), keydecrypt).toString(CryptoJS.enc.Utf8);
}
var clearfileurl = urltodecrypt.map(decrypteach);

When there is 1 element in the array, everything's fine: it return an array of rightly decrypted string element.

When there is >1 elements, var urltodecrypt give still the right array (verified), but var clearfileurl return an error: Error: Malformed UTF-8 data

What am I missing?

EDIT

Tried on @vector advices a loop over each element function on this model :

var urltodecrypt = this.url.chunk;
var arrayLength = urltodecrypt.length;
for (var i = 0; i < arrayLength; i++) {
     var clearfileurl = CryptoJS.AES.decrypt(urltodecrypt.toString(), keydecrypt).toString(CryptoJS.enc.Utf8);
}
console.log (clearfileurl);

Exact same result = 1 element array :ok / >1 elements array: Error: Malformed UTF-8 data

EDIT #2: question close

I just broke my first code (map) into different vars :

  • x.toString()
  • CryptoJS.AES.decrypt()
  • toString(CryptoJS.enc.Utf8)

I relaunched my server : everything's fine now, from 1 element array to +10 elements array.

Just in case, below my (heavy & superstitious...) tested working code:

var urltodecrypt = this.url.chunk;
console.log (urltodecrypt);
function decrypteach(x) {
    var stringurl = x.toString();
    var bytesfileurl = CryptoJS.AES.decrypt(stringurl, keydecrypt);
    var finaldecrypturl = bytesfileurl.toString(CryptoJS.enc.Utf8);
    return finaldecrypturl;
}
var clearfileurl = urltodecrypt.map(decrypteach);
console.log (clearfileurl);
Community
  • 1
  • 1
Ontokrat
  • 189
  • 1
  • 14
  • 1
    You've provided the code, but haven't shown what's supposed to be wrong with it. Please [edit] your question provide example inputs, outputs and expected outputs. – Artjom B. Apr 27 '17 at 20:41
  • Are you looping over the array elements or feeding the entire array into decrypteach() all at once? – vector Apr 27 '17 at 20:44
  • @vector, that question is answered by the code: `map`. – trincot Apr 27 '17 at 20:44
  • @vector feeding the entire array into decrypteach() all at once – Ontokrat Apr 27 '17 at 20:44
  • 1
    first of, your code does not show where [dekrypt1, dekrypt2, dekrypt3, ...] is initialised. looks like you need to loop over the array and return one decrypted element at a time, that's why it works for you when you have 1 element in the array and not 2 or more. so, call the decrypt function once for each array element or loop over them in the function and return array of decrypted items, no? – vector Apr 27 '17 at 20:48
  • @vector Could you provide a example of loop over each element function? – Ontokrat Apr 27 '17 at 21:04
  • Look at the first answer here: http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript?page=1&tab=votes#tab-top – vector Apr 27 '17 at 21:07
  • @vector I edited my question on your advices: same result with a loop function. – Ontokrat Apr 27 '17 at 21:33
  • create a codepen or a jsfiddle of what you've done. at this point, we need to see exactly what you're doing. – vector Apr 27 '17 at 21:36
  • @vector There is no need to create a codepen or jsfiddle. Stack Overflow has the same functionality. – Artjom B. Apr 27 '17 at 21:38
  • @vector & others : finally 'made' my code work in the `mapping` way - see edit #2. – Ontokrat Apr 28 '17 at 00:10

0 Answers0