// Will decrypt .tmp file into Root class public void JBufferDecrypt(APIEncryptedBody data, byte[] secretKey) { if (ReferenceEquals(null, data)) { MessageBox.Show("Please contact [email protected]", "Oh.. There's Bug!"); Application.Exit(); } _IsDecrypting = true; byte[] encryptedBytes = Convert.FromBase64String(data.body); AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); byte[] passwordBytes = secretKey; byte[] salt = Encoding.UTF8.GetBytes(data.author); var key = new Rfc2898DeriveBytes(passwordBytes, salt, 1000); aes.KeySize = 256; //Not Required aes.BlockSize = 128; //Not Required aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.Key = key.GetBytes(aes.KeySize / 8); aes.IV = key.GetBytes(aes.BlockSize / 8); //2314345645678765 // Debug.WriteLine(aes.IV); // Debug.WriteLine(aes.Key); // aes.Key = Encoding.UTF8.GetBytes(secretKey); // aes.IV = Encoding.UTF8.GetBytes("2314345645678765"); //2314345645678765 ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV); byte[] secret = crypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); crypto.Dispose(); var result = ASCIIEncoding.ASCII.GetString(secret); Data = JsonConvert.DeserializeObject <Root>(result); _IsDecrypting = false; }
// Will save from APIs and serialize to a .tmp file public void JFileSave(APIEncryptedBody data, string secretKey) { using (var fs = new StreamWriter(filename)) { try { var js = new JsonSerializer(); js.Serialize(fs, data); fs.Close(); if (File.Exists(filename)) { // JBufferDecrypt(data, secretKey); SrDecryptor(data, secretKey); } } catch (Exception e) { XtraMessageBox.Show("Possible Cause:\n1. Connection unavailable\n2. Update server down\n3. Not running as admin\n\nPlease contact: [email protected]", "There's bug"); if (File.Exists(filename)) { File.Delete(filename); } Application.Exit(); Debug.WriteLine(e); } } }
public void SrDecryptor(APIEncryptedBody data, string secretKey) { _IsDecrypting = true; // GCHandle gch = GCHandle.Alloc(secretKey, GCHandleType.Pinned); JBufferDecrypt(data, Encoding.UTF8.GetBytes(secretKey)); // ZeroMemory(gch.AddrOfPinnedObject(), secretKey.Length * 2); // gch.Free(); _IsDecrypting = false; }
// Get Encrypted Body public async Task <APIEncryptedBody> GetAPIDataEncrypted(string path) { APIEncryptedBody data = null; client.GetAsync(path).ContinueWith(async(Task <HttpResponseMessage> res) => { Debug.WriteLine(res); if (res.Result.IsSuccessStatusCode) { data = await res.Result.Content.ReadAsAsync <APIEncryptedBody>(); } return(data); }).Wait(); return(data); }