Skip to content

Instantly share code, notes, and snippets.

@Gaolz
Forked from axtutuu/aec_cipher.rb
Created September 22, 2017 07:15
Show Gist options
  • Save Gaolz/0ce53db3cc1f1ad15369fd3d05b585a6 to your computer and use it in GitHub Desktop.
Save Gaolz/0ce53db3cc1f1ad15369fd3d05b585a6 to your computer and use it in GitHub Desktop.
Ruby DES-ECB & PKCS5Padding で暗号化
# 暗号モード: ECB
# パディング方式: PKCS5Padding
# OpenSSL::Cipher.ciphers で利用可能な暗号方式名を取得可能
# https://docs.ruby-lang.org/ja/latest/class/OpenSSL=3a=3aCipher.html
module AesCipher extend self
KEY = "Your key"
CIPHER = "DES-ECB"
def encrypt(text)
cipher = OpenSSL::Cipher.new(CIPHER)
cipher.encrypt
cipher.key = KEY
enc = cipher.update(text)
enc << cipher.final
Base64.encode64(enc)
end
def decrypt(text)
enc = Base64.decode64(text)
cipher = OpenSSL::Cipher.new("DES-ECB")
cipher.decrypt
cipher.key = KEY
dec = cipher.update(enc)
dec << cipher.final
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment