private LinkedList <string> Upload(KidsNoteContent content, string dateFolderId, bool encrypt) { string folderName = string.Format("[{0}] {1}", content.Type, content.Id); string containingFolderId = CreateFolder(dateFolderId, folderName); MemoryStream ms = null; if (encrypt) { byte[] plain = Encoding.UTF8.GetBytes(content.FormatContent()); byte[] encrypted = new byte[plain.Length]; EncryptorChaCha chacha = new EncryptorChaCha(true, EncryptorChaCha.DefaultChaChaEncKey, EncryptorChaCha.DefaultChaChaEncNonce); chacha.Process(plain, 0, plain.Length, encrypted, 0); ms = new MemoryStream(encrypted); } else { ms = new MemoryStream(Encoding.UTF8.GetBytes(content.FormatContent())); } ms.Seek(0, SeekOrigin.Begin); LinkedList <string> idList = new LinkedList <string>(); UploadProgress(string.Format("Uploding [{0}] {1}", content.Type, content.Id)); string name = encrypt ? "본문.txt.chacha" : "본문.txt"; // 이미 암호화 해 두었다. string bodyId = UploadFile(ms, containingFolderId, name, name, Constants.GOOGLE_DRIVE_MIMETYPE_TEXT); idList.AddLast(bodyId); int i = 0; foreach (var each in content.Attachments) { ++i; int pos = each.DownloadUrl.LastIndexOf('.'); string ext = ""; if (pos > 0) { ext = each.DownloadUrl.Substring(pos + 1); ext = ext.ToLower(); } bool isVideo = each.Type == AttachmentType.VIDEO; string id = isVideo ? UploadVideoAttachment(each, i, containingFolderId, ext, encrypt) : UploadPhotoAttachment(each, i, containingFolderId, ext, encrypt); if (id != null && id.Length > 0) { idList.AddLast(id); } } return(idList); }
private void buttonTestChaCha_Click(object sender, RoutedEventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == true) { FileStream inStream = new FileStream(dialog.FileName, FileMode.Open); string encFile = dialog.FileName + ".enc"; string decFile = dialog.FileName + ".dec"; FileStream outStreamEnc = new FileStream(encFile, FileMode.Create); EncryptorChaCha chacha = new EncryptorChaCha(true, EncryptorChaCha.DefaultChaChaEncKey, EncryptorChaCha.DefaultChaChaEncNonce); byte[] readBuffer = new byte[4096]; byte[] outBuffer = new byte[readBuffer.Length]; int nRead = inStream.Read(readBuffer, 0, readBuffer.Length); int offset = 0; while (nRead > 0) { if (offset == 0) { outStreamEnc.Write(chacha.Nonce, 0, chacha.Nonce.Length); } offset += nRead; chacha.Process(readBuffer, 0, nRead, outBuffer, 0); outStreamEnc.Write(outBuffer, 0, nRead); nRead = inStream.Read(readBuffer, 0, readBuffer.Length); } inStream.Close(); outStreamEnc.Close(); byte[] nonce = new byte[EncryptorChaCha.DefaultChaChaEncNonce.Length]; chacha = new EncryptorChaCha(false, EncryptorChaCha.DefaultChaChaEncKey, nonce); inStream = new FileStream(encFile, FileMode.Open); FileStream outStreamDec = new FileStream(decFile, FileMode.Create); nRead = inStream.Read(nonce, 0, nonce.Length); if (nRead > 0) { nRead = inStream.Read(readBuffer, 0, readBuffer.Length); while (nRead > 0) { chacha.Process(readBuffer, 0, nRead, outBuffer, 0); outStreamDec.Write(outBuffer, 0, nRead); nRead = inStream.Read(readBuffer, 0, readBuffer.Length); } } inStream.Close(); outStreamDec.Close(); } }
private string UploadPhotoAttachment(KidsNoteContent.Attachment attach, int index, string parentId, string ext, bool encrypt) { attach.Data.Seek(0, SeekOrigin.Begin); MemoryStream encryptedStream = null; if (encrypt) { EncryptorChaCha chacha = new EncryptorChaCha(true, EncryptorChaCha.DefaultChaChaEncKey, EncryptorChaCha.DefaultChaChaEncNonce); encryptedStream = new MemoryStream(); byte[] readBuffer = new byte[4096]; byte[] encBuffer = new byte[readBuffer.Length]; int nRead = attach.Data.Read(readBuffer, 0, readBuffer.Length); while (nRead > 0) { chacha.Process(readBuffer, 0, nRead, encBuffer, 0); encryptedStream.Write(encBuffer, 0, nRead); nRead = attach.Data.Read(readBuffer, 0, readBuffer.Length); } encryptedStream.Seek(0, SeekOrigin.Begin); } string mType = MimeType.get(ext); string attachName = string.Format("{0}_{1}", index.ToString("000"), attach.Name); string message = String.Format("Uploading attachment... {0}", attachName); UploadProgress(message); Stream toUpload = encrypt ? encryptedStream : attach.Data; string suffix = encrypt ? ".chacha" : ""; string id = ""; try { id = UploadFile(toUpload, parentId, attachName + suffix, attach.Name + suffix, mType); } catch (Exception) { id = ""; } return(id); }
private void DecryptFolderImpl(string folder) { string[] files = Directory.GetFiles(folder); foreach (var file in files) { int pos = file.IndexOf(".chacha"); if (pos < 0) { continue; } string outFile = file.Substring(0, pos); EncryptorChaCha chacha = new EncryptorChaCha(false, EncryptorChaCha.DefaultChaChaEncKey, EncryptorChaCha.DefaultChaChaEncNonce); byte[] readBuffer = new byte[1024 * 16]; byte[] writeBuffer = new byte[readBuffer.Length]; FileStream inStream = new FileStream(file, FileMode.Open); FileStream outStream = new FileStream(outFile, FileMode.Create); int nBytes = inStream.Read(readBuffer, 0, readBuffer.Length); while (nBytes > 0) { chacha.Process(readBuffer, 0, nBytes, writeBuffer, 0); outStream.Write(writeBuffer, 0, nBytes); nBytes = inStream.Read(readBuffer, 0, readBuffer.Length); } inStream.Close(); outStream.Close(); } string[] directories = Directory.GetDirectories(folder); foreach (var dir in directories) { DecryptFolderImpl(dir); } }