Skip to content

Instantly share code, notes, and snippets.

@fushang318
Created November 26, 2010 10:04
Show Gist options
  • Save fushang318/716500 to your computer and use it in GitHub Desktop.
Save fushang318/716500 to your computer and use it in GitHub Desktop.
#使用 AES 加密的示例如下:
#Ruby代码
require "openssl"
cipher = OpenSSL::Cipher::Cipher
# AES, block size = 256,使用 CBC
aes = cipher.new('aes-256-cbc').encrypt
# 随机生成密钥
key = aes.random_key
# 随机生成初始向量
iv = aes.random_iv
crypted = aes.update 'hello '
crypted.<< aes.update 'world'
crypted.<< aes.final
aes = cipher.new('aes-256-cbc').decrypt
aes.key = key
aes.iv = iv
result = aes.update crypted
result.<< aes.final
puts result #=> "hello world"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment