AES 密钥和 IV 生成 16 个字符长度 [英] AES Key and IV generate with 16 character length

查看:62
本文介绍了AES 密钥和 IV 生成 16 个字符长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在 vb.net 中生成 AES 密钥和 IV.
http://programmers-en.high-way.info/vb/aes.html
如上链接,有 AesIV 和 AesKey 的声明.
但我不想对 AesIV 和 AesKey 使用硬编码.

I am searching for how to generate AES key and IV in vb.net.
http://programmers-en.high-way.info/vb/aes.html
As above link, there is the declaration for AesIV and AesKey.
But I don't want to use hard code for AesIV and AesKey.

Private Const AesIV As String = "!QAZ2WSX#EDC4RFV"  
Private Const AesKey As String = "5TGB&YHN7UJM(IK<"  

我想要做的是我想像上面的例子一样自动生成随机密钥和IV完全相同的16个字符.
请帮助我的人.谢谢...

What I want to do is I want to generate random key and IV automatically exactly the same 16character like above example.
Please help me somebody. Thanks...

推荐答案

使用 RNGCryptoServiceProvider 以生成加密强序列.

Use the RNGCryptoServiceProvider to generate cryptographically strong sequences.

Imports System.Security.Cryptography

Public Function GenerateKey(ByVal Length As Integer) As Byte()
    Dim ReturnArray(Length - 1) As Byte
    Using RNG As New RNGCryptoServiceProvider
        RNG.GetBytes(ReturnArray)
    End Using
    Return ReturnArray
End Function

示例用法:

AES.Key = GenerateKey(16)
AES.IV = GenerateKey(16)

注意:您必须使用完全相同的密钥和 IV 再次解密数据,因此您必须能够以某种方式取回它.

NOTE: You must use the exact same key and IV to decrypt the data again, so you must be able to get it back somehow.

这篇关于AES 密钥和 IV 生成 16 个字符长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆