csquared / fernet.js

Javascript implementation of Fernet symmetric encryption https://github.com/kr/fernet-spec
MIT License
73 stars 29 forks source link

Decrypting issue #12

Open kodeine opened 8 years ago

kodeine commented 8 years ago

Hello, I am using Fernet for Python but im trying to decrypt it from angular2 app. The decryption is not working and it gives me error

handleError TypeError: Cannot read property 'sigBytes' of undefined
    at Object.init (http://localhost:8100/build/js/app.bundle.js:68362:21)
    at http://localhost:8100/build/js/app.bundle.js:67958:25
    at Function.createHmac (http://localhost:8100/build/js/app.bundle.js:74924:12)
    at Object.decodeToken [as decode] (http://localhost:8100/build/js/app.bundle.js:75037:32)
    at FeedRequestProvider.ApiHelper.decrypt (http://localhost:8100/build/js/app.bundle.js:3925:28)
    at MapSubscriber.project (http://localhost:8100/build/js/app.bundle.js:4083:27)
    at MapSubscriber._next (http://localhost:8100/build/js/app.bundle.js:143244:35)
    at MapSubscriber.Subscriber.next (http://localhost:8100/build/js/app.bundle.js:136995:18)
    at XMLHttpRequest.onLoad (http://localhost:8100/build/js/app.bundle.js:46134:38)
    at ZoneDelegate.invokeTask (http://localhost:8100/build/js/zone.js:356:38)

This is the code im using to decrypt,

decrypt(response):string {
        console.log('DECRYPTING...');
        console.log(response.text());
        var token = new this.fernet.Token({
            secret: this.key,
            token: response.text()
        })

        let buffer = token.decode();
        console.log('fernet', buffer);

Can you please let me know where i am doing this wrong?

Thanks

csquared commented 8 years ago

can I see the value of: response.text() and this.key that you have?

donspaulding commented 7 years ago

I'm getting bit by the same error. Here's a reproducible test case:

On python:

pip install cryptography

Then:

>>> from cryptography.fernet import Fernet
>>> k = Fernet.generate_key()
>>> print(k)
b'LvxMTvhAEXNB2o66GnAOPO6J63lL9KvB-WNDeyEoNGA='
>>> print(Fernet(k).encrypt(b'Will not break.  Will not break.  Oops, it broke!'))
b'gAAAAABZoH7ygX3JB6eZgocWIERBUPtNO0B2LJaRNXKtWEledr0LkvHpY2nZjPBDliJj__zDNrX2Oxhr67nXE-dsmRfAvrUtS4QKkHAmbUub8DMPQ6p2P7XDN5PE-pz9--eh1zxYsH7UFKJun1F4XPiiNnZChvJEYw=='

And then in nodejs:

> fernet = require('fernet')
{ [Function: fernet]
  Hex: { stringify: [Function: stringify], parse: [Function: parse] },
  Base64: 
   { stringify: [Function: stringify],
     parse: [Function: parse],
     _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' },
  parseHex: [Function: parseHex],
  decode64toHex: [Function: decode64],
  hexBits: [Function: hexBits],
  urlsafe: [Function: urlsafe],
  setSecret: [Function: setSecret],
  ArrayToHex: [Function: ArrayToHex],
  setIV: [Function: setIV],
  encryptMessage: [Function],
  decryptMessage: [Function],
  timeBytes: [Function: timeBytes],
  createToken: [Function],
  createHmac: [Function: createHmac],
  Secret: [Function: Secret],
  Token: [Function: Token],
  ttl: 60,
  versionHex: '80',
  ivHex: 'd13e3bf708a8bf34f88e6a133064f82a',
  iv: 
   { words: [ -784450569, 145276724, -124884461, 811923498 ],
     sigBytes: 16 },
  secret: undefined,
  message: undefined,
  cipherText: undefined,
  token: undefined,
  version: 128,
  optsIV: undefined,
  maxClockSkew: 60 }
> new fernet.Token({secret: 'LvxMTvhAEXNB2o66GnAOPO6J63lL9KvB-WNDeyEoNGA=', token: 'gAAAAABZoH7ygX3JB6eZgocWIERBUPtNO0B2LJaRNXKtWEledr0LkvHpY2nZjPBDliJj__zDNrX2Oxhr67nXE-dsmRfAvrUtS4QKkHAmbUub8DMPQ6p2P7XDN5PE-pz9--eh1zxYsH7UFKJun1F4XPiiNnZChvJEYw==', ttl: 60000}).decode()
TypeError: Cannot read property 'sigBytes' of undefined
    at init (/home/don/dev/tester/node_modules/crypto-js/hmac.js:53:21)
    at /home/don/dev/tester/node_modules/crypto-js/core.js:744:25
    at Function.createHmac (/home/don/dev/tester/node_modules/fernet/fernet.js:124:12)
    at Token.decodeToken [as decode] (/home/don/dev/tester/node_modules/fernet/lib/token.js:77:32)
    at repl:1:263
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:433:10)
> 
donspaulding commented 7 years ago

Seems related:

https://github.com/brix/crypto-js/issues/85

donspaulding commented 7 years ago

Nevermind, I see the error in my code is that I thought I could pass the secret along as a string alongside the token when doing new Token({secret: <secret_obj_NOT_STRING>, token: <string_to_be_decrypted>, ttl: <int>}).decode()

Sorry for the noise, and I assume the original poster had a similar problem with this.key! ;-)

csquared commented 7 years ago

No problem! Glad you got it sorted :)

On Aug 25, 2017, at 1:14 PM, Don Spaulding notifications@github.com wrote:

Nevermind, I see the error in my code is that I thought I could pass the secret along as a string alongside the token when doing new Token({secret: , token: , ttl: }).decode()

Sorry for the noise, and I assume the original poster had a similar problem with this.key! ;-)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

morkyy commented 6 years ago

@donspaulding Hey, I'm doing the exact same thing as you. Encrypting with fernet using the cryptography library on the server and decrypting on the client. Can you explain how you ended up solving this issue? I have the secret as string too but I'm not sure on how to turn it into a usable object.

edit: never mind I solved it.

wings-sakhi commented 5 years ago

@donspaulding Hey, I'm doing the exact same thing as you. Encrypting with fernet using the cryptography library on the server and decrypting on the client. Can you explain how you ended up solving this issue? I have the secret as string too but I'm not sure on how to turn it into a usable object.

edit: never mind I solved it.

Hi, I'm facing the same issue, can you please explain how you solved the issue?

ghost commented 5 years ago

@donspaulding Hey, I'm doing the exact same thing as you. Encrypting with fernet using the cryptography library on the server and decrypting on the client. Can you explain how you ended up solving this issue? I have the secret as string too but I'm not sure on how to turn it into a usable object. edit: never mind I solved it.

Hi, I'm facing the same issue, can you please explain how you solved the issue?

One needs to create a Secret object and then use that when creating the token:

var secret = new fernet.Secret('cw_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4=');

...and then one can use their secret with a new Token:

var token = new fernet.Token({
    secret: secret,
    token: 'gAAAAABSO_yhAAECAwQFBgcICQoLDA0OD1PGoFV6wgWZG6AOBfQqevwJT2qKtCZ0EjKy1_TvyxTseR_3ebIF6Ph-xa2QT_tEvg==',
    ttl: 0
})
console.log(token.decode());