Andresch Serj
Andresch Serj

Reputation: 37418

Problems with SJCL "Cannot read property 'replace' of undefined"

Using Sjcl i try to write a little service sending and receiving encrypted data from my javascript application.

Sadly, the sjcl documentation is lacking information on how to process their AES encrypted data. Their encrypt method returns an object containing serveral attributes of which i can only guess what is what.

{"iv":"i0t5BttfXwtY6hxuFSZxJg==",
"v":1,
"iter":1000,
"ks":128,
"ts":64,
"mode":"ccm",
"cipher":"aes",
"salt":"MZ8hpbz+5hU=",
"ct":"n5mR5jwawYwsaUV0xbcYXrcCXPWjR5qMG23qU5Spguz4jpjG5QdFMWSf"}

I can identify iter, ks, ts, mode, cipher and salt. My guess is that ct is the cipher text, representing the encrypted data. But what is v and iv?

I tried decrypting my cipher text giving ct as a parameter and even tried giving the whole result as a parameter but it always just produces errors:

var result = sjcl.json.encrypt(
  'pw123', 
  '{text: "this should be decrypted"}', 
  parameters, 
  rp
);

var originalText = sjcl.json.decrypt(
  'pw123', 
  result.ct,
  parameters,
  rp);

// Results in:
// Uncaught TypeError: Cannot read property 'replace' of undefined

How do i decrypt my ct using their decrypt method? Anyone any experience with this and can give a brief example?

Upvotes: 2

Views: 1166

Answers (2)

Andresch Serj
Andresch Serj

Reputation: 37418

The Problem in my Example was that i tried to acces result.ct but result is a stringified JSON so accessing it's attribute ct obviously returned undefined.

In my other attempt i overwrote parameters by passing parameters. Decrypting without passing additional parameters worked - the initial parameters where already given in the result stringified JSON.

The updated fiddle is here: http://jsfiddle.net/e5rqogm3/1/

Upvotes: 1

Artjom B.
Artjom B.

Reputation: 61952

The encryption/decryption works with the default parameters:

var result = sjcl.json.encrypt(
  'pw123', 
  '{text: "this should be decrypted"}'
);

var originalText = sjcl.json.decrypt(
  'pw123', 
  result);

But what is v and iv?

v is probably the version information for the ciphertext so that later versions of SJCL can decrypt it. It is simply set to 1 and never used in the code.

The iv is the initialization vector that is needed for most block cipher modes such as CCM. For AES it is 16 bytes long or simply the block size.

Upvotes: 1

Related Questions