How to do FPE (format preserving encryption) on a text like ‘A123456’ , if the encryption preserved the format I should get a string with a char than 6 digits , for example ‘Z655432’ , I used the python library FF3 , so I split the string ‘A123456’ to ‘A’ and ‘123456’ and I apply the FF3cipher to each string , well here I find the minimum characters problem so I have to do some padding on ‘A’, the problem is when u do padding u lost the format, u wont get the format that u want.
I tried padding but the format is not preserved, I tried the pyffx library that don’t have the minimum character requirement but apparently its not secure , it does not have the NIST requirements
from ff3 import FF3Cipher
key = "2DE79D232DF5585D68CE47882AE256D8"
tweak = "CB92D080979564"
alphabet = "AZERTYUIOPQSDFGHJKLMWXCVBN"
cipher = FF3Cipher.withCustomAlphabet(key, tweak, alphabet)
def pad_to_min_length_alp(text, min_len):
pad_char = 'A'
if len(text) < min_len:
return text + pad_char * (min_len - len(text))
return text
plaintext = 'A'
padded_plaintext = pad_to_min_length_alp(plaintext, cipher.minLen)
ciphertext = cipher.encrypt(padded_plaintext)
print(f"Encrypted value: {ciphertext}")
result : Encrypted value: SPVVY
so A has been encrypted to a 5 characters string
the format is not preserved
11