废话不多说,直接上代码:
using Aliyun.OSS; // 引入阿里云 OSS SDK using System; using System.IO; using System.Security.Cryptography; // 引入 .NET Framework 中的加密库 using System.Text; public class OSSHelper { /// <summary> /// 将文件上传至 OSS,并使用 DES 算法加密返回的文件 URL /// </summary> /// <param name="accessKeyId">阿里云授权 Access Key Id</param> /// <param name="accessKeySecret">阿里云授权 Access Key Secret</param> /// <param name="endpoint">OSS 服务域名</param> /// <param name="bucketName">Bucket 名称</param> /// <param name="objectKey">Object Key,即在 OSS 上的文件名称</param> /// <param name="filePath">待上传的文件本地路径</param> /// <param name="desKey">DES 密钥</param> /// <param name="desIV">DES 向量</param> /// <returns>加密后的文件 URL</returns> public static string UploadFileToOSS(string accessKeyId, string accessKeySecret, string endpoint, string bucketName, string objectKey, string filePath, byte[] desKey, byte[] desIV) { // 创建 OSS 客户端 var client = new OssClient(endpoint, accessKeyId, accessKeySecret); // 上传文件 using (var fs = new FileStream(filePath, FileMode.Open)) { client.PutObject(bucketName, objectKey, fs); } // 返回加密后的文件路径 return EncryptString($"http://{bucketName}.{endpoint}/{objectKey}", desKey, desIV); } /// <summary> /// 从 OSS 上下载指定的文件,并使用 DES 算法解密文件 URL /// </summary> /// <param name="accessKeyId">阿里云授权 Access Key Id</param> /// <param name="accessKeySecret">阿里云授权 Access Key Secret</param> /// <param name="endpoint">OSS 服务域名</param> /// <param name="bucketName">Bucket 名称</param> /// <param name="objectKey">Object Key,即在 OSS 上的文件名称</param> /// <param name="downloadPath">下载文件保存的本地路径</param> /// <param name="desKey">DES 密钥</param> /// <param name="desIV">DES 向量</param> public static void DownloadFileFromOSS(string accessKeyId, string accessKeySecret, string endpoint, string bucketName, string objectKey, string downloadPath, byte[] desKey, byte[] desIV) { // 创建 OSS 客户端 var client = new OssClient(endpoint, accessKeyId, accessKeySecret); // 下载文件 var obj = client.GetObject(bucketName, objectKey); using (var fs = new FileStream(downloadPath, FileMode.Create)) { int length = 4 * 1024; var buffer = new byte[length]; var bytesRead = obj.Content.Read(buffer, 0, length); while (bytesRead > 0) { fs.Write(buffer, 0, bytesRead); bytesRead = obj.Content.Read(buffer, 0, length); } } // 解密文件路径并输出 var decryptedUrl = DecryptString($"http://{bucketName}.{endpoint}/{objectKey}", desKey, desIV); Console.WriteLine(decryptedUrl); } /// <summary> /// 使用 DES 算法加密字符串 /// </summary> /// <param name="plainText">待加密的字符串</param> /// <param name="key">DES 密钥(长度为 8)</param> /// <param name="iv">DES 向量(长度为 8)</param> /// <returns>加密后的字符串</returns> public static string EncryptString(string plainText, byte[] key, byte[] iv) { byte[] encrypted; using (var des = DES.Create()) { des.Key = key; des.IV = iv; var inputBuffer = Encoding.UTF8.GetBytes(plainText); encrypted = des.CreateEncryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length); } return Convert.ToBase64String(encrypted); } /// <summary> /// 使用 DES 算法解密字符串 /// </summary> /// <param name="cipherText">待解密的字符串</param> /// <param name="key">DES 密钥(长度为 8)</param> /// <param name="iv">DES 向量(长度为 8)</param> /// <returns>解密后的字符串</returns> public static string DecryptString(string cipherText, byte[] key, byte[] iv) { byte[] decrypted; using (var des = DES.Create()) { des.Key = key; des.IV = iv; var inputBuffer = Convert.FromBase64String(cipherText); decrypted = des.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length); } return Encoding.UTF8.GetString(decrypted); } } // 在完成了上述代码之后,我们可以按照以下方式调用这些方法: //// 定义加密所使用的 DES 密钥和向量 //byte[] desKey = Encoding.ASCII.GetBytes("01234567"); // byte[] desIV = Encoding.ASCII.GetBytes("abcdefgh"); // // 调用上传文件至 OSS 并加密返回 URL 的方法 // string encryptedUrl = OSSHelper.UploadFileToOSS("your-access-key-id", "your-access-key-secret", "your-endpoint", "your-bucket-name", "test.txt", "C:\\test.txt", desKey, desIV); // Console.WriteLine(encryptedUrl); //// 调用从 OSS 下载文件并解密 URL 的方法 //OSSHelper.DownloadFileFromOSS("your-access-key-id", "your-access-key-secret", "your-endpoint", "your-bucket-name", "test.txt", "C:\\download.txt", desKey, desIV);View Code
标签:文件,string,C#,OSS,DES,var,using,byte From: https://www.cnblogs.com/wxsdyz/p/17341163.html