C# 通过DES在CBC模式下实现初始向量(IV)
我不明白如何将初始向量用于DES。我已经阅读了CBC模式下初始向量的公式。如何将我的明文实现到公式中。 我能举例说明吗 明文=计算机,二进制=01000011 01001111 01001101 01010000 01010101 01010100 01000101 01010010,十六进制=43 4F 4D 50 55 54 45 52| 密钥=ENCRYPTT,二进制=01000101 01001110 01000011 01010010 01011001 01010000 01010100 01010100,十六进制=45 4E 43 52 59 50 54 谢谢 更新: 我有两个DES加密项目。第一个项目使用库,第二个项目不使用库。第一个项目使用库,使用CBC加密模式,第二个项目不使用库,不使用加密模式。加密的密文会导致第一个和第二个不同的程序。问题是我应该在第二个程序中添加什么,使密文得到相同的结果。如何在没有库的情况下在我的第二个程序上添加CBC模式 带库的第一个项目代码:C# 通过DES在CBC模式下实现初始向量(IV),c#,des,cbc-mode,C#,Des,Cbc Mode,我不明白如何将初始向量用于DES。我已经阅读了CBC模式下初始向量的公式。如何将我的明文实现到公式中。 我能举例说明吗 明文=计算机,二进制=01000011 01001111 01001101 01010000 01010101 01010100 01000101 01010010,十六进制=43 4F 4D 50 55 54 45 52| 密钥=ENCRYPTT,二进制=01000101 01001110 01000011 01010010 01011001 01010000 0101010
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.Mode = CipherMode.CBC;
public void getAll_RL()
{
string proses = "";
List<string> R = new List<string>();
List<string> L = new List<string>();
R.Add(txtR0.Text);
R.Add(txtR1.Text);
L.Add(txtL0.Text);
L.Add(txtL1.Text);
L.Add(txtR1.Text);
for (int x = 2; x <= 16; x++)
{
txtProses.Text = string.Empty;
string resultEks = string.Empty;
cp.Table(R[x - 1], "E", ref resultEks); //Tabel Ekspansi
string resultXOR = string.Empty;
string subKey = (string)DGVSubKey.Rows[x - 1].Cells[1].Value;
cp.XOR(resultEks, subKey, ref resultXOR);
string resultSBOX = string.Empty;
cp.SBOX(resultXOR, ref resultSBOX);
string resultPBOX = string.Empty;
cp.Table(resultSBOX, "PBOX", ref resultPBOX);
string resultXOR2 = string.Empty;
cp.XOR(resultPBOX, L[x - 1], ref resultXOR2);
R.Add(resultXOR2);
L.Add(resultXOR2);
}
txtL16.Text = L[16];
txtR16.Text = R[16];
txtRL16.Text = R[16] + L[16] ;
}
public void getIP1()
{
string RL16 = txtRL16.Text;
if (RL16.Length == 64)
{
string result = string.Empty;
cp.Table(RL16, "IP1", ref result);
txtIP1.Text = result;
}
}
public void getHexResult()
{
string IP1 = txtIP1.Text;
if (IP1.Length == 64)
{
List<string> AllBinary = new List<string>();
string Hex = string.Empty;
string ResultHex = string.Empty;
for (int x = 0; x < 8; x++)
{
string string8 = IP1.Substring(8 * x, 8);
AllBinary.Add(string8);
string hex = Convert.ToInt64(AllBinary[x], 2).ToString("X");
Hex += hex + " ";
}
string[] hexSplit = Hex.TrimEnd().Split(' ');
for (int x = 0; x < 8; x++)
{
if (hexSplit[x].Length == 1)
{
ResultHex += "0" + hexSplit[x] +" ";
}
else
{
ResultHex += hexSplit[x] + " ";
}
}
txtHexResult.Text = ResultHex.TrimEnd();
}
}
不带库的第二个项目代码:
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.Mode = CipherMode.CBC;
public void getAll_RL()
{
string proses = "";
List<string> R = new List<string>();
List<string> L = new List<string>();
R.Add(txtR0.Text);
R.Add(txtR1.Text);
L.Add(txtL0.Text);
L.Add(txtL1.Text);
L.Add(txtR1.Text);
for (int x = 2; x <= 16; x++)
{
txtProses.Text = string.Empty;
string resultEks = string.Empty;
cp.Table(R[x - 1], "E", ref resultEks); //Tabel Ekspansi
string resultXOR = string.Empty;
string subKey = (string)DGVSubKey.Rows[x - 1].Cells[1].Value;
cp.XOR(resultEks, subKey, ref resultXOR);
string resultSBOX = string.Empty;
cp.SBOX(resultXOR, ref resultSBOX);
string resultPBOX = string.Empty;
cp.Table(resultSBOX, "PBOX", ref resultPBOX);
string resultXOR2 = string.Empty;
cp.XOR(resultPBOX, L[x - 1], ref resultXOR2);
R.Add(resultXOR2);
L.Add(resultXOR2);
}
txtL16.Text = L[16];
txtR16.Text = R[16];
txtRL16.Text = R[16] + L[16] ;
}
public void getIP1()
{
string RL16 = txtRL16.Text;
if (RL16.Length == 64)
{
string result = string.Empty;
cp.Table(RL16, "IP1", ref result);
txtIP1.Text = result;
}
}
public void getHexResult()
{
string IP1 = txtIP1.Text;
if (IP1.Length == 64)
{
List<string> AllBinary = new List<string>();
string Hex = string.Empty;
string ResultHex = string.Empty;
for (int x = 0; x < 8; x++)
{
string string8 = IP1.Substring(8 * x, 8);
AllBinary.Add(string8);
string hex = Convert.ToInt64(AllBinary[x], 2).ToString("X");
Hex += hex + " ";
}
string[] hexSplit = Hex.TrimEnd().Split(' ');
for (int x = 0; x < 8; x++)
{
if (hexSplit[x].Length == 1)
{
ResultHex += "0" + hexSplit[x] +" ";
}
else
{
ResultHex += hexSplit[x] + " ";
}
}
txtHexResult.Text = ResultHex.TrimEnd();
}
}
public void getAll_RL()
{
弦乐散文=”;
列表R=新列表();
列表L=新列表();
R.Add(txtR0.Text);
R.Add(txtR1.Text);
L.Add(txtL0.Text);
L.添加(txtL1.文本);
L.添加(txtR1.Text);
对于(int x=2;x这个问题似乎是离题的,因为它通常是关于IV对于CBC模式的攻击者来说是不可预测的。所以你应该使用一个安全的随机生成器来生成IV,并将IV与密文保持在一起。有不同的方案,但使用静态的、可预测的IV并不能阻止它。@owlstead感谢您的经验我的意思是,如果初始向量(IV)为使用CBC模式进行加密。IV我已经设置了与加密密钥相同的值。您能解释一下计算吗?IV是公共的,因此它永远不会与密钥具有相同的值。通常IV是一个纯随机数据块,在加密前与明文异或(以下向量是加密后的密文块)。如果IV是完全随机的,则必须与解密方进行通信。通常,它只是在密文前加上前缀(它总是恰好是一个块,因此可以轻松检索并随后跳过进行解密。抱歉@owlstead。我不太可能解释我的意思。IV是随机获得的。但我创建的项目,IV是从密钥输入中获得的。我编辑了我的意思。请查看我可以解决问题的更新以及它的工作方式。)ks加密模式。提前感谢您的解释