Created
October 7, 2016 03:08
-
-
Save axtutuu/d0ef87d00a2b6a5623335b43f715bd50 to your computer and use it in GitHub Desktop.
Ruby DES-ECB & PKCS5Padding で暗号化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 暗号モード: 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