private string GenerateSignature(string queryStringToSign, string partialUrl) { string signature = ""; StringBuilder string_to_sign = new StringBuilder(); string_to_sign.Append(this.pubnubConfig.SubscribeKey).Append("\n").Append(this.pubnubConfig.PublishKey).Append("\n"); string_to_sign.Append(partialUrl).Append("\n"); string_to_sign.Append(queryStringToSign); PubnubCrypto pubnubCrypto = new PubnubCrypto(this.pubnubConfig.CipherKey, this.pubnubConfig, this.pubnubLog); signature = pubnubCrypto.PubnubAccessManagerSign(this.pubnubConfig.SecretKey, string_to_sign.ToString()); System.Diagnostics.Debug.WriteLine("string_to_sign = " + string_to_sign.ToString()); System.Diagnostics.Debug.WriteLine("signature = " + signature); return(signature); }
public string Encrypt(string inputString) { if (string.IsNullOrEmpty(inputString)) { throw new ArgumentException("inputString is not valid"); } if (pubnubConfig == null || string.IsNullOrEmpty(pubnubConfig.CipherKey)) { throw new MissingMemberException("CipherKey missing"); } PubnubCrypto pc = new PubnubCrypto(pubnubConfig.CipherKey); return(pc.Encrypt(inputString)); }
public byte[] DecryptFile(byte[] inputBytes) { if (inputBytes == null) { throw new ArgumentException("inputBytes is not valid"); } if (pubnubConfig == null || string.IsNullOrEmpty(pubnubConfig.CipherKey)) { throw new ArgumentException("CipherKey missing"); } PubnubCrypto pc = new PubnubCrypto(pubnubConfig.CipherKey); return(pc.Decrypt(inputBytes, true)); }
public List <object> DecodeDecryptLoop <T>(List <object> message, string[] channels, string[] channelGroups, PNCallback <T> errorCallback) { List <object> returnMessage = new List <object>(); if (config.CipherKey.Length > 0) { PubnubCrypto aes = new PubnubCrypto(config.CipherKey, config, pubnubLog); object[] myObjectArray = (from item in message select item as object).ToArray(); object[] enumerable = myObjectArray[0] as object[]; if (enumerable != null) { List <object> receivedMsg = new List <object>(); foreach (object element in enumerable) { string decryptMessage = ""; try { Dictionary <string, object> historyEnv = jsonLib.ConvertToDictionaryObject(element); if (historyEnv != null && historyEnv.ContainsKey("message")) { string dictionaryValue = aes.Decrypt(historyEnv["message"].ToString()); historyEnv["message"] = jsonLib.DeserializeToObject(dictionaryValue); decryptMessage = jsonLib.SerializeToJsonString(historyEnv); } else { decryptMessage = aes.Decrypt(element.ToString()); } } catch (Exception ex) { decryptMessage = "**DECRYPT ERROR**"; PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(ex); PNStatus status = new StatusBuilder(config, jsonLib).CreateStatusResponse <T>(PNOperationType.PNHistoryOperation, category, null, (int)HttpStatusCode.NotFound, new PNException(ex)); if (channels != null && channels.Length > 0) { status.AffectedChannels.AddRange(channels); } if (channelGroups != null && channelGroups.Length > 0) { status.AffectedChannelGroups.AddRange(channelGroups); } errorCallback.OnResponse(default(T), status); } object decodeMessage = (decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : jsonLib.DeserializeToObject(decryptMessage); receivedMsg.Add(decodeMessage); } returnMessage.Add(receivedMsg); } for (int index = 1; index < myObjectArray.Length; index++) { returnMessage.Add(myObjectArray[index]); } return(returnMessage); } else { var myObjectArray = (from item in message select item as object).ToArray(); IEnumerable enumerable = myObjectArray[0] as IEnumerable; if (enumerable != null) { List <object> receivedMessage = new List <object>(); foreach (object element in enumerable) { receivedMessage.Add(element); } returnMessage.Add(receivedMessage); } for (int index = 1; index < myObjectArray.Length; index++) { returnMessage.Add(myObjectArray[index]); } return(returnMessage); } }
public List <object> FetchHistoryDecodeDecryptLoop <T>(PNOperationType type, Dictionary <string, object> messageContainer, string[] channels, string[] channelGroups, PNCallback <T> errorCallback) { List <object> returnMessage = new List <object>(); Dictionary <string, List <object> > dicMessage = new Dictionary <string, List <object> >(); foreach (KeyValuePair <string, object> kvp in messageContainer) { List <object> currentVal = kvp.Value as List <object>; if (currentVal != null) { object[] currentValArray = jsonLib.ConvertToObjectArray(currentVal); List <object> decryptList = (currentValArray != null && currentValArray.Length > 0) ? new List <object>() : null; if (currentValArray != null && decryptList != null) { foreach (object currentObj in currentValArray) { Dictionary <string, object> dicValue = jsonLib.ConvertToDictionaryObject(currentObj); if (dicValue != null && dicValue.Count > 0 && dicValue.ContainsKey("message")) { Dictionary <string, object> dicDecrypt = new Dictionary <string, object>(); foreach (KeyValuePair <string, object> kvpValue in dicValue) { if (kvpValue.Key == "message" && config.CipherKey.Length > 0) { PubnubCrypto aes = new PubnubCrypto(config.CipherKey, config, pubnubLog); string decryptMessage = ""; try { decryptMessage = aes.Decrypt(kvpValue.Value.ToString()); } catch (Exception ex) { #region "Exception" decryptMessage = "**DECRYPT ERROR**"; PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(ex); PNStatus status = new StatusBuilder(config, jsonLib).CreateStatusResponse <T>(type, category, null, (int)HttpStatusCode.NotFound, new PNException(ex)); if (channels != null && channels.Length > 0) { status.AffectedChannels.AddRange(channels); } if (channelGroups != null && channelGroups.Length > 0) { status.AffectedChannelGroups.AddRange(channelGroups); } errorCallback.OnResponse(default(T), status); #endregion } object decodeMessage = (decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : jsonLib.DeserializeToObject(decryptMessage); dicDecrypt.Add(kvpValue.Key, decodeMessage); } else { dicDecrypt.Add(kvpValue.Key, kvpValue.Value); } } decryptList.Add(dicDecrypt); } } } dicMessage.Add(kvp.Key, decryptList); } } if (dicMessage.Count > 0) { returnMessage.Add(dicMessage); } return(returnMessage); }