2

Iam trying to encrypt file names using the cryptography library but the output is so long that windows doesn't rename it and gives me error. This was asked here but didnt have a solution.

fileName= f.encrypt(b'#Filenamehere')

print(fileName)

Output

b'gAAAAABhpf2t5brpCcfFoMZ9NvQ9R21uCeGGpVGZE2L_xfWwSw4IwSEj0Eos5smAmfZz3nySqGXsQOCjbThrTAFZuBOD3r1-t-aaV6RVpHWadybdE5A5RKVudM8BoD4_wvksaw4W9swNqgDZuGw=='

Is there a way to limit the output encrypted message to a certain number in cryptography library.

manis
  • 53
  • 4
  • Why do you want to change your files names this way? – Daweo Nov 30 '21 at 10:41
  • iam trying to encrypt my files but i also wanna change the filename so that nobody knows what it is. Windows has a limit on filename characters. Is there any other way to do this. – manis Nov 30 '21 at 10:46
  • You are evidently using Fernet. Fernet is a good choice for many problems, but not all. You can see the format of the resulting "token" [here](https://github.com/fernet/spec/blob/master/Spec.md). The token is *always* larger than the input, therefore you cannot take a filename that is already maximum size and replace it with an encrypted filename. You can reduce the extra size somewhat by 1) removing the timestamp and version bytes from the token, and 2) re-encoding the output using a larger alphabet than base64, say base96 or whatever alphabet is allowed for Windows filenames. – President James K. Polk Nov 30 '21 at 18:47

1 Answers1

1

change the filename so that nobody knows what it is. Windows has a limit on filename characters. Is there any other way to do this.

I suggest taking look at Vernam cipher, cryptomuseum.com article explain how it is working and describe history of its' usage. Vernam is easy to implement, gives ciphertext same length as plaintext, but requires key length no lesser than plaintext.

Beware that as it does deals with bits, you might get byte outside 0-127 range (I am not sure how windows filesystem will react to that) or byte with character not allowed in filename

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Your solution to use ciper is great, i didn't think of that lol. But i think this might create collision problems while encrypting and decrypting. – manis Dec 02 '21 at 06:20