栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将Java中的加密代码转换为Ruby

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将Java中的加密代码转换为Ruby

加密代码:

def aes(key,string)  cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")  cipher.encrypt  cipher.padding = 1  cipher.key = hex_to_bin(Digest::SHA1.hexdigest('secret_key')[0..32])  cipher_text = cipher.update(string)  cipher_text << cipher.final  return bin_to_hex(cipher_text).upcaseend

解密代码:

def aes_decrypt(key, encrypted)  encrypted = hex_to_bin(encrypted.downcase)  cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")  cipher.decrypt  cipher.padding = 1  cipher.key = hex_to_bin(Digest::SHA1.hexdigest('secret_key')[0..32])  d = cipher.update(encrypted)  d << cipher.finalend

hex_to_bin和bin_to_hex

def hex_to_bin(str)  [str].pack "H*"enddef bin_to_hex(s)  s.unpack('C*').map{ |b| "%02X" % b }.join('')end

在我的情况下,Java代码使用默认的初始化向量,因此我没有设置任何iv,此外,那里

hex_to_bin
缺少一块。因此,此后,所有程序都开始正常工作。

希望有人遇到此问题对您有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495116.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号