@@ -254,18 +254,32 @@ By following this approach, the password remains securely stored in the database
254
254
255
255
256
256
``` php
257
+ # You sould install pycryptodome before runing this code
258
+ # pip install pycryptodome
257
259
import base64
258
260
from Crypto.Cipher import DES
261
+ from Crypto.Util.Padding import pad, unpad
259
262
260
263
def encrypt_data(data, key):
261
- cipher = DES.new(key, DES.MODE_ECB)
262
- encrypted_data = cipher.encrypt(data)
264
+ cipher = DES.new(key.encode(), DES.MODE_ECB)
265
+ padded_data = pad(data.encode(), DES.block_size)
266
+ encrypted_data = cipher.encrypt(padded_data)
263
267
return base64.b64encode(encrypted_data).decode('utf-8')
264
268
265
269
def decrypt_data(encrypted_data, key):
266
- cipher = DES.new(key, DES.MODE_ECB)
267
- decrypted_data = cipher.decrypt(base64.b64decode(encrypted_data))
268
- return decrypted_data.decode('utf-8')
270
+ cipher = DES.new(key.encode(), DES.MODE_ECB)
271
+ decrypted_data = cipher.decrypt(base64.b64decode(encrypted_data.encode()))
272
+ return unpad(decrypted_data, DES.block_size).decode('utf-8')
273
+
274
+ if __name__ == '__main__':
275
+ key = 'abcdefgh' # 8 bytes key for DES
276
+ data = 'Hello, World' # Data to be encrypted
277
+
278
+ encrypted_data = encrypt_data(data, key)
279
+ print('Encrypted data:', encrypted_data)
280
+
281
+ decrypted_data = decrypt_data(encrypted_data, key)
282
+ print('Decrypted data:', decrypted_data)
269
283
```
270
284
271
285
@@ -280,23 +294,40 @@ The noncompliant code uses the DES (Data Encryption Standard) algorithm, which i
280
294
281
295
282
296
``` php
283
- import base64
297
+ # Importing required libraries
298
+ import base64, os
299
+
300
+ # Importing required libraries from cryptography
284
301
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
285
302
from cryptography.hazmat.backends import default_backend
286
303
287
- def encrypt_data(data, key):
288
- backend = default_backend()
289
- cipher = Cipher(algorithms.AES(key), modes.GCM(), backend=backend)
290
- encryptor = cipher.encryptor()
291
- encrypted_data = encryptor.update(data) + encryptor.finalize()
292
- return base64.urlsafe_b64encode(encrypted_data).decode('utf-8')
304
+ # Function to encrypt data using AES-GCM algorithm and return the encrypted data in string format
305
+ def encrypt(data:str, key:str) -> str:
306
+ iv = os.urandom(12)
307
+ encryptor = Cipher(algorithms.AES(key.encode('utf-8')), modes.GCM(iv), backend=default_backend()).encryptor()
308
+ encrypted_data = encryptor.update(data.encode('utf-8')) + encryptor.finalize()
309
+ return base64.urlsafe_b64encode(iv + encryptor.tag + encrypted_data).decode('utf-8')
310
+
311
+ # Function to decrypt data using AES-GCM algorithm and return the decrypted data in string format
312
+ def decrypt(encrypted_data, key) -> str:
313
+ decoded_data = base64.urlsafe_b64decode(encrypted_data)
314
+ iv = decoded_data[:12]
315
+ tag = decoded_data[12:28]
316
+ encrypted_data = decoded_data[28:]
317
+ decryptor = Cipher(algorithms.AES(key.encode('utf-8')), modes.GCM(iv, tag), backend=default_backend()).decryptor()
318
+ return (decryptor.update(encrypted_data) + decryptor.finalize()).decode('utf-8')
319
+
320
+ # Main function to test the above functions
321
+ if __name__ == '__main__':
322
+ key = '689ef728d55342d9af07ed4194cf1d4C' # 32 bytes key for AES-256
323
+ data = 'Hello, World' # Data to be encrypted
293
324
294
- def decrypt_data(encrypted_data, key):
295
- backend = default_backend( )
296
- cipher = Cipher(algorithms.AES(key), modes.GCM(), backend=backend )
297
- decryptor = cipher.decryptor()
298
- decrypted_data = decryptor.update(base64.urlsafe_b64decode( encrypted_data)) + decryptor.finalize( )
299
- return decrypted_data.decode('utf-8' )
325
+ # Encrypting and decrypting the data
326
+ encrypted_data = encrypt(data, key )
327
+ print('Encrypted data:', encrypted_data )
328
+
329
+ decrypted_data = decrypt( encrypted_data, key )
330
+ print('Decrypted data:', decrypted_data )
300
331
```
301
332
302
333
The compliant code uses the cryptography library, which provides a more secure and modern cryptographic API. It employs the AES (Advanced Encryption Standard) algorithm with GCM (Galois/Counter Mode) mode, which is considered more secure than DES. The urlsafe_b64encode and urlsafe_b64decode functions from base64 module are used for encoding and decoding the encrypted data, respectively.
0 commit comments