1

I need to decrypt content encoded using the AES/OFB/NoPadding combo in C#. This doesn't seem to be supported natively: the following code won't do it as

var aes = new AesManaged
{
   Padding = PaddingMode.None,
   Mode = CipherMode.OFB,
   KeySize = 128,
   BlockSize = 128
};

Produces:

System.Security.Cryptography.CryptographicException:
Specified cipher mode is not valid for this algorithm.

The closest issue that I could find here on SO is this one, which uses BouncyCastle to do encryption:

Public Function DoEncryption(ByVal KeyArray() As Byte, ByVal IVArray() As Byte, ByVal Buffer     As Byte()) As Byte()

    Dim ae As New CipherKeyGenerator()
    ae.Init(New KeyGenerationParameters(New SecureRandom(), 256))
    Dim aesKeyParam As KeyParameter = ParameterUtilities.CreateKeyParameter("AES",     KeyArray)
    Dim aesIVKeyParam As ParametersWithIV = New ParametersWithIV(aesKeyParam, IVArray)
    Dim cipher As IBufferedCipher = CipherUtilities.GetCipher("AES/OFB/NoPadding")
    cipher.Init(True, aesIVKeyParam)
    Dim encrypted() As Byte = cipher.DoFinal(Buffer)
    Return encrypted

End Function

Other issues (like this one) contain more information but also lots of custom code - I'd rather use BouncyCastle. Can anybody help me with the decryption, or point me to some helpful documentation?

ticofab
  • 7,551
  • 13
  • 49
  • 90
  • "BouncyCastle C# APIs", there is no C# API and VB.NET API, that's not how .NET works. If you are asking how to convert VB.NET code to C# code, there are even ways to do that online in a matter of seconds – Camilo Terevinto Jun 19 '19 at 08:58
  • Thanks for the pointer to the online conversion @CamiloTerevinto. I have updated my question too to reflect more what I need and eliminate the false dichotomy you pointed out. – ticofab Jun 19 '19 at 09:02
  • How is the code you added even *similar* to the VB.NET version? Your update reflects that you did not understand my comment – Camilo Terevinto Jun 19 '19 at 09:28
  • Thanks for your additional comment @CamiloTerevinto :) I added code to show what I tried to achieve on my own - it wasn't meant to be similar to the VB code below it but only to give more context to my question. – ticofab Jun 19 '19 at 09:39
  • Step 1: go to [this site](http://converter.telerik.com/). Step 2: choose VB.NET > C#. Step 3: paste the VB.NET code. Step 4: copy the C# code to your project. I don't see what's the difficulty here – Camilo Terevinto Jun 19 '19 at 09:40
  • I have done all steps 1 to 4 already, thanks to your first comment. Now I am busy adapting the resulting code to decryption and will post an update once I get something working. I thank you sincerely for your time and interest in helping me out :) – ticofab Jun 19 '19 at 09:45
  • I can't make sense of the question. OFB is not supported by .NET classes, but you can use Bouncycastle. So what's the question? – President James K. Polk Jun 19 '19 at 11:33
  • Thanks for your interest @JamesKPolk, see my answer below. Peace everybody. – ticofab Jun 19 '19 at 12:01

1 Answers1

0

From the code in the actual question, the only step necessary to trigger decryption instead of encryption was to change the boolean parameter in the cipher.Init invocation:

cipher.Init(False, aesIVKeyParam) // False == decryption, True == encryption

The final snippet in C# is

private static byte[] DoDecryption(byte[] keyArray, byte[] ivArray, byte[] encoded)
{
    var aesKeyParam = ParameterUtilities.CreateKeyParameter("AES", keyArray);
    var aesIvKeyParam = new ParametersWithIV(aesKeyParam, ivArray);
    var cipher = CipherUtilities.GetCipher("AES/OFB/NOPADDING");
    cipher.Init(false, aesIvKeyParam);
    var decrypted = cipher.DoFinal(encoded);
    return decrypted;
}
ticofab
  • 7,551
  • 13
  • 49
  • 90