简体   繁体   English

如何使用 java 生成 s3 样式的访问/密钥

[英]how to generate s3 style access/secret key using java

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) 如何设置它以使用与使用 HMAC-SHA1 的 s3 相同的密钥生成,如此所述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.您可能已经猜到我是 java 加密和充气城堡库的新手,但是我确实在 bc 文档中找到了 JCEKeyGenerator.HMACSHA1,但找不到它的使用示例。 Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks.谢谢。

You'll need to make use of javax.crypto.KeyGenerator to create the AWSAccessKeyId and the AWSSecretAccessKey :您需要使用javax.crypto.KeyGenerator来创建AWSAccessKeyIdAWSSecretAccessKey

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]", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用AWS S3 Java SDK,确定授权(访问密钥和秘密密钥)是否正确的最快方法是什么? - Using the AWS S3 Java SDK, what is the fastest way to determine if the authorization (access key and secret key) is correct? 使用 root 访问密钥和密钥时的 S3 策略 - S3 policy when using root access key and secret key 没有访问权限的AWS S3上载和Java中的密钥 - AWS S3 upload without access and secret key in Java 如何在Java中生成一次密钥并在2个不同的程序中使用该密钥 - How to generate secret key in Java once and use that key in 2 different programs 即使输入了错误的访问密钥或密钥,也从 s3 获取 preSignedUrl - Getting preSignedUrl from s3 even if wrong access key or secret key is entered 如何使用SecureRandom.getInstanceStrong()生成密钥? - How to generate secret key using SecureRandom.getInstanceStrong()? jwt 摘要和 java 的密钥是如何关联的? - How are jwt digest and java's secret key linked? 如何序列化Java中的Secret key - How to serialize Secret key in Java 如何管理java中的密钥 - How to manage secret Key in java 如何使用 Java 生成具有大量数据的 parquet 文件并上传到 aws s3 存储桶 - How to generate parquet file with large amount of data using Java and upload to aws s3 bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM