ruby下使用rsa非对称加密

作者: daos | 来源:发表于2018-04-18 16:45 被阅读14次

license 是通过私有加密的文件,下面程序是在程序中使用公钥解密,然后进行相应验证操作

    require 'openssl'
    require "base64"
    require "json"
    keypath = "/usr/auth/pub_key.pem"
    aclpath = "/usr/auth/license.txt"
    #判断加密文件是否存在
    if !File::exists?(keypath) or !File::exists?(aclpath) then
        puts "授权文件不存在"
    end
    #读取并解密文件
    rsa_priv = OpenSSL::PKey::RSA.new(File.read(keypath))
    buffer   = rsa_priv.public_decrypt(Base64.decode64(File.read(aclpath)))        
    #判断时间是否过期
    if !buffer then
        puts '无效的授权文件'
    end
    #判断时间
    license = JSON.parse(buffer);
    #读取内容并验证
    puts license

相关文章

网友评论

    本文标题:ruby下使用rsa非对称加密

    本文链接:https://www.haomeiwen.com/subject/dffbkftx.html