Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrypting issue #12

Open
kodeine opened this issue Aug 30, 2016 · 8 comments
Open

Decrypting issue #12

kodeine opened this issue Aug 30, 2016 · 8 comments

Comments

@kodeine
Copy link

kodeine commented Aug 30, 2016

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
Copy link
Owner

csquared commented Sep 9, 2016

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

@donspaulding
Copy link

donspaulding commented Aug 25, 2017

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
Copy link

Seems related:

brix/crypto-js#85

@donspaulding
Copy link

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
Copy link
Owner

csquared commented Aug 25, 2017 via email

@morkyy
Copy link

morkyy commented Jun 22, 2018

@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
Copy link

@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
Copy link

ghost commented Dec 26, 2018

@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());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants