bcgit / bc-csharp

BouncyCastle.NET Cryptography Library (Mirror)
https://www.bouncycastle.org/csharp
MIT License
1.67k stars 556 forks source link

AES-GCM 128 Encryption & Decryption without tag #126

Closed faliqulamin closed 6 years ago

faliqulamin commented 6 years ago

I use Bouncy Castle to implement DLMS (Protocol for energy metering) and use AES-GCM for the comunication security,

and this is the example of AES-GCM From the documentaion (The example is on red block) : image

so, when i try to encrypt the example with this code :

public static byte[] Encrypt(byte[] Plaintext, byte[] iv, byte[] EK) //success {
var cipher = new GcmBlockCipher(new AesEngine()); var parameters = new AeadParameters(new KeyParameter(EK), 128, iv); cipher.Init(true, parameters); var cipherText = new byte[cipher.GetOutputSize(Plaintext.Length)]; var len = cipher.ProcessBytes(Plaintext, 0, Plaintext.Length, cipherText, 0); cipher.DoFinal(cipherText, len); return cipherText; }

the cipher is : 41-13-12-FF-93-5A-47-56-68-27-C4-67-BC-03-87-BA-79-BD-B9-DF-0E-13-49-AE-2A not like the cipher in the example above (blue box) : 41-13-12-FF-93-5A-47-56-68-27-C4-67- BC

in the output of my code, there are additions byte (TAG) image

So, please help me to encrypt or decrypt without the tag?

thanks for help before and sorry with my english. :)

peterdettman commented 6 years ago

Our implementation wasn't really designed to omit the tag completely. For encryption of course you can just throw away the tag, but for decryption there will be problems.

GCMBlockCipher should work like you want if the macSize (AeadParameters.MacSize) were set to 0, but GCMBlockCipher.Init would throw an exception. I suggest you copy the GCMBlockCipher class and modify the Init method to allow this value to be 0, then use new AeadParameters(..., 0, IV).

If that works for you, we could consider how to make it possible to do this with a subclass instead of needing to copy GCMBlockCipher.

faliqulamin commented 6 years ago

hai @peterdettman , thanks for your suggest. its work.

jimsch commented 6 years ago

Another way to deal with this would be to use Counter (CTR) mode instead of GCM in this case. The two are equal if you ignore all of the authentication code. The only code you would need to play with would be the IV setup.