婉兮清扬

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

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);
}


}

上一篇 下一篇

 
姓名:
评论:

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

杯汝来前!老子今朝,点检形骸。
甚长年抱渴,咽如焦釜;于今喜睡,气似奔雷。
汝说“刘伶,古今达者,醉后何妨死便埋”。
浑如此,叹汝于知己,真少恩哉!

答案:

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

蒋清野
2000.12.31 于 洛杉矶