django-cryptography icon indicating copy to clipboard operation
django-cryptography copied to clipboard

django.core.signing.BadSignature: Signature "b'k2eKksHUgI7NOp8dMFc6VxSfTlKs4UftAIhFXgV9DOw=\n'" does not match

Open marquicus opened this issue 4 years ago • 7 comments

Hello maybe I'm doing something wrong but I'm getting the following error:

django.core.signing.BadSignature: Signature "b'ISgLribFeoptkfKApq1Fh083Ze/kwxvWv/NXnvs/wf8=\n'" does not match

I followed the example:

class Employee(models.Model):
    nombres = encrypt(models.CharField(max_length=40))
...

I've tried changing the CRYPTOGRAPHY_KEY and SIGNING_BACKEND without success

marquicus avatar Jan 20 '20 20:01 marquicus

Are you attempting to encrypt an existing field?

https://django-cryptography.readthedocs.io/en/latest/migrating.html

georgemarshall avatar Jan 29 '20 07:01 georgemarshall

I think I had the same error. I would get it during the 3rd migration mentioned in https://django-cryptography.readthedocs.io/en/latest/migrating.html, and even when adding a new field. In my case, the problem came up because I wasn't allowing the new encrypted field to be null in the DB. Ie, the field didn't specify null=True,default=None. That meant that when I looked in the DB, the new column's values were all '', which, I agree with the code, doesn't look like a very encrypted value to me... Once I added null=True,default=None to the new encrypted field, the error went away and the migrations worked fine, and everything was great.

mnelson4 avatar Apr 22 '20 04:04 mnelson4

Hi,

I faced same issue, and resolved it by changing SECRET_KEY to be urlsafe by generating new one using python -c "import secrets; print(secrets.token_urlsafe())" So my question is: This is by design that used key should be urlsafe? Or this is still kind of workaround, because I have some other setting which causing BadSignature issue? I'm asking since I didn't found this kind of limitation anywhere. However here: https://docs.djangoproject.com/en/3.2/topics/signing/#django.core.signing.Signer we can find information that sed can't be url safe, which suggest that key should be url safe?

Regards Marcin

dahlinPL avatar Apr 23 '21 16:04 dahlinPL

I think I had the same error. I would get it during the 3rd migration mentioned in https://django-cryptography.readthedocs.io/en/latest/migrating.html, and even when adding a new field. In my case, the problem came up because I wasn't allowing the new encrypted field to be null in the DB. Ie, the field didn't specify null=True,default=None. That meant that when I looked in the DB, the new column's values were all '', which, I agree with the code, doesn't look like a very encrypted value to me... Once I added null=True,default=None to the new encrypted field, the error went away and the migrations worked fine, and everything was great.

Great solution, fixed my issue

siddiknavadhiti avatar Feb 25 '22 15:02 siddiknavadhiti

If anyone still facing this issue you could simply do this:

  • Delete the encrypted field.
  • Do a migration.
  • Add the encrypted field again.
  • Do a migration. Worked for me

parad0x96 avatar Jul 14 '22 13:07 parad0x96

I got this error on one of my servers so I moved to this - https://django-q2.readthedocs.io/en/master/index.html

Amanb1145 avatar Mar 06 '23 19:03 Amanb1145

If someone faces this error, we have a django secret key for development and one for production. This is what messed things up for us. Still looking for a way to solve this elegantly.

ThierryDeruyttere avatar Mar 05 '24 17:03 ThierryDeruyttere