服务端加密(SSE-C)
SSE-C即Server Side Encryption with Custom key,由服务端提供对象的加密功能,用户只需要在执行操作的时候带上加密密钥,由服务端来执行加密和解密操作。
服务端使用AES256加密算法,加密密钥由客户端提供,使用32位加密密钥
func RandString(len int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
bytes := make([]byte, len)
for i := 0; i < len; i++ {
b := r.Intn(26) + 65
bytes[i] = byte(b)
}
return string(bytes)
}
// customKey: RandString(32)
func (p *S3EncryptDemo) putObject() {
var key = "ExampleEnc.txt"
out, err := p.svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(p.bucket),
Key: aws.String(key),
Body: strings.NewReader("1234"),
SSECustomerKey: aws.String(p.customKey),
SSECustomerAlgorithm: aws.String("AES256"),
})
if err != nil {
fmt.Println("err, ", err)
return
}
fmt.Println("success put, ", out)
}
func (p *S3EncryptDemo) getObject() {
key := "ExampleEnc.txt"
out, err := p.svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(p.bucket),
Key: aws.String(key),
SSECustomerKey: aws.String(p.customKey),
SSECustomerAlgorithm: aws.String("AES256"),
})
if err != nil {
fmt.Println("err, ", err)
return
}
body, _ := ioutil.ReadAll(out.Body)
fmt.Println("success, ", string(body))
}