顯示具有 CBC 標籤的文章。 顯示所有文章
顯示具有 CBC 標籤的文章。 顯示所有文章

2022年4月16日 星期六

[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(四)

[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(四)

2022-04-16

續

[研究][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

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

參考下面這篇進行修改

RijndaelManaged 類別 (System.Security.Cryptography) | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.security.cryptography.rijndaelmanaged?view=net-6.0

Default2.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" 
    Inherits="WebApplication1.Default2" %>

<!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">
        key:<asp:TextBox ID="TextBox1" runat="server" Width="350px"></asp:TextBox><br />
        iv:<asp:TextBox ID="TextBox2" runat="server" Width="350px"></asp:TextBox><br />
        原文:<asp:TextBox ID="TextBox3" runat="server" Width="350px"></asp:TextBox><br />
        加密後:<asp:TextBox ID="TextBox4" runat="server" Width="350px"></asp:TextBox><br />
        解密後:<asp:TextBox ID="TextBox5" runat="server" Width="350px"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" /><br />
        <asp:Label ID="Label_MSG" runat="server"></asp:Label>
    </form>
</body>
</html>

Default2.aspx.cs


using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace WebApplication1
{
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Text = "1234567812345678"; // key, iv
            TextBox2.Text = "12345678"; // key, iv 長度必須是 8
            TextBox3.Text = "Test String";
        }

        // https://docs.microsoft.com/zh-tw/dotnet/api/system.security.cryptography.rijndaelmanaged?view=net-6.0
        protected void Button1_Click1(object sender, EventArgs e)
        {
            try
            {

                string original = "Here is some data to encrypt!";

                // Create a new instance of the RijndaelManaged
                // class.  This generates a new key and initialization
                // vector (IV).
                using (RijndaelManaged rijAlg = new RijndaelManaged())
                {
                    // 亂碼,不喜歡,保存不方便
                    //rijAlg.GenerateKey();
                    //rijAlg.GenerateIV();
                    //TextBox1.Text = System.Text.Encoding.Unicode.GetString(rijAlg.Key);
                    //TextBox2.Text = System.Text.Encoding.Unicode.GetString(rijAlg.IV);
                    //TextBox5.Text = TextBox2.Text.Length.ToString();
                        
                    rijAlg.Key = System.Text.Encoding.Unicode.GetBytes(TextBox1.Text);

                    //指定的初始化向量 (IV) 不符合此演算法的區塊大小。 
                    // Rijndael IV size doesn't match block size
                    //byte[] iv = new byte[128];
                    //string ivStr = "";
                    //for (int i = 0; i < 16; i++)
                    //    ivStr += "12345678";
                    //TextBox2.Text = ivStr;
                    rijAlg.IV = System.Text.Encoding.Unicode.GetBytes(TextBox2.Text);

                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = EncryptStringToBytes(original, rijAlg.Key, rijAlg.IV);

                    // Decrypt the bytes to a string.
                    string roundtrip = DecryptStringFromBytes(encrypted, rijAlg.Key, rijAlg.IV);

                    //Display the original data and the decrypted data.
                    //Console.WriteLine("Original:   {0}", original);
                    //Console.WriteLine("Round Trip: {0}", roundtrip);
                    TextBox4.Text = original;
                    TextBox5.Text = roundtrip;
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine("Error: {0}", e.Message);
                Label_MSG.Text = "不明錯誤。";
                if (ex!=null)
                    Label_MSG.Text = ex.Message;
            }
        }

        static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted;
            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                //rijAlg.Mode = CipherMode.ECB; // 微軟建議別用;可執行
                //rijAlg.Mode = CipherMode.CBC; // 預設,Fortify SCA 認為不安全
                rijAlg.Mode = CipherMode.CFB; // Fortify SCA 認為安全;可執行
                //rijAlg.Mode = CipherMode.OFB; // Fortify SCA 認為安全;System.Security.Cryptography.CryptographicException: '指定的 Cipher 模式對此演算法而言是無效的。'
                                                  // 經查,RijndaelManaged目前不支援OFB模式。
                //rijAlg.Mode = CipherMode.CTS; // Fortify SCA 認為安全;System.Security.Cryptography.CryptographicException: '指定的 Cipher 模式對此演算法而言是無效的。'
                //rijAlg.Mode = CipherMode.CTR; // Fortify SCA 建議,但沒有這種模式

                // Create an encryptor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {

                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

            // Return the encrypted bytes from the memory stream.
            return encrypted;
        }

        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;
                rijAlg.Mode = CipherMode.CFB;

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return plaintext;
        }

    }
}

實際測試

解密後字串尾不會再多了 �����,正常了。

(完)

相關

[研究][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



[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(三)

[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(三)

2022-04-16

續

[研究][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

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

測試一下

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.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">
        key:<asp:TextBox ID="TextBox1" runat="server">1234567812345678</asp:TextBox><br />
        iv:<asp:TextBox ID="TextBox2" runat="server">1234567812345678</asp:TextBox><br />
        原文:<asp:TextBox ID="TextBox3" runat="server" Width="350px"></asp:TextBox><br />
        加密後:<asp:TextBox ID="TextBox4" runat="server" Width="350px"></asp:TextBox><br />
        解密後:<asp:TextBox ID="TextBox5" runat="server" Width="350px"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />

    </form>
</body>
</html>

Default.aspx.cs


using System;
using System.Security.Cryptography;
using System.Text;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Text = "1234567812345678"; // key, iv
            TextBox2.Text = "1234567812345678"; // key, iv
            TextBox3.Text = "Test String";
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            String encryptData = Encrypt(TextBox3.Text, TextBox1.Text, TextBox2.Text);
            TextBox4.Text = encryptData;

            String decryptData = Decrypt(TextBox4.Text, TextBox1.Text, TextBox2.Text);
            TextBox5.Text = decryptData;
        }
        public static string Encrypt(string toEncrypt, string key, string iv)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            //進階加密標準(英語:Advanced Encryption Standard,縮寫:AES)
            //https://zh.wikipedia.org/wiki/%E9%AB%98%E7%BA%A7%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86

            // RijndaelManaged 類別
            // https://msdn.microsoft.com/zh-tw/library/system.security.cryptography.rijndaelmanaged(v=vs.110).aspx

            //區塊(Block)密碼工作模式(mode of operation)
            //https://zh.wikipedia.org/wiki/%E5%9D%97%E5%AF%86%E7%A0%81%E7%9A%84%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F

            RijndaelManaged myRijndael = new RijndaelManaged();
            myRijndael.KeySize = 256;
            myRijndael.Key = keyArray;
            myRijndael.IV = ivArray;  // 初始化向量 initialization vector (IV)
            //myRijndael.Mode = CipherMode.ECB; // 微軟建議別用;可執行
            //myRijndael.Mode = CipherMode.CBC; // 預設;Fortify SCA 認為不安全;密碼分組連結(CBC,Cipher-block chaining)模式
            myRijndael.Mode = CipherMode.CFB; // Fortify SCA 認為安全;可執行
            //myRijndael.Mode = CipherMode.OFB; // Fortify SCA 認為安全;System.Security.Cryptography.CryptographicException: '指定的 Cipher 模式對此演算法而言是無效的。'
                                                // 經查,RijndaelManaged目前不支援OFB模式。
            //myRijndael.Mode = CipherMode.CTS; // Fortify SCA 認為安全;System.Security.Cryptography.CryptographicException: '指定的 Cipher 模式對此演算法而言是無效的。'
            //rijAlg.Mode = CipherMode.CTR; // Fortify SCA 建議,但沒有這種模式

            myRijndael.Padding = PaddingMode.Zeros;

            ICryptoTransform cTransform = myRijndael.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string Decrypt(string toDecrypt, string key, string iv)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            RijndaelManaged myRijndael = new RijndaelManaged();
            myRijndael.KeySize = 256;
            myRijndael.Key = keyArray;
            myRijndael.IV = ivArray;

            //myRijndael.Mode = CipherMode.ECB;
            //myRijndael.Mode = CipherMode.CBC;
            myRijndael.Mode = CipherMode.CFB;
            //myRijndael.Mode = CipherMode.OFB;
            //myRijndael.Mode = CipherMode.CTS;

            myRijndael.Padding = PaddingMode.Zeros;

            ICryptoTransform cTransform = myRijndael.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }

    }
}

實際測試,最後能用的只有 CFB模式


myRijndael.Mode = CipherMode.CFB; 

但也發現新問題,下面這篇 WinForm 解密字串是正常的,
[研究] [C#] AES-256-CBC 加密字串、解密字串
http://shaurong.blogspot.com/2016/11/c-aes-256-cbc_22.html

結果本篇 WebForm 解密後字串尾多了 �����,待研究。

後面的測試發現,疑似為 UTF8 問題,好像要改為 UniCode?待測。

**********
正常版本


[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(四)
https://shaurong.blogspot.com/2022/04/aspnetfortify-sca-ciphermodecbc-weak_59.html

(完)

相關

[研究][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




[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(一)

[研究][ASP.NET]Fortify SCA 報告 CipherMode.CBC 有 Weak Encryption: Insecure Mode of Operation 問題之解決(一)

2022-04-16

本篇一系列文並非今天發生,而是今天整理。


出事的程式碼是 

rijAlg.Mode = CipherMode.CBC;   


********************************************************************************

Fortify SCA 的建議

Recommendations:

當加密的資料超過一個區塊時,請避免使用 ECB 和 CBC 作業模式。在與 SSL 搭配使用時,CBC 模式的效能會有些降低而導致嚴重風險 [1]。因此,請改用 CCM (Counter with CBC-MAC) 模式,或者,如果效能需優先考量,則使用 GCM (Galois/Counter Mode) 模式 (如果可用)。

範例 2:以下程式碼使用 AES 加密搭配 GCM 模式:

  ...

  var cipher = new AesGcm(securityKeyArray)

  ...

References:

[1] CVE 2014-3566, http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-3566

[2] Timing vulnerabilities with CBC-mode symmetric decryption using padding, https://docs.microsoft.com/en-us/dotnet/standard/security/vulnerabilities-cbc-mode

[3] Standards Mapping - Common Weakness Enumeration, CWE ID 327

[4] Standards Mapping - DISA Control Correlation Identifier Version 2, CCI-002450

[5] Standards Mapping - FIPS200, MP

[6] Standards Mapping - General Data Protection Regulation, Insufficient Data Protection

[7] Standards Mapping - NIST Special Publication 800-53 Revision 4, SC-13 Cryptographic Protection (P1)

[8] Standards Mapping - NIST Special Publication 800-53 Revision 5, SC-13 Cryptographic Protection

[9] Standards Mapping - OWASP Application Security Verification Standard 4.0, 2.6.3 Look-up Secret Verifier Requirements (L2 L3), 2.9.3 Cryptographic Software and Devices Verifier Requirements (L2 L3), 6.2.1 Algorithms (L1 L2 L3), 6.2.2 Algorithms (L2 L3), 8.3.7 Sensitive Private Data (L2 L3), 9.1.2 Communications Security Requirements (L1 L2 L3), 9.1.3 Communications Security Requirements (L1 L2 L3)

[10] Standards Mapping - OWASP Mobile Top 10 Risks 2014, M6 Broken Cryptography

[11] Standards Mapping - OWASP Top 10 2004, A8 Insecure Storage

[12] Standards Mapping - OWASP Top 10 2007, A8 Insecure Cryptographic Storage

[13] Standards Mapping - OWASP Top 10 2010, A7 Insecure Cryptographic Storage

[14] Standards Mapping - OWASP Top 10 2013, A6 Sensitive Data Exposure

[15] Standards Mapping - OWASP Top 10 2017, A3 Sensitive Data Exposure

[16] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1, Requirement 6.5.8

[17] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2, Requirement 6.3.1.3, Requirement 6.5.8

[18] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0, Requirement 6.5.3

[19] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0, Requirement 6.5.3

[20] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1, Requirement 6.5.3

[21] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2, Requirement 6.5.3

[22] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1, Requirement 6.5.3

[23] Standards Mapping - Payment Card Industry Software Security Framework 1.0, Control Objective 7.4 - Use of Cryptography

[24] Standards Mapping - Payment Card Industry Software Security Framework 1.1, Control Objective 7.4 - Use of Cryptography

[25] Standards Mapping - SANS Top 25 2009, Porous Defenses - CWE ID 327

[26] Standards Mapping - SANS Top 25 2010, Porous Defenses - CWE ID 327

[27] Standards Mapping - SANS Top 25 2011, Porous Defenses - CWE ID 327

[28] Standards Mapping - Security Technical Implementation Guide Version 3.1, APP3150.1 CAT II

[29] Standards Mapping - Security Technical Implementation Guide Version 3.10, APP3150.1 CAT II

[30] Standards Mapping - Security Technical Implementation Guide Version 3.4, APP3150.1 CAT II

[31] Standards Mapping - Security Technical Implementation Guide Version 3.5, APP3150.1 CAT II

[32] Standards Mapping - Security Technical Implementation Guide Version 3.6, APP3150.1 CAT II

[33] Standards Mapping - Security Technical Implementation Guide Version 3.7, APP3150.1 CAT II

[34] Standards Mapping - Security Technical Implementation Guide Version 3.9, APP3150.1 CAT II

[35] Standards Mapping - Security Technical Implementation Guide Version 4.1, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[36] Standards Mapping - Security Technical Implementation Guide Version 4.10, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[37] Standards Mapping - Security Technical Implementation Guide Version 4.11, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[38] Standards Mapping - Security Technical Implementation Guide Version 4.2, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[39] Standards Mapping - Security Technical Implementation Guide Version 4.3, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[40] Standards Mapping - Security Technical Implementation Guide Version 4.4, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[41] Standards Mapping - Security Technical Implementation Guide Version 4.5, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[42] Standards Mapping - Security Technical Implementation Guide Version 4.6, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[43] Standards Mapping - Security Technical Implementation Guide Version 4.7, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[44] Standards Mapping - Security Technical Implementation Guide Version 4.8, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[45] Standards Mapping - Security Technical Implementation Guide Version 4.9, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

[46] Standards Mapping - Security Technical Implementation Guide Version 5.1, APSC-DV-002010 CAT II, APSC-DV-002040 CAT II

********************************************************************************

解決:

CipherMode 列舉 (System.Security.Cryptography) | Microsoft Docs

https://docs.microsoft.com/zh-tw/dotnet/api/system.security.cryptography.ciphermode?view=netframework-4.8&f1url=%3FappId%3DDev16IDEF1%26l%3DZH-TW%26k%3Dk(System.Security.Cryptography.CipherMode.OFB);k(TargetFrameworkMoniker-.NETFramework,Version%253Dv4.8);k(DevLang-csharp)%26rd%3Dtrue

因為微軟建議別用 ECB,Fortify SCA 又說 CBC 不安全,建議用 CTR ( 後來 Fortify 有修過說明,沒再提),但 CipherMode 不支援 CTR,敝人選了個代碼較高的 CTS (代碼 5)


rijAlg.Mode = CipherMode.CTS;   

編譯正常。

(完)

相關

[研究][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

2016年11月22日 星期二

[研究] [C#] AES-256-CBC 加密字串、解密字串

[研究] [C#] AES-256-CBC 加密字串、解密字串

2022-04-16 最好看一下本文最後相關的幾篇
****************************************
2016-11-22



Visual Studio 2015 with Update 3
WinForm 程式

Form1.cs

using System;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace AES256CBCEncryptString
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "1234567812345678"; // key, iv
            textBox2.Text = "1234567812345678"; // key, iv
            richTextBox1.Text = "Test String";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            String encryptData = Encrypt(richTextBox1.Text, textBox1.Text, textBox2.Text);
            richTextBox2.Text = encryptData;

            String decryptData = Decrypt(richTextBox2.Text, textBox1.Text, textBox2.Text);
            richTextBox3.Text = decryptData;
        }
        public static string Encrypt(string toEncrypt, string key, string iv)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            //進階加密標準(英語:Advanced Encryption Standard,縮寫:AES)
            //https://zh.wikipedia.org/wiki/%E9%AB%98%E7%BA%A7%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86

            // RijndaelManaged 類別
            // https://msdn.microsoft.com/zh-tw/library/system.security.cryptography.rijndaelmanaged(v=vs.110).aspx

            //區塊(Block)密碼工作模式(mode of operation)
            //https://zh.wikipedia.org/wiki/%E5%9D%97%E5%AF%86%E7%A0%81%E7%9A%84%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.KeySize = 256;
            rDel.Key = keyArray;
            rDel.IV = ivArray;  // 初始化向量 initialization vector (IV)
            rDel.Mode = CipherMode.CBC; // 密碼分組連結(CBC,Cipher-block chaining)模式
            rDel.Padding = PaddingMode.Zeros;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string Decrypt(string toDecrypt, string key, string iv)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.KeySize = 256;
            rDel.Key = keyArray;
            rDel.IV = ivArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.Zeros;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }


    }
}



(完)

相關

[研究][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