0

I have a string let's say choice="text to encrypt and decrypt" I am using a public key I produced with another program to encrypt and it works fine. I get the cipher text and try to decrypt it, but I am getting the following error.

script.js:282 the pub key is : 021e645806ee84055b51------675e75f93d41c407b7826cf1e76c10aaa05dfc8e
script.js:291 the cipher text is: gkEJlPREx/EVPUue3vWlDWImilRrnyUmeNpkvSbY1vVLqf6IYoBqk9H/PBwXXck++Q==?T+kht1n981qDPmJc7H3DrFxqRi2oe6OFu60ASI389Rg=
script.js:303 the priv key is : 2293f6d33--------f37cc02674edec14eae9586c4fab5bd2a89631f10237b13
cryptico.js:3499 Uncaught TypeError: ciphertext.split is not a function
    at Object.my.decrypt (cryptico.js:3499)
    at Object.success (script.js:306)
    at u (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at k (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)

and when I look for the error it stops at the second line of the decrypt function

my.decrypt = function(ciphertext, key)
    {
        var cipherblock = ciphertext.split("?");
        var aeskey = key.decrypt(my.b64to16(cipherblock[0]));}

I understand that the cipher code splits on the "?" but I can see that I have it in my cipher code. What can be wrong? p.s. I added the dashes to the keys replacing the original values

@UPDATE

I used the example from this page https://github.com/wwwtyro/cryptico and I was getting the same error. So I changed the code as follows

let EncryptionResult = cryptico.encrypt(choice, pub_key);
let DecryptionResult = cryptico.decrypt(EncryptionResult.cipher, priv_key);

Now it passes the original error but I am getting a second error

cryptico.js:3500 Uncaught TypeError: key.decrypt is not a function
    at Object.my.decrypt (cryptico.js:3500)
    at Object.success (script.js:303)
    at u (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at k (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)
    enter code here

and it stops at the second line var aeskey = key.decrypt(my.b64to16(cipherblock[0])); . Does this have to do anything with base64?? My keys are hex encoded, I don't know if this helps.

Cheetara
  • 165
  • 1
  • 5
  • 14
  • 1
    your error suggests that your `ciphertext` variable is not a string, what does show `console.log(typeof ciphertext);`? – Kaddath Nov 15 '18 at 13:50
  • It shows string. But you where right, I was using the variable without the ".cipher" extension. This passed the original problem but no I am getting Uncaught TypeError: key.decrypt is not a function – Cheetara Nov 15 '18 at 14:03
  • The new error suggests that your second parameter given to `cryptico.decrypt`, `priv_key` shouldn't be a string but an object that should have a `decrypt` function. Don't have time to check the examples, but i guess this parameter should be constructed with the library? – Kaddath Nov 15 '18 at 16:11
  • Yes, the example is constructed with the cryptico library. But I have to use a pair of keys created with another library... There is no such example or documentation and I don't know what kind of format should I convert my keys to in order to encrypt -decrypt correctly. Maybe the library does not support keys created with another library, I don't know... – Cheetara Nov 16 '18 at 08:32
  • 1
    There's a very strong chance that it doesn't support a key from another library, as it directly runs functions on it without testing their existence. But it's quite normal if the library expects an object instead of something simple like a string (and usual in cryptography). You can try to build a cryptico key object from your existing key object properties if you are forced to use another lib for the keys. If you run into troubles, that's probably subject for a new question with a more precise title instead of editing this one.. (you can still link the new question here for me if the case) – Kaddath Nov 16 '18 at 09:24
  • Thank you it works this way. The problem is now the user has two pairs of keys... Is there any library you know that can use foreign keys to encrypt and decrypt data? – Cheetara Nov 16 '18 at 09:51
  • usually cryptography libraries, when they use objects, don't support other libraries ones, unless they correspond to a standard (libraries that use other libraries in common for example). You can try to paste your original key object in console and explore its properties to see if you can find the original string you used to build the key inside. If you find it, you can build a cryptico key with that string and use it to decrypt.. – Kaddath Nov 16 '18 at 10:43
  • 1
    Very helpful information and answers :-) I consider the question as completely answered! You saved my day. – Cheetara Nov 16 '18 at 11:03
  • You're welcome, always glad to help! – Kaddath Nov 16 '18 at 12:55

0 Answers0