LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2017, 06:49 PM   #1
darthguinea
LQ Newbie
 
Registered: Jan 2017
Posts: 14

Rep: Reputation: Disabled
Question python & javascript AES module


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.

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

Thank you
 
Old 09-06-2017, 06:43 AM   #2
darthguinea
LQ Newbie
 
Registered: Jan 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
I've tried this, however its not decrypting on the javascript side:

python:
Code:
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():

    cipher = AESCipher('824601be6c2941fabe7fe256d4d5a2b7')
    encrypteddata = cipher.encrypt('Secreat Message to Encrypt')
    print encrypteddata

    decryptdata =cipher.decrypt(encrypteddata)
    print decryptdata

main()

javascript:
Code:
<html>
  <head>
    <script src="js/rollups/aes.js" ></script>
    <script>
        var iv = CryptoJS.enc.Hex.parse('1011121314151617');
        var key = CryptoJS.enc.Hex.parse('824601be6c2941fabe7fe256d4d5a2b7');
        var encrypted = CryptoJS.enc.Hex.parse('MTAxMTEyMTMxNDE1MTYxN3U1X+MdhNxr/UVOSBkUQuuZHhyN0uMjlj+nnFBpGGSC');

        var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv, mode: CryptoJS.mode.CBC}).toString(CryptoJS.enc.Utf8);
        console.log('my message');
        console.log("msg: " + CryptoJS.enc.Hex.stringify(decrypted));
        // Lorem ipsum dolor sit amet, ...
    </script>
  </head>
  <body></body>
</html>
OP:
Code:
main.html:15 my message
main.html:16 msg:
 
  


Reply

Tags
aes, javascript, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python extension module installed in /usr/local/lib[64]/python[2.7|3.x]/site-packages not found Nate_KS Slackware 1 07-11-2017 10:06 PM
Encryption library on Linux provides AES implementation based on cpu aes instruction rainman1985_2010 Programming 3 07-03-2015 05:34 PM
dm-crypt aes-xts-plain64 vs aes-cbc-essiv for volumes > 2TiB Molly Linux - Security 1 09-13-2010 05:24 PM
Python: Trying to store AES encrypted string into database - can I convert to ascii? BrianK Programming 2 01-23-2009 01:58 PM
python update - Unable to load GTK2 Python bindings: No module named gtk itpedersen Linux - Software 2 10-03-2008 03:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration