[研究][ASP.NET]「 指定的初始化向量 (IV) 不符合此演算法的區塊大小」之解決
2022-04-17
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
之前寫程式遇到「 指定的初始化向量 (IV) 不符合此演算法的區塊大小」
Rijndael IV size doesn't match block size
那到底區塊大小應該多少?
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> Key:<asp:Label ID="Label_Key" runat="server"></asp:Label><br /> IV:<asp:Label ID="Label_IV" runat="server"></asp:Label><br /> Key Size:<asp:Label ID="Label_KeySize" runat="server"></asp:Label><br /> Rijndael IV size (block size) :<asp:Label ID="Label_IVSize" runat="server"> </asp:Label><br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html> |
Default.aspx.cs
using System;
using System.Security.Cryptography;
namespace WebApplication3
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.GenerateKey();
rijAlg.GenerateIV();
Label_Key.Text = System.Text.Encoding.Unicode.GetString(rijAlg.Key);
Label_IV.Text = System.Text.Encoding.Unicode.GetString(rijAlg.IV);
Label_KeySize.Text = Label_Key.Text.Length.ToString();
Label_IVSize.Text = Label_IV.Text.Length.ToString();
}
}
}
}
|
測試
rijAlg.Key = System.Text.Encoding.Unicode.GetBytes("1234567890123456"); rijAlg.IV = System.Text.Encoding.Unicode.GetBytes("12345678"); |
(完)
相關
[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(一)
https://shaurong.blogspot.com/2022/04/aspnetfortify-sca-ciphermodecbc-weak.html
[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(二)
https://shaurong.blogspot.com/2022/04/aspnetaspnetfortify-sca-ciphermodecbc.html
[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(三)
https://shaurong.blogspot.com/2022/04/aspnetfortify-sca-ciphermodecbc-weak_16.html
[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(四)
https://shaurong.blogspot.com/2022/04/aspnetfortify-sca-ciphermodecbc-weak_59.html
[研究] [C#] AES-256-CBC 加密字串、解密字串
http://shaurong.blogspot.com/2016/11/c-aes-256-cbc_22.html
System.Security.Cryptography 命名空間 | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.security.cryptography?view=net-6.0
沒有留言:
張貼留言