AES Missing Mode - c#

I'm using AES on WP8 (Windows Phone 8) in C# on Visual Studio, and System.Security.Cryptography does not contain the attribute 'Mode' for AESManaged.
I've looked up this problem for the past 3 days now, and haven't found any reference or anything to import.
The code I am currently using is:
AesManaged cipher = new AesManaged();
cipher.BlockSize = 8;
/*cipher.Mode = CipherMode.CFB;
cipher.Padding = PaddingMode.None;*/
//cipher.KeySize = 128;
//cipher.FeedbackSize = 8;
cipher.Key = key;
cipher.IV = key;
return cipher;
While the BlockSize throws an exception 'Specified block size is not valid for this algorithm.'
I was originally using RijndaelManaged but that isn't available on WP8 but according to this it should be available.

Silverlight verison of AES have no mode property. Here is MSDN article about that.
"The AES algorithm is essentially the Rijndael symmetric algorithm with a fixed block size and iteration count. This class functions the same way as the .NET Framework RijndaelManaged class but limits blocks to 128 bits and does not allow feedback modes.
The cipher mode is always CBC, and the padding mode is always PKCS7."
You can extract AES from BounceCastle library if you need more modes and flexibility. I did that before.

Related

RijndaelManaged decrypt encrypted with 8 bytes key

I have the next code that perfectly works encrypting and decrypting in net framework even with a invalid key for RijndaelManaged, but now in the netCore it doesn’t work because its needs a 16 bytes key, I want to recreate the key that the library in net frameworks makes with a key expansion to decrypt in a net core app, but I don’t now how
I found a similar problem but in PHP here RijndaelManaged.CreateEncryptor key expansion, but it says “just chop off the last 2 bytes” but of what? and how I complete the rest of bytes?
//Code tha works in the framework app
///8 bytes
_key = System.Text.ASCIIEncoding.UTF8.GetBytes("abcdefgh");
//16 bytes
_iv = System.Text.ASCIIEncoding.UTF8.GetBytes("abcdefghijklmnop");
_provider = new RijndaelManaged();
_provider.Mode = CipherMode.CBC;
_provider.KeySize = 128;
_provider.CreateEncryptor(_key, _iv)

CWE-326 on FIPS compliant AES256

Why does Veracode flag the following FIPS compliant C#?
var cipher = new AesCng()
{
BlockSize = 128,
KeySize = 256,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
Hi: current Veracode scans likely wouldn't flag CWE 326 on a use of AES with a key size of 256. If this was a recent scan, could you please reach out to Veracode Support so we can take a closer look to find out why this was flagged?
If this was a result from an older scan, you may want to try rescanning the application to see if the issue is still flagged.

What does RijndaelManaged encryption do with invalid key sizes

We are trying to integrate with a legacy c# application that uses RijndaelManaged for symmetric encryption. However it appears that they have used a 13 byte string as an encryption key!
The code is basically:
var initVectorBytes = Encoding.ASCII.GetBytes("16-char string");
var keyBytes = Encoding.ASCII.GetBytes("13-char string");
var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC };
var decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
var memoryStream = new System.IO.MemoryStream(encryptedbytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
....
In theory this shouldn't work - the docs clearly say "The key size must be 128, 192, or 256 bits" and when we try this (on a Xamarin/Mono compiler - don't have easy access to .net at the moment) it throws an exception.
But it apparently works on the legacy system, and they have unit tests that also call CreateDecryptor with a 13 byte key; so presumably a real .net system does somehow do something with this code. (I note that the docs for .net version 2.0 don't talk about key length restrictions - the code is compiled using .net 3.5 however)
Is it possible that it uses the Rijndael algorithm with a 104 byte key and block size? Or would it somehow pad the key or something?

AES256 Encryption in C#

I am using System.Security.Cryptography.RijndaelManaged class in C#(.NET 3.5) to do encryption with settings:
RijndaelManaged AesCrypto = new RijndaelManaged();
AesCrypto.BlockSize = 128;
AesCrypto.Mode = CipherMode.CBC;
CryptoStream CryptStream = new CryptoStream(memStream1,
AesCrypto.CreateEncryptor(EncryptionKey1, EncryptionIV1),
CryptoStreamMode.Write);
And with 256 bit key and IV. I believe that results in AES256. Am I right?
Would there be any differences if I am using System.Security.Cryptography.AesManaged class?
Also, I was thinking, we TRUST Microsoft implementation of AES, can this be verified, or maybe one should write his own implementation of AES?
About the differences between AesManaged and RijndaelManaged:
The AES algorithm is essentially the Rijndael symmetric algorithm with a fixed block size and iteration count. This class functions the same way as the RijndaelManaged class but limits blocks to 128 bits and does not allow feedback modes.
Taken from MSDN, here is the http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aspx

AES Decryption - Porting code from C# to Java

I am trying to port the following code from C# into Java. I have made multiple attempts to try and decrypt my encrypted data and I get gibberish every time. The code below uses the org.bouncycastle library and unfortunately there doesn't seem to be a 1-1 mapping between the C# code and the Java code.
I basically know three things:
byte[] file - This contains my encrypted file. Usually a pretty large array of bytes.
byte[] padding - It is 32*bytes* every time and it seems that the first 16 bytes of this are used as the IV.
byte[] aesKey - It is 32*bytes* every time and I do not know how exactly the C# code is using this array.
Original C# Code
private byte[] decryptmessage(byte[] cmessage, byte[] iVector, byte[] m_Key)
{
{
//// randomly generated number acts as inetialization vector
m_IV = new byte[16];
Array.Copy(iVector, 0, m_IV, 0, 16);
// GenerateAESKey();
KeyParameter aesKeyParam = ParameterUtilities.CreateKeyParameter("AES", m_Key);
ParametersWithIV aesIVKeyParam = new ParametersWithIV(aesKeyParam, m_IV);
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CFB/NoPadding");
cipher.Init(false, aesIVKeyParam);
return cipher.DoFinal(cmessage);
}
}
My attempt in Java
private static byte[] decryptMessage(byte[] file, byte[] iVector, byte[] aesKey) throws Exception {
IvParameterSpec spec = new IvParameterSpec(Arrays.copyOfRange(iVector, 0, 16));
SecretKeySpec key = new SecretKeySpec(Arrays.copyOfRange(aesKey, 0, 16), "AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, spec);
return cipher.doFinal(file);
}
P.S: This is the final step of decryption. Before all this I had to take out some initial set of bytes from my encrypted file and decrypt them using an RSA private key to get this AES key.
If someone has a link / document I can read that properly explains the whole process of using AES to encrypt a file, then using RSA on the key and iv to the begining of the encrypted file, I will be extremely happy. I have just been staring at the C# code, I'd like to see something with pictures.
EDIT: Bytes not bits.
EDIT2: Renamed padding to iVector for consistency and correctness.
In the C# code, you initialize the key with 256 bits (32 bytes) and thus get AES-256. In the Java code, you only use 128 bit (16 bytes) and get AES-128.
So the fix is probably:
SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
You might then find that Java doesn't want to use 256 bit keys (for legal reason). You then have to intall the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6.

Categories

Resources