dtj4307 2014-08-13 15:46
浏览 340

不带IV的AES128

I am trying to decypher some AES128 data that does not have the IV. Go provides easy way to decypher with IV, but I cannot figure out how to go about not using the IV. So here what I have so far:

block, err := aes.NewCipher(key)

if err != nil {
    panic(err)
}

if len(data)%aes.BlockSize != 0 {
    panic("ciphertext is not a multiple of the block size")
}

fmt.Printf("Decyphered:
%s
", data)

So I am struggling to figure out how to decypher using the block.

Thanks...

  • 写回答

1条回答 默认 最新

  • doucang8303 2014-08-13 16:44
    关注

    I'm assuming you're using CBC here, but CFB mode should work the same.

    Note that since the IV isn't considered secret, it's often prepended to the ciphertext itself for convenience.

    Because of the way these modes handle the IV, if you use the incorrect IV you only lose the first block of plaintext. If the actual IV is there, you end up decrypting random data at the beginning of your plaintext output, so it doesn't hurt to simply try to decrypt it with an empty IV. Without the original IV though, you cannot get back that first block (short of using brute-force).

    <kbd>Example</kbd>

    key := []byte("YELLOW SUBMARINE")
    plaintext := []byte("exampleplaintext that is longer than a single block and some pad")
    
    if len(plaintext)%aes.BlockSize != 0 {
        panic("plaintext not multiple of block size")
    }
    
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    
    // The IV needs to be unique, but not secure. Therefore it's common to
    // include it at the beginning of the ciphertext.
    ciphertext := make([]byte, aes.BlockSize+len(plaintext))
    iv := ciphertext[:aes.BlockSize]
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
        panic(err)
    }
    
    mode := cipher.NewCBCEncrypter(block, iv)
    mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
    fmt.Printf("%x
    ", ciphertext)
    
    // Now Decrypt with wrong IV
    iv = make([]byte, 16)
    
    // If you wanted the correct IV, in thise case we could pull it from the ciphertext
    //iv = ciphertext[:aes.BlockSize]
    //ciphertext = ciphertext[aes.BlockSize:]
    
    if len(ciphertext)%aes.BlockSize != 0 {
        panic("ciphertext is not a multiple of the block size")
    }
    
    mode = cipher.NewCBCDecrypter(block, iv)
    newPlaintext := make([]byte, len(ciphertext))
    mode.CryptBlocks(newPlaintext, ciphertext)
    
    fmt.Printf("%s
    ", newPlaintext)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀