示例#1
0
        /// <summary>
        /// Encrypts a stream with a random symmetric key
        /// </summary>
        /// <param name="unencryptedStream">stream to encrypt</param>
        /// <param name="publicKeyModulus">modulus of the RSA key to encrypt the symmetric key with</param>
        /// <param name="publicKeyExponent">exponent of the RSA key to encrypt the symmetric key with</param>
        /// <param name="symmetricEncryptionParameters">returns encrypted symmetric key and IV</param>
        /// <returns>encrypted stream</returns>
        private Stream EncryptStream(Stream unencryptedStream, string publicKeyModulus, string publicKeyExponent,
                                     out SymmetricEncryptionParameters symmetricEncryptionParameters)
        {
            // generate a symmetric key
            var aesProvider = new RijndaelManaged();

            byte[] encodedSymmetricKey = Encoding.ASCII.GetBytes(Convert.ToBase64String(aesProvider.Key));
            byte[] encodedSymmetricIV  = Encoding.ASCII.GetBytes(Convert.ToBase64String(aesProvider.IV));

            // encrypt the stream
            var encryptedStream = new MemoryStream();

            using (var aesEncryptor = aesProvider.CreateEncryptor())
                using (var base64Enc = new CryptoStream(unencryptedStream, new ToBase64Transform(), CryptoStreamMode.Read))
                    using (var encrypted = new CryptoStream(base64Enc, aesEncryptor, CryptoStreamMode.Read))
                    {
                        encrypted.CopyTo(encryptedStream);
                        encryptedStream.Position = 0;
                    }

            // create an RSA key
            var parameters = new RSAParameters
            {
                Modulus  = Util.DecodeHexString(publicKeyModulus),
                Exponent = Util.DecodeHexString(publicKeyExponent)
            };

            var encryptionProvider = new RSACryptoServiceProvider();

            encryptionProvider.ImportParameters(parameters);

            // encrypt the symmetric key with the RSA key
            byte[] encryptedSymmetricKey = encryptionProvider.Encrypt(encodedSymmetricKey, false);
            byte[] encryptedSymmetricIV  = encryptionProvider.Encrypt(encodedSymmetricIV, false);

            symmetricEncryptionParameters = new SymmetricEncryptionParameters
            {
                Key = BitConverter.ToString(encryptedSymmetricKey).Replace("-", ""),
                IV  = BitConverter.ToString(encryptedSymmetricIV).Replace("-", "")
            };

            return(encryptedStream);
        }
示例#2
0
        /// <summary>
        /// Submits information about the uploaded document to the API
        /// </summary>
        private void UpdateDocumentStatusAfterFileUpload(int documentId, SymmetricEncryptionParameters encryptionParameters,
                                                         string fileName, string fileType, int fileSize,
                                                         PrinterSettings finishingOptions, OAuthToken userAccessToken)
        {
            using (var http = new HttpClient())
            {
                var message = new DocumentStatusMessage
                {
                    IsSuccess            = true,
                    Status               = DocumentStatus.ClientUpload,
                    ClientUploadDocument = new FileUploadMessage
                    {
                        EncryptedSymmetricKey = encryptionParameters.Key,
                        EncryptedSymmetricIV  = encryptionParameters.IV,
                        File = new FileInfoMessage
                        {
                            FriendlyName = fileName,
                            FileType     = fileType,
                            FileSize     = fileSize
                        },
                        PrintOptions = finishingOptions
                    }
                };

                var endpoint       = ApiEndpoints.UpdateDocumentStatus.Replace("{document_id}", documentId.ToString(CultureInfo.InvariantCulture));
                var requestContent = new StringContent(JsonConvert.SerializeObject(message), Encoding.UTF8, "application/json");

                SignOAuthRequest(http, userAccessToken, HttpMethod.Post, endpoint);
                var response = http.PostAsync(_apiUri.AbsoluteUri + endpoint, requestContent).Result;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new BreezyApiException(
                              String.Format("Could not update document status.\r\nResponse status code: {0}.\r\nResponse: {1}",
                                            response.StatusCode, response.Content.ReadAsStringAsync().Result));
                }
            }
        }