Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Connect Demo
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.2 (/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7)" project-jdk-type="Python SDK" />
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<module fileurl="file://$PROJECT_DIR$/.idea/CryptoApp.iml" filepath="$PROJECT_DIR$/.idea/CryptoApp.iml" />
<component name="DependencyValidationManager">
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
__author__ = 'donpinto'
from couchbase import Couchbase
import json
import keyring
from simplecrypt import encrypt,decrypt
#Get bucketpassword from keyring
bktpwd = keyring.get_password('bucketpassword','CBEncrypt')
cb = Couchbase.connect(bucket='customer', host='localhost', password=str(bktpwd))
#Get app encryptionkey from keyring
cryptkey = keyring.get_password('key1','CBEncrypt')
#Loop over the documents and encrypt the ccard
for i in range(0,1000):
result = cb.get('customer'+str(i))
customer = result.value
print customer['ccInfo']['cardNumber']
customer['ccInfo']['cardNumber'] = encrypt(cryptkey,customer['ccInfo']['cardNumber'])
#customer['ccInfo']['cardNumber'] = decrypt(cryptkey,customer['ccInfo']['cardNumber'])
print customer
cb.set('customer'+str(i), customer)
__author__ = 'donpinto'
"dateLastActive": "Wed Mar 05 03:14:59 UTC 2014",
"dateAdded": "Wed Mar 05 03:14:59 UTC 2014",
"firstName": "Matt",
"lastName": "Botsford",
"emailAddress": "",
"postalCode": "12912-4268",
"phoneNumber": "600-649-7428 x055",
"ccInfo": {
"cardType": "discover",
"cardNumber": "1212-1221-1121-1234",
"cardExpiry": "2013-09-12"
"customerId": "customer00",
"firstName" : "Don"
from Crypto.Cipher import AES
from Crypto.Hash import MD5
from base64 import encodestring, decodestring #base64 encoding to enable json dumps
# the block size for the key and message must be 16, 24, or 32 for AES
# the character used for padding--with a block cipher such as AES, the value
# you encrypt must be a multiple of BLOCK_SIZE in length. This character is
# used to ensure that your value is always a multiple of BLOCK_SIZE
def pad(msg, block_size=BLOCK_SIZE, padding=PADDING):
*pad the text to be encrypted*
- appends a padding character to the end of the String
- until the string has block_size length
return msg + ((block_size - len(msg) % block_size) * padding)
def depad(msg, padding=PADDING):
"""depad the decryptet message"""
return msg.rstrip(padding)
def getSecret(key):
"""hases the key to MD5"""
def encrypt(key, msg):
"""encrypts the message"""
secret = getSecret(key)
cipher =
return encodestring(cipher.encrypt(pad(msg)))
def decrypt(key, msg):
"""decrypts the message"""
secret = getSecret(key)
cipher =
return depad((cipher.decrypt(decodestring(msg))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.