【问题标题】:Random Number Generator with AES Counter Mode - Ruby具有 AES 计数器模式的随机数生成器 - Ruby
【发布时间】:2016-04-06 09:15:03
【问题描述】:

我的目标是在 Ruby 中实现一个基于 AES 计数器模式的随机数生成器。

我自己实现了计数器模式如下:

require './aes_helpers'
require 'openssl'

class AES_CTR_Random

  # Setup basic aes, since a mode is required ecb is used,
  # which constructs same cipher text blocks for same plain text blocks
  def intialize_openssl_aes(encrypt_or_decrypt, mode, key, iv, data, bits)
    key_s = byte_array_to_byte_string(key)
    data_s = byte_array_to_byte_string(data)

    aes = OpenSSL::Cipher::AES.new(bits, mode)
    aes.send(encrypt_or_decrypt)
    aes.padding = 0
    aes.key = key_s
    aes.iv = byte_array_to_byte_string(iv) if iv
    encrypted = aes.update(data_s) + aes.final

    byte_string_to_byte_array(encrypted)
 end

  # Encrypt or decrypt one AES block
  def process_aes_block(encrypt_or_decrypt, key, block)
    intialize_openssl_aes(encrypt_or_decrypt, 'ECB', key, nil, block, 128)
end

  # Generate a stream cipher key using given AES key and initialization vector
  def generate_aes_ctr_stream_key(key, iv, length)
    iv = byte_array_to_integer(iv)
    stream_key = []
    while stream_key.length < length
    stream_key += process_aes_block(:encrypt, key, integer_to_byte_array(iv))
    iv += 1
    end
    stream_key.take(length)
  end

  # Run Cipher Counter Mode
  def run_ctr(key, iv, data)
    # Get a properly sized stream cipher key using aes cipher counter mode
    stream_key = generate_aes_ctr_stream_key(key, iv, data.length)

    # Stream cipher decryption
    byte_array_xor_byte_array(stream_key, data)
  end

  # Encrypt plaintext with aes 128 ctr using given key and iv
  def encrypt_ctr(key, iv, plaintext)
    key = hexadecimal_string_to_byte_buffer(key)
    iv = hexadecimal_string_to_byte_buffer(iv)
    plaintext = byte_string_to_byte_array(plaintext)

    encrypted = run_ctr(key, iv, plaintext)

    byte_buffer_to_hexadecimal_string(iv + encrypted)
  end

  # Decrypt aes 128 ctr encrypted ciphertext
  def decrypt_ctr(key, ciphertext)
    key = hexadecimal_string_to_byte_buffer(key)
    ciphertext = hexadecimal_string_to_byte_buffer(ciphertext)

    iv = ciphertext.take(16)
    ciphertext = ciphertext.drop(16)

    plaintext = run_ctr(key, iv, ciphertext)

    byte_array_to_byte_string(plaintext)
  end
end

但现在我不太明白如何从这个实现中获取随机数。

谁能指导我解决问题?

【问题讨论】:

  • 你是问如何以统一的方式将随机字节转换为特定区间内的随机数?
  • @ArtjomB。是的,正是我在问如何生成安全的随机数。
  • @ArtjomB。您对如何实现这样的功能(PNGR AES CTR)有什么建议吗?

标签: ruby encryption random cryptography aes


【解决方案1】:

您可以使用 AES 来构建 CTR_DRBG,如 NIST Special Publication 800-90A 第 10.2 节:10.2 基于块密码的 DRBG 机制中指定的,它使用 CTR 块密码操作模式作为基础原语。

流密码的缺点是它不重复块,这可能会使输出略有偏差。之前的一个答案提到了AES-CTR,它有这个缺点。

【讨论】:

  • 可能必须改进您的 IV/nonce 和缓冲区处理,以将其用作原语。
  • 看来 OP 想要来自 DRBG 的 DRNG
  • @MaartenBodewes 是的,该规范在理论上似乎很好,但由于我对密码学真的很陌生,所以对于我的目的和知识来说它似乎太先进了。
猜你喜欢
  • 2011-04-12
  • 2017-08-30
  • 2012-11-15
  • 1970-01-01
  • 2014-02-05
  • 2015-06-08
  • 2020-09-04
  • 2019-08-06
  • 1970-01-01
相关资源
最近更新 更多