C# 在.Net中为AWS s3生成预签名Url

C# 在.Net中为AWS s3生成预签名Url,c#,rest,amazon-s3,pre-signed-url,C#,Rest,Amazon S3,Pre Signed Url,根据提供的示例数据,应该可以生成值为的签名密钥: aeeed9bbccd4d02ee5c0109b86d86835f995330da4c265957d157751f604d404 以下是我在.Net中的代码: 使用系统; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq; 使用System.Security.Cryptography; 使用系统文本; 命名空间播放亚马逊3 { 公共类ReadTextFilePerRest { 专用

根据提供的示例数据,应该可以生成值为的签名密钥:

aeeed9bbccd4d02ee5c0109b86d86835f995330da4c265957d157751f604d404
以下是我在.Net中的代码:

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Security.Cryptography;
使用系统文本;
命名空间播放亚马逊3
{
公共类ReadTextFilePerRest
{
专用字符串_regionSample=“us-east-1”;
私有字符串_dateSample=“20130524”;
私有字符串_secretAccessKeySample=“wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY”;
私有字符串\u canonicalRequestPath=“..\\Files\\SampleFiles\\CanonicalRequest.txt”;
私有字符串\u stringToSignPath=“..\\Files\\SampleFiles\\StringToSign.txt”;
私有字符串_canonicalRequest;
私有字符串_stringToSign;
public void ReadPayloadFiles()
{
_stringToSign=File.ReadAllText(_stringToSignPath);
_canonicalRequest=File.ReadAllText(_canonicalRequestPath);
}
//它需要返回:aeeed9bbccd4d02ee5c0109b86d86835f995330da4c265957d157751f604d404
公共字符串签名密钥()
{
var keyBytes=Encoding.ASCII.GetBytes(“AWS4”+_secretAccessKeySample);
var dateBytes=Encoding.ASCII.GetBytes(_dateSample);
var regionBytes=Encoding.ASCII.GetBytes(_regionSample);
var serviceBytes=Encoding.ASCII.GetBytes(“s3”);
var requestBytes=Encoding.ASCII.GetBytes(“aws4_请求”);
var stringToSignBytes=Encoding.ASCII.GetBytes(_stringToSign);
使用(HMACSHA256 hmac=新的HMACSHA256(日期字节))
{
var dateKey=hmac.ComputeHash(keyBytes);
使用(HMACSHA256 hmac2=新的HMACSHA256(regionBytes))
{
var dateRegionKey=hmac2.ComputeHash(dateKey);
使用(HMACSHA256 hmac3=新的HMACSHA256(serviceBytes))
{
var dateRegionServiceKey=hmac3.ComputeHash(dateRegionKey);
使用(HMACSHA256 hmac4=新的HMACSHA256(请求字节))
{
var signingKey=hmac4.ComputeHash(dateRegionServiceKey);
使用(HMACSHA256 hmac5=新的HMACSHA256(stringToSignBytes))
{
var signature=hmac5.ComputeHash(signingKey);
通过传真(签名)返回;
}
}
}
}
}
}
私有字符串ByteToString(IEnumerable缓冲区)
{
var sBinary=buffer.Aggregate(“,(current,buff)=>current+buff.ToString(“X2”);
返回一年;
}
}
}
但是,我生成的签名密钥不同。有人能告诉我哪里出了错吗?

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Security.Cryptography;
使用系统文本;
命名空间播放亚马逊3
{
/// 
///此类仅负责计算用于创建预签名Url的最终签名
///通过REST与iam服务进行沟通
/// 
公共类预签名重新签名
{
private const string RegionSample=“us-east-1”;
private const string DateSample=“20150830”;
私有常量字符串ServiceSample=“iam”;
//AWS提供了一个示例
private const string SecretAccessKeySample=“wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY”;
/// 
///此方法将在main中调用
/// 
public void ExecuteMethod()
{
var finalSignature=SigningKey();
控制台。书面(“最终签名:+最终签名”);
}
私有字符串签名密钥()
{
//生成派生签名密钥
var衍生签名密钥=
GetSignatureKey(SecretAccessKeySample、DateSample、RegionSample、ServiceSample);
//aws为测试提供的示例签名密钥
var stringToSign=“AWS4-HMAC-SHA256”+“\n”+
“20150830T123600Z”+“\n”+
“20150830/us-east-1/iam/aws4\U请求”+“\n”+
“F536975D06C0309214F805BB90CCFF089219ECD68B257EFEF23EDD43B7E1A59”;
//生成最终签名
var签名=HmacSha256(stringToSign,derivedSigningKey);
//返回最终签名的十六进制值
通过传真(签名)返回;
}
/// 
///在.Net中计算hmac-sha256
/// 
/// 
/// 
/// 
专用字节[]HmacSha256(字符串数据,字节[]键)
{
常量字符串算法=“HmacSHA256”;
var kha=KeyedHashAlgorithm.Create(算法);
kha.Key=Key;
返回kha.ComputeHash(Encoding.UTF8.GetBytes(data));
}
/// 
///从提供的信息中获取派生签名密钥(不是最终签名)
/// 
/// 
/// 
/// 
/// 
/// 
私有字节[]GetSignatureKey(字符串键、字符串日期戳、字符串区域名、字符串服务名)
{
var kSecret=Encoding.UTF8.GetBytes((“AWS4”+key.tocharray());
var kDate=HmacSha256(日期戳,k秘密);
var kRegion=HmacSha256(区域名称,kDate);
var kService=HmacSha256(serviceName,kRegion);
var kSigning=HmacSha256(“aws4_请求”,kService);
返回kSigning;
}
/// 
///它返回字节[]的十六进制值
/// 
/// 
/// 
私有字符串ByteToString(IEnumerable缓冲区)
{
var sBinary=buffer.Aggregate(“”)