Created
November 26, 2010 10:04
-
-
Save fushang318/716500 to your computer and use it in GitHub Desktop.
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
#使用 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