`

ruby 加密 解密

 
阅读更多
require "openssl"
require "base64"

    def des_encrypt(str)
        cipher = OpenSSL::Cipher::Cipher.new("des-ecb")

        cipher.encrypt
        cipher.key = 'SSOFOUNDER'
        # cipher.random_iv
        # cipher.iv = IV

        en_str = cipher.update(str)
        en_str << cipher.final

        return Base64.encode64(en_str)
    end

    def des_decrypt(enscrypt_str)
        cipher = OpenSSL::Cipher::Cipher.new("des-ecb")

        cipher.decrypt
        cipher.key = 'SSOFOUNDER'
        # cipher.random_iv
        # cipher.iv = IV

        cipher.update(Base64.decode64(enscrypt_str)) + cipher.final
        # des_str << cipher.final
    end
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics