How to decrypt encrypted AES / CCM encrypted text using Bouncy Castle?

Encryption

Encryption is performed using the Crypto Library at Stanford Javascript (SJCL). The following is a complete encryption example, divided into two parts. The first is password-based password output with PBKDF2 . In the second part, the actual encryption takes place with the derivative key and the initialization vector (IV). Note that salt and IV are hardcoded, so it is easier to provide a solution for decrypting C #.

// Key derivation… var password = "password"; var salt = sjcl.codec.hex.toBits( "5f9bcef98873d06a" // Random generated with sjcl.random.randomWords(2, 0); ); // Hex encoded with sjcl.codec.hex.toBits(randomSalt); var iterations = 1000; var keySize = 128; var encryptionKey = sjcl.misc.pbkdf2(password, salt, iterations, keySize); // Encryption… var blockCipher = new sjcl.cipher.aes(encryptionKey); var plainText = sjcl.codec.utf8String.toBits("secret"); var iv = sjcl.codec.hex.toBits("8291ff107e798a29"); var adata = ""; // What is adata? var tag = 64; // What is tag? I think it is authentication strength. var cipherText = sjcl.mode.ccm.encrypt(blockCipher, plainText, iv, adata, tag); 

The value of the encryptionKey variable:

  • SJCL Bitmap: [ -74545279, -553931361, -1590906567, 1562838103 ]
  • Hex encoded: fb8e8781defbad9fa12cb1395d270457
  • Base64 encoded: +46Hgd77rZ+hLLE5XScEVw==

The value of the variable iv :

  • SJCL bit array: [ -2104361200, 2121894441 ]
  • Hexadecimal code: 8291ff107e798a29
  • Base64 encoded: gpH/EH55iik=

The value of the cipherText variable:

  • SJCL bit array: [ 1789401157, -1485204800, -440319203, 17593459146752 ]
  • Hex encoded: 6aa81845a77992c0e5c1431d4be2
  • Base64 encoded: aqgYRad5ksDlwUMdS+I=

Question

The question arises:

How can I decrypt text using Bouncy Castle ?


An example of working decryption after using jbtule below

 using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Parameters; namespace SjclHelpers { public static class Encryption { /// <summary>Decrypts the cipher text.</summary> /// <param name="cipherText">The cipher text.</pararesm> /// <param name="key">The encryption key.</param> /// <param name="initializationVector">The IV.</param> /// <returns>The decrypted text.</returns> public static byte[] Decrypt(this byte[] cipherText, byte[] key, byte[] initializationVector) { var keyParameter = new KeyParameter(key); const int macSize = 64; var nonce = initializationVector; var associatedText = new byte [] {}; var ccmParameters = new CcmParameters( keyParameter, macSize, nonce, associatedText); var ccmMode = new CcmBlockCipher(new AesFastEngine()); var forEncryption = false; ccmMode.Init(forEncryption, ccmParameters); var plainBytes = new byte[ccmMode.GetOutputSize(cipherText.Length)]; var res = ccmMode.ProcessBytes( cipherText, 0, cipherText.Length, plainBytes, 0); ccmMode.DoFinal(plainBytes, res); return plainBytes; }}} 

I get a System.ArgumentException . I think he complains that one of the byte arrays is short.

Boncy Castle is available on the NuGet website at this location: http://nuget.org/packages/BouncyCastle .


ABOUT

The AES / CCM decryption solution will be part of the CodePlex SjclHelpers project and will be released as a NuGet package.

+8
c # encryption aes bouncycastle
source share
2 answers

From what I see:

  • Nonce should be IV.
  • Typically, you use AeadParameters instead of CcmParameters , but it might be all right, probably don't wrap it with parametersWithIV
  • associateText is optional because CCM can authenticate unencrypted data associated with your encrypted data if you need it. You probably need an argument, since it should be the same as sjcl adata , and the transport method can be anything.
  • It is macSize that tag and macSize same.
  • DoFinal should be ccmMode.DoFinal(plainBytes, res);
  • For security after decryption, you should compare the last ( macSize / 8 ) bytes of cipherText with ccmMode.GetMac() to verify the authenticity.
  • var plainBytes = new byte[ccmMode.GetOutputSize(cipherText.Length)]
+2
source share

You cannot decrypt sjcl JSON with Bouncy Castle. Because SJCL pre-computed table is different from Bouncy Castle. I created my own library. If you are still looking for a decryption solution, give it a try. https://github.com/mebius1080p/SJCLDecryptor

0
source share

All Articles