2

报告文档 api GetReportDocument api

"response": {
              "payload": {
                "reportDocumentId": "0356cf79-b8b0-4226-b4b9-0ee058ea5760",
                "url": "https://d34o8swod1owfl.cloudfront.net/SampleResult%2BKey%3DSample%2BINITVEC%3D58+fa+bf+a7+08+11+95+0f+c1+a8+c6+e0+d5+6f+ae+c8",
                "encryptionDetails": {
                  "standard": "AES",
                  "initializationVector": "58 fa bf a7 08 11 95 0f c1 a8 c6 e0 d5 6f ae c8",
                  "key": "Sample"
                }
              }
            }

当我直接访问这个网址 "url": "https://d34o8swod1owfl.cloudfront.net/SampleResult%2BKey%3DSample%2BINITVEC%3D58+fa+bf+a7+08+11+95+0f+c1+a8+c6+e0+d5+6f+ae+c8"时,自动下载文件,我应该用AES解密文件吗?初始化向量是用Base64编码的?如何获取报告?</p>

4

2 回答 2

2

通过 url 下载文件后(你只有 5 分钟的时间),你必须解密它。因此,按字节(二进制)下载它并使用密码模式 CBC 使用 AES(最初称为 Rijndael)对其进行解密。如您所想,密钥和初始化向量是 base 64 编码的。她是 C# 中的一些示例代码:

GetReportDocumentResponse requestResult = ... //your request to getreportdocument here
byte[] rawData = new System.Net.WebClient().DownloadData(requestResult.payload.url);
byte[] key = Convert.FromBase64String(requestResult.payload.encryptionDetails.key);
byte[] iv = Convert.FromBase64String(requestResult.payload.encryptionDetails.initializationVector);

string documentText; // this will contain the decrypted document
using (var rijndaelManaged = new RijndaelManaged { Key = key, IV = iv, Mode = CipherMode.CBC })
using (var memoryStream = new MemoryStream(rawData))
using (var cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateDecryptor(key, iv), CryptoStreamMode.Read))
{
    documentText = new StreamReader(cryptoStream).ReadToEnd();
}
于 2021-03-21T18:10:26.220 回答
2

//这是我的工作代码
++++++++++++++++++++++++++++++

response3 = reportsApi.GetReportDocument("Give_your_ReportDocumentId");
            
byte[] rawData = new System.Net.WebClient().DownloadData(response3.Payload.Url);
byte[] key = Convert.FromBase64String(response3.Payload.EncryptionDetails.Key);
byte[] iv = Convert.FromBase64String(response3.Payload.EncryptionDetails.InitializationVector);

var reportData = ReportsApiTests.DecryptString(key, iv, rawData);
File.WriteAllText(report_type + ".txt", reportData);


public static string DecryptString(byte[] key, byte[] iv, byte[] cipherText)
{
    byte[] buffer = cipherText;

    using (Aes aes = Aes.Create())
    {
        aes.Key = key;
        aes.IV = iv;
        ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

        using (MemoryStream memoryStream = new MemoryStream(buffer))
        {
            using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }
    }
    
}
于 2021-08-07T21:17:29.780 回答