实现代码:
class des
require 'openssl'
require 'base64'
alg = 'des-ede3-cbc'
key = "mz4wjs6l"
des_key = "nz4wjs6l"
#加密
def encode(str)
des = openssl::cipher::cipher.new(alg)
des.pkcs5_keyivgen(key, des_key)
des.encrypt
cipher = des.update(str)
cipher << des.final
return base64.encode64(cipher) #base64编码,才能保存到数据库
end
#解密
def decode(str)
str = base64.decode64(str)
des = openssl::cipher::cipher.new(alg)
des.pkcs5_keyivgen(key, des_key)
des.decrypt
des.update(str) + des.final
end
end
测试代码:
require 'pp'
des = des.new
str = '1234567890123456'
pp '=====encrypt====='
str = des.encode(str)
pp str
pp '=====decrypt====='
pp des.decode(str)
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
循序渐进掌握递归正则表达式
一般来说,递归的正则表达式用来匹配任意嵌套层次的结构或左右对称的结构。例如匹配: 递归正则在正则表达式里算是比较灵活的部分,换句话说就是可能会比较难...
[阅读全文]
-
scrapy 使用
启动方式: 写一个启动文件,与配置文件同级 from scrapy.cmdline import execute import sys,os sys...
[阅读全文]
-
-
-
经典例题
例题: 1.用户输入账号 2.用户输入密码 3.判断用户的账号是不是alex 4.如果账号是alex在继续判断密码是不是alexdsb 5.账号和密...
[阅读全文]
-
-
-
-
-
网友评论