I am trying to generate access keys and secret keys in the same fashion as S3 using java but am having some trouble.
我试图使用java以与S3相同的方式生成访问密钥和密钥,但是遇到了一些麻烦。
As a starting point I am looking at this bouncy castle example , I have this code up and running but am not sure of two things 1) how to set it up to use the same key generation as s3 which uses HMAC-SHA1 as outlined here and 2) how to get the friendly public/private key strings out for the the user.
作为一个起点,我正在看这个充满活力的城堡示例,我有这个代码并运行,但我不确定两件事1)如何设置它使用相同的密钥生成s3使用HMAC-SHA1如此处所述2)如何为用户获取友好的公钥/私钥字符串。
You may have guessed I am new to java encryption and the bouncy castle libraries, however I did find JCEKeyGenerator.HMACSHA1 in the bc docs but am unable to find an example of its use. Any help would be greatly appreciated.
您可能已经猜到我是java加密和充气城堡库的新手,但是我确实在bc文档中找到了JCEKeyGenerator.HMACSHA1,但我找不到它的使用示例。任何帮助将不胜感激。
Thanks.
谢谢。
1 个解决方案
#1
3
You'll need to make use of javax.crypto.KeyGenerator
to create the AWSAccessKeyId
and the AWSSecretAccessKey
:
您需要使用javax.crypto.KeyGenerator来创建AWSAccessKeyId和AWSSecretAccessKey:
javax.crypto.KeyGenerator generator = javax.crypto.KeyGenerator.getInstance("HMACSHA1");
generator.init(120);
byte[] awsAccessKeyId = generator.generateKey().getEncoded();
generator.init(240);
byte[] awsSecretAccessKey = generator.generateKey().getEncoded();
Then, you'll want to base64 encode the bytes (this uses MimeUtility from mail.jar):
然后,你需要base64编码字节(这使用来自mail.jar的MimeUtility):
final ByteArrayOutputStream encoded = new ByteArrayOutputStream();
final OutputStream encoder = javax.mail.internet.MimeUtility.encode(encoded, "base64");
encoder.write(awsAccessKeyId);
encoder.flush();
encoder.close();
String accessKeyId = new String(encoded.toByteArray(), encoding).replaceAll("[\\r\\n]", "");
#1
3
You'll need to make use of javax.crypto.KeyGenerator
to create the AWSAccessKeyId
and the AWSSecretAccessKey
:
您需要使用javax.crypto.KeyGenerator来创建AWSAccessKeyId和AWSSecretAccessKey:
javax.crypto.KeyGenerator generator = javax.crypto.KeyGenerator.getInstance("HMACSHA1");
generator.init(120);
byte[] awsAccessKeyId = generator.generateKey().getEncoded();
generator.init(240);
byte[] awsSecretAccessKey = generator.generateKey().getEncoded();
Then, you'll want to base64 encode the bytes (this uses MimeUtility from mail.jar):
然后,你需要base64编码字节(这使用来自mail.jar的MimeUtility):
final ByteArrayOutputStream encoded = new ByteArrayOutputStream();
final OutputStream encoder = javax.mail.internet.MimeUtility.encode(encoded, "base64");
encoder.write(awsAccessKeyId);
encoder.flush();
encoder.close();
String accessKeyId = new String(encoded.toByteArray(), encoding).replaceAll("[\\r\\n]", "");