0

I am looking for a module that I can use in both python & javascript so that can encrypt something in python then decrypt in javascript if I pass in a key (and vice versa).

So far I've checked out SlowAES and CryptoJS, but cant find any good documentation or examples. Is anyone able to help?

Below is my attempt to get this working:

JS:

var encoded_message = 'MTAxMTEyMTMxNDE1MTYxN2asfw3LtCtoL+mvWtJsIVSVCsvZdBIvZWWRuKEI85nd';
var my_iv = CryptoJS.enc.Base64.parse('1011121314151617');
var my_key = CryptoJS.enc.Base64.parse('824601be6c2941fabe7fe256d4d5a2b7');

console.log('my iv [' + my_iv + ']');
console.log('my key [' + my_key + ']');
console.log('my enc message [' + encoded_message + ']');

var data = CryptoJS.AES.decrypt(encoded_message, my_key, { iv: my_iv, mode: CryptoJS.mode.CBC });
console.log(data);
var dec = CryptoJS.enc.Hex.stringify(data);
console.log('data [' + dec + ']');


var encoded_message = CryptoJS.enc.Base64.parse('MTAxMTEyMTMxNDE1MTYxN2asfw3LtCtoL+mvWtJsIVSVCsvZdBIvZWWRuKEI85nd');
console.log('\n\n\n');
console.log('message [' + encoded_message + ']');

Python:

import os, random, struct
from Crypto.Cipher import AES
from Crypto import Random
import base64
class AESCipher:
    def __init__(self, key):
        BS = 16
        self.pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
        self.unpad = lambda s : s[0:-ord(s[-1])]
        self.key = self.pad(key[0:16])

    def encrypt(self, raw):
        raw = self.pad(raw)
        iv = "1011121314151617"
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.b64encode(iv + cipher.encrypt(raw))

    def decrypt(self, enc):
        enc = enc.replace(' ', '+')
        enc = base64.b64decode(enc)
        iv = enc[:16]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return self.unpad(cipher.decrypt(enc[16:]))


def main():     

    message = 'this is my new message'
    print message[:16]

    cipher = AESCipher('824601be6c2941fabe7fe256d4d5a2b7')
    encrypteddata = cipher.encrypt('work you bloody thing!')
    print encrypteddata         

    decryptdata =cipher.decrypt(encrypteddata)
    print decryptdata 

main()
2

1 Answer 1

2

I've recently been using sjcl in Javascript,.. http://bitwiseshiftleft.github.io/sjcl/

They also appears to be a python compatible version. https://pypi.python.org/pypi/sjcl

At it's simplest you can just do ->

sjcl.encrypt("password", "data")

and

sjcl.decrypt("password", "encrypted-data")

But you still can do low level stuff with it too.

Sign up to request clarification or add additional context in comments.

10 Comments

@LukePark In what way?
Well, software recommendations are off-topic, and your answer doesn't solve the problem that the OP is having with their code.
@LukePark I am looking for a module that I can use in both python & javascript so that can encrypt something in python then decrypt in javascript if I pass in a key (and vice versa). You see the bit about looking for a module..?
Yep, sure do. That is the off-topic part. I'd assume someone with ~3k rep would know that already.
@LukePark I never asked the question, keep up.. And This doesn't solve the OPs problem its obviously totally incorrect. Don't try and re-direct your error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.