gpt4 book ai didi

java - Recaptcha stoken 生成(从 Java 转换为 Ruby)

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:47 26 4
gpt4 key购买 nike

Google 提供了以下示例代码,展示了如何为其第二版 Recaptcha 生成安全 token :

public class STokenUtils {
private static final String CIPHER_INSTANCE_NAME = "AES/ECB/PKCS5Padding";

public static final String createSToken(String siteSecret) {
String sessionId = UUID.randomUUID().toString();
String jsonToken = createJsonToken(sessionId);
return encryptAes(jsonToken, siteSecret);
}

private static final String createJsonToken(String sessionId) {
JsonObject obj = new JsonObject();
obj.addProperty("session_id", sessionId);
obj.addProperty("ts_ms", System.currentTimeMillis());
return new Gson().toJson(obj);
}

private static String encryptAes(String input, String siteSecret) {
try {
SecretKeySpec secretKey = getKey(siteSecret);
Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE_NAME);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return BaseEncoding.base64Url().omitPadding().encode(cipher.doFinal(input.getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

private static String decryptAes(String input, String key) throws Exception {
SecretKeySpec secretKey = getKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE_NAME);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(
BaseEncoding.base64Url().omitPadding().decode(input)), "UTF-8");
}

private static SecretKeySpec getKey(String siteSecret){
try {
byte[] key = siteSecret.getBytes("UTF-8");
key = Arrays.copyOf(MessageDigest.getInstance("SHA").digest(key), 16);
return new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}

完整代码可在以下位置找到:https://github.com/google/recaptcha-java

我想在 Ruby 2.1+ 中生成这个 token ,并且已经做到了这一点,但它输出了不正确的数据。我正在尝试慢慢调试它,但与此同时我想知道是否有人可以在我的过程中看到任何明显的缺陷?

      stoken_json = hash_to_json({'session_id' => SecureRandom.uuid, 'ts_ms' => Time.now.to_i})
cipher = OpenSSL::Cipher::AES128.new(:ECB)
private_key_digest = Digest::SHA1.hexdigest(private_key)[0...16]

cipher.encrypt
cipher.key = private_key_digest
encrypted_stoken = cipher.update(stoken_json) << cipher.final
encoded_stoken = Base64.urlsafe_encode64(encrypted_stoken).gsub(/\=+\Z/, '')

最佳答案

事实证明我很接近。我需要 digest 而不是 hexdigest 私钥:

private_key_digest = Digest::SHA1.digest(private_key)[0...16]

所以最后的代码是:

stoken_json = hash_to_json({'session_id' => SecureRandom.uuid, 'ts_ms' => (Time.now.to_f * 1000).to_i})
cipher = OpenSSL::Cipher::AES128.new(:ECB)
private_key_digest = Digest::SHA1.digest(private_key)[0...16]

cipher.encrypt
cipher.key = private_key_digest
encrypted_stoken = cipher.update(stoken_json) << cipher.final
encoded_stoken = Base64.urlsafe_encode64(encrypted_stoken).gsub(/\=+\Z/, '')

似乎没有内置方法从 base64 字符串中去除填充,因此 .gsub 位于末尾。

我还需要以毫秒为单位的时间戳,以便该部分也已修改。

recaptcha gem 有一个方法 hash_to_json 我正在使用,否则我怀疑你会使用 JSON gem。

关于java - Recaptcha stoken 生成(从 Java 转换为 Ruby),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com