Encrypt with Ruby (Rails) and decrypt with Python

Introduction

To run backend tasks in Rails, Sidekiq, Resque, Delayed Job, Action Job are famous.

Since I mostly build Rails applications on AWS, I often let lambda handle backend tasks.

This time, I will write about how to get the encrypted character string on Rails side with lambda (python) and decrypt it.

Case

It is like this.

Untitled presentation (1).png

The environment is rails 5.0.

Rails MessageEncryptor is now available for ActiveSupport to encrypt strings. There is. ** This cannot be used this time. **(I will explain later)

This time, I will encrypt it by myself and decrypt it.

Encryptable.rb



module Encryptable
  SECRET = 'hogehogehogehogehogehogehogehoge'
  IV = 'hogehogehogehoge'
  CIP_NAME = 'aes-256-cbc'

  def encrypt(value)
    b64data = Base64::strict_encode64(value)
    cip = OpenSSL::Cipher.new(CIP_NAME)
    cip.encrypt
    cip.key = SECRET
    cip.iv = IV
    encrypted = cip.update(b64data)
    encrypted << cip.final
    "#{Base64::strict_encode64(encrypted)}"
  end

  def decrypt(value)
    data = Base64::strict_decode64(value)
    cip = OpenSSL::Cipher.new(CIP_NAME)
    cip.decrypt
    cip.key = SECRET
    cip.iv = IV
    decrypted = cip.update(data)
    decrypted << cip.final
    "#{Base64::strict_decode64(decrypted)}".encode('UTF-8','UTF-8')
  end

Lambda side

Decryptable.py


import base64
from Crypto.Cipher import AES

SECRET = 'hogehogehogehogehogehogehogehoge'
IV = 'hogehogehogehoge'

def decrypt(encrypted_value):
  data = base64.b64decode(encrypted_value)
  cip = AES.new(SECRET, AES.MODE_CBC, IV)
  b64_decrypted_data = cip.decrypt(data)
  decrypted_data = base64.b64decode(b64_decrypted_data)
  return decrypted_data

MessageEncryptor cannot be used.

As you can see from the source of MessageEncryptor, Marshal is used for serialization. For this reason, type information etc. is added to the character string encrypted with MessageEncryptor by the marshalling method peculiar to ruby. It cannot be decrypted on the Python side.

Recommended Posts

Encrypt with Ruby (Rails) and decrypt with Python
Scraping with Node, Ruby and Python
Easy web scraping with Python and Ruby
Ruby, Python and map
Python and Ruby split
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
[Ruby vs Python] Benchmark comparison between Rails and Flask
Python and ruby slice memo
Ruby and Python syntax ~ branch ~
python with pyenv and venv
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
Works with Python and R
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Communicate with FX-5204PS with Python and PyUSB
Difference between Ruby and Python split
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Dynamic proxy with python, ruby, PHP
Scraping with Python, Selenium and Chromedriver
HTTPS with Django and Let's Encrypt
Decrypt files encrypted with OpenSSL with Python 3
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Reading and writing NetCDF with Python
I played with PyQt5 and Python3
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python
I compared the speed of Hash with Topaz, Ruby and Python
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Data pipeline construction with Python and Luigi
Calculate and display standard weight with python
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Passwordless authentication with RDS and IAM (Python)
Eating and comparing programming languages: Python and Ruby
Python installation and package management with pip