private static void doDecrypt(EncryptedContent content, Blob ckBits, DecryptorV2.DecryptSuccessCallback onSuccess_0, EncryptError.OnError onError_1) { if (!content.hasInitialVector()) { onError_1.onError( net.named_data.jndn.encrypt.EncryptError.ErrorCode.MissingRequiredInitialVector, "Expecting Initial Vector in the encrypted content, but it is not present"); return; } Blob plainData; try { Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(javax.crypto.Cipher.DECRYPT_MODE, new SecretKeySpec(ckBits.getImmutableArray(), "AES"), new IvParameterSpec(content.getInitialVector() .getImmutableArray())); plainData = new Blob(cipher.doFinal(content.getPayload() .getImmutableArray()), false); } catch (Exception ex) { onError_1.onError(net.named_data.jndn.encrypt.EncryptError.ErrorCode.DecryptionFailure, "Decryption error in doDecrypt: " + ex); return; } try { onSuccess_0.onSuccess(plainData); } catch (Exception exception) { logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onSuccess", exception); } }
/// <summary> /// Asynchronously decrypt the encryptedContent. /// </summary> /// /// <param name="encryptedContent">the EncryptedContent object. If you may change it later, then pass in a copy of the object.</param> /// <param name="onSuccess">NOTE: The library will log any exceptions thrown by this callback, but for better error handling the callback should catch and properly handle any exceptions.</param> /// <param name="onError_0">error string. NOTE: The library will log any exceptions thrown by this callback, but for better error handling the callback should catch and properly handle any exceptions.</param> public void decrypt(EncryptedContent encryptedContent, DecryptorV2.DecryptSuccessCallback onSuccess, EncryptError.OnError onError_0) { if (encryptedContent.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.KEYNAME) { logger_.log(ILOG.J2CsMapping.Util.Logging.Level.INFO, "Missing required KeyLocator in the supplied EncryptedContent block"); onError_0.onError(net.named_data.jndn.encrypt.EncryptError.ErrorCode.MissingRequiredKeyLocator, "Missing required KeyLocator in the supplied EncryptedContent block"); return; } if (!encryptedContent.hasInitialVector()) { logger_.log(ILOG.J2CsMapping.Util.Logging.Level.INFO, "Missing required initial vector in the supplied EncryptedContent block"); onError_0.onError( net.named_data.jndn.encrypt.EncryptError.ErrorCode.MissingRequiredInitialVector, "Missing required initial vector in the supplied EncryptedContent block"); return; } Name ckName_1 = encryptedContent.getKeyLocatorName(); DecryptorV2.ContentKey contentKey_2 = ILOG.J2CsMapping.Collections.Collections.Get(contentKeys_, ckName_1); bool isNew = (contentKey_2 == null); if (isNew) { contentKey_2 = new DecryptorV2.ContentKey(); ILOG.J2CsMapping.Collections.Collections.Put(contentKeys_, ckName_1, contentKey_2); } if (contentKey_2.isRetrieved) { doDecrypt(encryptedContent, contentKey_2.bits, onSuccess, onError_0); } else { logger_.log( ILOG.J2CsMapping.Util.Logging.Level.INFO, "CK {0} not yet available, so adding to the pending decrypt queue", ckName_1); ILOG.J2CsMapping.Collections.Collections.Add(contentKey_2.pendingDecrypts, new ContentKey.PendingDecrypt( encryptedContent, onSuccess, onError_0)); } if (isNew) { fetchCk(ckName_1, contentKey_2, onError_0, net.named_data.jndn.encrypt.EncryptorV2.N_RETRIES); } }