-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_crypt.py
54 lines (45 loc) · 1.46 KB
/
card_crypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
"""
This is test file
File : authorizenet.py
Description: API tool
Created On: 3-Feb-2016
Created By: [email protected]
"""
# keystone auth configuration
import base64
from Crypto import Random
from Crypto.Cipher import AES
from datetime import date
# AESCipher helper class for encryption/decryption
# Rationale: put this class into separate cipher.py file and place it into openstack_dashboard/utils for future importing and usage
class AESCipher:
_BS = 16
_pae = lambda self, s: s + (self._BS - len(s) % self._BS) * chr(self._BS - len(s) % self._BS)
_unpad = lambda self, s : s[0:-ord(s[-1])]
def __init__( self, key ):
self.key = key
def encrypt( self, raw ):
"""
Method: encrypt
Desc: To encrypt the data
params: enc=> is the value
Return:Encrypted value
"""
#Encrypting the values
raw = self._pad(raw)
iv = Random.new().read( AES.block_size )
cipher = AES.new( self.key, AES.MODE_CBC, iv )
return base64.b64encode( iv + cipher.encrypt( raw ) )
def decrypt( self, enc ):
"""
Method: decrypt
Desc: To decrypt the data
params: enc=> is the value
Return: Decrypted value
"""
#Decrypting the values
enc = base64.b64decode(enc)
iv = enc[:16]
cipher = AES.new(self.key, AES.MODE_CBC, iv )
return self._unpad(cipher.decrypt( enc[16:] ))