#!/usr/bin/ruby require 'openssl' class AesEcb256 def encrypt(key, data, padding) aes = OpenSSL::Cipher.new("AES-256-ECB") aes.encrypt aes.key = key aes.padding = padding length = 16 count = data.length if count < length add = length - count data = data + ("\0" * add) else if count > length add = length - (count % length) data = data + ("\0" * add) end end puts "data = #{data}" txt = aes.update(data) + aes.final puts "txt=:#{txt}" txt.unpack('H*')[0] end def decrypt(key, data, padding) aes = OpenSSL::Cipher.new("AES-256-ECB") aes.decrypt aes.key = key aes.padding = padding txt = aes.update([data].pack('H*')) << aes.final txt.gsub("\0", "") end end # 使用方法 key = "7b53919f0c8481dcc22125d2783acd6f" aes = AesEcb256.new m = aes.encrypt(key, "11.25", 0) p m message = aes.decrypt(key, m, 0) p message
2 条评论
这是一个公共类吧。
感谢分享