Skip to content

Instantly share code, notes, and snippets.

@Jwink3101
Last active April 9, 2018 19:25
Show Gist options
  • Select an option

  • Save Jwink3101/f3b2bf461237c9241342b5dd817d6e67 to your computer and use it in GitHub Desktop.

Select an option

Save Jwink3101/f3b2bf461237c9241342b5dd817d6e67 to your computer and use it in GitHub Desktop.
An example I am working on to use openssl via subprocess from Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Simple example of calling openSSL from python. Also with base64 set since I
will want to store in a text file
"""
from __future__ import print_function, unicode_literals
import subprocess
import shlex
textin = u'can you see me? What about Ûńįç°dė'
def encrypt(data,password):
cmd = u'openssl des3 -salt -base64 -pass pass:' + password
# Encode it to bytes
# cmd = cmd.encode('utf8')
proc = subprocess.Popen(shlex.split(cmd),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out,err = proc.communicate(data)
return out
def decrypt(encrypted,password):
cmd = u'openssl des3 -d -salt -base64 -pass pass:' + password
# Encode it to bytes
# cmd = cmd.encode('utf8')
proc = subprocess.Popen(shlex.split(cmd),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out,err = proc.communicate(encrypted)
return out
udata = textin.encode('utf8')
password='1234'
edata = encrypt(udata,password)
# Just for example if storing/writing to a text file
etext = edata.decode('ascii')
edata = etext.encode('ascii')
ddata = decrypt(edata,password)
textout = ddata.decode('utf8')
print('textin:',textin)
print('udata:',udata)
print('edata:',edata)
print('ddata:',ddata)
print('textout:',textout)
print('textin == textout:',textin == textout)
@Jwink3101
Copy link
Copy Markdown
Author

Updated this to send the data via communicate()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment