首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PBEWithMD5AndDES在Ruby中的实现

PBEWithMD5AndDES在Ruby中的实现
EN

Stack Overflow用户
提问于 2012-05-30 10:01:44
回答 4查看 2.5K关注 0票数 4

我试图得到一个明显在Java世界中流行的加密库的ruby实现-- PBEWithMD5AndDES

有人知道如何使用openssl或其他开源gem来执行与此格式兼容的加密/解密吗?

更新:

我使用了一个gem 奇尔卡特来实现它,但是它是付费的,我需要一个开源的解决方案。

EN

回答 4

Stack Overflow用户

发布于 2013-08-16 06:26:49

我知道它太老了,但我也有同样的问题,所以我解决了它,在这里它是加密的,盐是你的盐刺,密码密钥是你的密码密钥字符串,迭代是你想要使用的迭代次数。

代码语言:javascript
复制
def encrypt_account_number
cipher = OpenSSL::Cipher::Cipher.new("DES")
cipher.encrypt
cipher.pkcs5_keyivgen passkey, salt,iterations,digest
encrypted_account_number =  cipher.update(account_number)
encrypted_account_number << cipher.final
Base64.encode64(encrypted_account_number )
end

def decrypt_account_number
cipher = OpenSSL::Cipher::Cipher.new("DES")
base_64_code = Base64.decode64(account_number)
cipher.decrypt
cipher.pkcs5_keyivgen passkey, salt,iterations,digest

decrypted_account_number = cipher.update base_64_code
decrypted_account_number << cipher.final
decrypted_account_number
end
票数 2
EN

Stack Overflow用户

发布于 2012-05-30 22:51:20

假设ruby有一个DES实现,您不需要实际实现PBEWithMD5andDES。您需要实现的是密钥派生函数(从密码中获取密钥),然后将该派生密钥以适当的模式和填充方式传递给DES。

值得庆幸的是,关键派生函数在实现中并不特别安全,所以您可以自己安全地完成它。根据rfc,PBEwithMD5AndDES实际上是在CBC模式下与DES一起使用的PBKDF1 (一个ker派生函数)。

PBKDF1看起来并没有那么难实现。看起来您可以通过一个for循环和一个md5调用来完成这个任务。

注意,您可能仍然会得到一些奇怪的结果,因为Java和Ruby中可能会使用不同的填充方案。我假设规范之一是PKCs1.5填充,但快速地看一下,我无法确认这一点

5.1 PBKDF1 PBKDF1应用哈希函数(应该是MD2 6、MD5 19或SHA-1 18 )来派生密钥。派生密钥的长度有界。 根据哈希函数输出的长度,MD2和MD5为16倍,SHA-1为20倍。PBKDF1与密钥兼容。 PKCS #5 v1.5中的派生过程 建议使用PBKDF1仅是为了与现有的 应用程序,因为它生成的密钥可能不够大。 一些应用程序。 PBKDF1 (P,S,c,dkLen) 选项:哈希基础散列函数 输入:p密码、八进制字符串S salt、八位字节字符串c迭代计数、派生键的八进制dkLen预期长度、正整数,MD2或MD5最多为16,SHA-1最多为20。 输出: DK派生键,dkLen-八进制字符串。 步骤: 1.如果dkLen > 16表示MD2和MD5,或者dkLen > 20表示SHA-1,则输出“派生键太长”并停止。2.将c迭代的底层散列函数hash应用于密码P和salt S的级联,然后提取第一个dkLen八进制以生成一个派生密钥DK: T_1 = Hash (P ~+~*),T_2 = Hash (T_1),……T_c = Hash (T_{c-1}),DK =Tc<0.dkLen-1> 3.输出导出的键DK。

票数 1
EN

Stack Overflow用户

发布于 2014-03-07 11:50:08

至于它的价值,我在张贴我的python代码,它实际上是有效的(我有大量的加密值是用org.jasypt.util.text.BasicTextEncryptor完成的,我需要对它们进行解密。)

代码语言:javascript
复制
import base64
import hashlib
from Crypto.Cipher import DES

"""
Note about PBEWithMD5AndDES in java crypto library:

Encrypt:
  Generate a salt (random): 8 bytes
  <start derived key generation>
  Append salt to the password
  MD5 Hash it, and hash the result, hash the result ... 1000 times
  MD5 always gives us a 16 byte hash
  Final result: first 8 bytes is the "key" and the next is the "initialization vector"
  (there is something about the first 8 bytes needing to be of odd paraity, therefore
  the least significant bit needs to be changed to 1 if required. We don't do it, 
  maybe the python crypto library does it for us)
  <end derived key generation>

  Pad the input string with 1-8 bytes (note: not 0-7, so we always have padding)
    so that the result is a multiple of 8 bytes. Padding byte value is same as number of 
    bytes being padded, eg, \x07 if 7 bytes need to be padded.
  Use the key and iv to encrypt the input string, using DES with CBC mode.
  Prepend the encrypted value with the salt (needed for decrypting since it is random)
  Base64 encode it -> this is your result

Decrypt:
  Base64 decode the input message
  Extract the salt (first 8 bytes). The rest is the encoded text.
  Use derived key generation as in Encrypt above to get the key and iv
  Decrypt the encoded text using key and iv
  Remove padding -> this is your result

(I only have implemented decrypt here since that's all I needed, 
but encrypt should be straighforward as well)

"""

def get_derived_key(password, salt, count):
    key = password + salt
    for i in range(count):
        m = hashlib.md5(key)
        key = m.digest()
    return (key[:8], key[8:])

def decrypt(msg, password):
    msg_bytes = base64.b64decode(msg)
    salt = msg_bytes[:8]
    enc_text = msg_bytes[8:]
    (dk, iv) = get_derived_key(password, salt, 1000)
    crypter = DES.new(dk, DES.MODE_CBC, iv)
    text = crypter.decrypt(enc_text)
    # remove the padding at the end, if any
    return re.sub(r'[\x01-\x08]','',text)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10814071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档