gpt4 book ai didi

c# - .NET 是否支持 OFB cipherMode?

转载 作者:行者123 更新时间:2023-11-30 16:21:09 26 4
gpt4 key购买 nike

我正在使用 AESCryptoServiceProvider 类。我正在尝试测试不同的 CipherMode 值。

使用OFB模式时出现异常:指定的算法无效。

在文档中记录了模式:

AESCryptoServiceProvider 类 http://msdn.microsoft.com/es-es/library/system.security.cryptography.aescryptoserviceprovider.aspx

密码模式 http://msdn.microsoft.com/es-es/library/system.security.cryptography.ciphermode.aspx

我也看过这个类似的帖子,但没有找到答案:

Does .NET support of AES OFB

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace V_ModosDeEncadenamiento
{
class Program
{
static void Main(string[] args)
{
//Clave de 128
byte[] claveAES128Bits = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };


//Vector de inicialización
byte[] vectorInicializacion = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF };

//Texto plano de prueba para codificar. Bloque de 16 bytes todos iguales
byte[] textoPlanoBloques16BytesIguales =
{0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF};


//Nombre del fichero en disco con texto cifrado
String nombreFicheroECB = "zz_TextoCifradoECB.bin";
String nombreFicheroCBC = "zz_TextoCifradoCBC.bin";
String nombreFicheroCFB = "zz_TextoCifradoCFB.bin";
String nombreFicheroOFB = "zz_TextoCifradoOFB.bin";

//Array con el texto descifrado
byte[] textoDescifrado = new byte[textoPlanoBloques16BytesIguales.Length];

AesCryptoServiceProvider aesCrypto = new AesCryptoServiceProvider();
FileStream fileWrite;
FileStream fileRead;
ICryptoTransform encryptor;
ICryptoTransform decryptor;
CryptoStream stream;
CryptoStream streamOut;



//Establecer valores
aesCrypto.KeySize = 128;
aesCrypto.Key = claveAES128Bits;
aesCrypto.IV = vectorInicializacion;
aesCrypto.Padding = PaddingMode.PKCS7;

aesCrypto.Mode = CipherMode.OFB;
fileWrite = new FileStream(nombreFicheroOFB, FileMode.Create, FileAccess.Write, FileShare.None);
encryptor = aesCrypto.CreateEncryptor();
stream = new CryptoStream(fileWrite, encryptor, CryptoStreamMode.Write);

// THE EXCEPTION HAPPENS HERE **************************************************
//******************************************************************************
stream.Write(textoPlanoBloques16BytesIguales, 0, textoPlanoBloques16BytesIguales.Length);
stream.Flush();
stream.Close();
stream.Dispose();
fileWrite.Close();

fileRead = new FileStream(nombreFicheroOFB, FileMode.Open, FileAccess.Read, FileShare.None);
decryptor = aesCrypto.CreateDecryptor();
streamOut = new CryptoStream(fileRead, decryptor, CryptoStreamMode.Read);
streamOut.Read(textoDescifrado, 0, textoDescifrado.Length);
streamOut.Flush();
streamOut.Close();
streamOut.Dispose();
fileRead.Close();

//Mostrar datos descifrados
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Cifrado modo EFB.");
Console.WriteLine("Datos descifrados:");
for (int i = 0; i <= textoDescifrado.Length - 1; i++)
{
if (((i % 8) == 0) && (i != 0)) Console.WriteLine(); //8 bytes en cada linea
Console.Write(" {0:X2}", textoDescifrado[i]);
}

最佳答案

查看这篇文章:http://social.msdn.microsoft.com/Forums/is/netfxbcl/thread/891955cd-3125-487c-9746-9fd07f24b12f

RijndaelManaged 目前不支持 OFB 模式。您可以(几乎轻松地)在 ECB 模式之上自己编写 OFB 模式代码,或者使用辅助加密库,例如 BouncyCaSTLe、SecureBlackbox 等。

关于c# - .NET 是否支持 OFB cipherMode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13482408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com