ruby下使用rsa非对称加密

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容