婉兮清扬

案上诗书杯中酒之快意人生

Getting Started with AWS SDK for Java (4)

发表时间:2016-07-29 07:46:25
The following is an example of using the AWS SimpleDB service along with AWS KMS. Since SimpleDB does not natively integrates with KMS, we will have to encrypt the data before storing it to SimpleDB, and decrypt the data after retrieving it from SimpleDB.



import java.nio.*;
import java.util.*;
import java.nio.charset.*;

import com.amazonaws.regions.*;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.simpledb.*;
import com.amazonaws.services.simpledb.model.*;
import com.amazonaws.services.kms.*;
import com.amazonaws.services.kms.model.*;


public class SDB
{

public AmazonSimpleDBClient client;
public AWSKMSClient kms;

public String keyId = "arn:aws:kms:ap-southeast-2:[aws-account-id]:key/[aws-kms-key-very-long-id-ere]";
public static Charset charset = Charset.forName("ASCII");
public static CharsetEncoder encoder = charset.newEncoder();
public static CharsetDecoder decoder = charset.newDecoder();

public SDB()
{
client = new AmazonSimpleDBClient();
client.configureRegion(Regions.AP_SOUTHEAST_2);

kms = new AWSKMSClient();
kms.configureRegion(Regions.AP_SOUTHEAST_2);

}


public void createDomain(String domain)
{
try
{
CreateDomainRequest request = new CreateDomainRequest(domain);
client.createDomain(request);
} catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}

public void deleteAttribute(String domain, String item)
{
try
{
DeleteAttributesRequest request = new DeleteAttributesRequest(domain, item);
client.deleteAttributes(request);
} catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}

public void putAttribute(String domain, String item, String name, String value)
{
try
{
ReplaceableAttribute attribute = new ReplaceableAttribute(name, value, true);
List list = new ArrayList();
list.add(attribute);

PutAttributesRequest request = new PutAttributesRequest(domain, item, list);
client.putAttributes(request);

} catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}

public String getAttribute(String domain, String item, String name)
{
String value = "Empty Result";
try
{
GetAttributesRequest request = new GetAttributesRequest(domain, item);
GetAttributesResult result = client.getAttributes(request);
List list = result.getAttributes();
for (Attribute attribute : list)
{
if (attribute.getName().equals(name))
{
return attribute.getValue();
}
}

} catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
return value;
}

public String encrypt(String message)
{
String result = "Encryption Error.";
try
{
ByteBuffer plainText = encoder.encode(CharBuffer.wrap(message));
EncryptRequest req = new EncryptRequest().withKeyId(keyId).withPlaintext(plainText);
ByteBuffer cipherText = kms.encrypt(req).getCiphertextBlob();
byte[] bytes = new byte[cipherText.remaining()];
cipherText.get(bytes);
result = Base64.getEncoder().encodeToString(bytes);

System.out.println("\nEncryption:");
System.out.println("Original Text: " + message);
System.out.println("Encrypted Text: " + result);
} catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
return result;
}

public String decrypt(String message)
{
String result = "Decryption Error.";
try
{
byte[] encryptedBytes = Base64.getDecoder().decode(message);
ByteBuffer ciphertextBlob = ByteBuffer.wrap(encryptedBytes);
DecryptRequest req = new DecryptRequest().withCiphertextBlob(ciphertextBlob);
ByteBuffer plainText = kms.decrypt(req).getPlaintext();
result = decoder.decode(plainText).toString();

System.out.println("\nDecryption:");
System.out.println("Original Text: " + message);
System.out.println("Encrypted Text: " + result);
} catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
return result;
}

public static void main(String[] args)
{
String domainName = "demo-domain";
String itemName = "demo-item";
String attributeName = "test-attribute";
String attributeValue = "This is the information to be stored in SimpleDB.";

SDB test = new SDB();
String value = test.encrypt(attributeValue);
test.putAttribute(domainName, itemName, attributeName, value);

try
{
Thread.sleep(3000); // Sleep for some time to make sure we can get the result
} catch (Exception e) {}

value = test.getAttribute(domainName, itemName, attributeName);
test.decrypt(value);
}


}

上一篇 下一篇

 
姓名:
评论:

请输入下面这首诗词的作者姓名。

在天愿作比翼鸟,在地愿为连理枝。
天长地久有时尽,此恨绵绵无绝期。

答案:

云与清风常拥有,
冰雪知音世难求。
击节纵歌相对笑,
案上诗书杯中酒。

2020年12月31日
洛杉矶

最新评论


2026-07-23 11:37:04
qyjohn 评论了 《几句翻译》

2026-07-21 20:11:28
wells 评论了 《几句翻译》

2025-11-15 06:29:51
qyjohn 评论了 《青桔》

2025-11-14 01:55:04
wells 评论了 《青桔》

2025-10-24 18:04:06
Ted Wang 评论了 《关于作者(About Me)》

2025-03-27 08:28:09
Max 评论了 《父亲走了》

2025-03-08 04:13:52
W 评论了 《我不想颂扬普京大帝》

2024-07-22 16:02:03
云思 评论了 《无题》

2024-06-27 10:01:54
yiming 评论了 《致访客(Welcome)》

2024-05-06 08:50:06
qyjohn 评论了 《致访客(Welcome)》

2024-05-03 19:49:00
jimmie 评论了 《致访客(Welcome)》

2024-02-19 03:32:57
林小静 评论了 《埋剑渡》

2024-01-05 03:54:28
路过者 评论了 《2023年度盘点》

2024-01-04 10:22:05
qyjohn 评论了 《致访客(Welcome)》

2024-01-04 08:05:13
qyjohn 评论了 《2023年度盘点》