返回提交历史
Modified
docs/login/cookie_refresh.md
+62
-0
XFEstudio/bilibili-API-collect
feat: cookie刷新生成CorrespondPath算法添加java代码实现 (#1017)
Co-authored-by: Duyulin <duyulin@kingsoft.com>
e6dc29e
代码差异
1 个文件
+62
-0
@@ -278,6 +278,68 @@ JNrRuoEUXpabUzGB8QIDAQAB
278
278
97759947aa357ed5d88cf9bf1172737570b7bba2d6788d39006f082b2b25ddf53b581f1f0c61ed8573317485ef525d2789faa25a277b4602a4b9cbf837681093a03e96cb9773a11df4bb1e20f1587180b3e958194de922d7dd94d0a2f0b9b0ef74e426e8041f99b99e7c02407ef4ab38040e61be81e4fdfbdb73461e3a2ad810
279
279
```
280
280
281
### Java
282
```Java
283
import javax.crypto.Cipher;
284
import javax.crypto.spec.OAEPParameterSpec;
285
import javax.crypto.spec.PSource;
286
import java.math.BigInteger;
287
import java.security.KeyFactory;
288
import java.security.PublicKey;
289
import java.security.spec.MGF1ParameterSpec;
290
import java.security.spec.X509EncodedKeySpec;
291
import java.util.Base64;
292
293
public class CookieRefresh {
294
private static final String PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" +
295
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLgd2OAkcGVtoE3ThUREbio0Eg\n" +
296
"Uc/prcajMKXvkCKFCWhJYJcLkcM2DKKcSeFpD/j6Boy538YXnR6VhcuUJOhH2x71\n" +
297
"nzPjfdTcqMz7djHum0qSZA0AyCBDABUqCrfNgCiJ00Ra7GmRj+YCK1NJEuewlb40\n" +
298
"JNrRuoEUXpabUzGB8QIDAQAB\n" +
299
"-----END PUBLIC KEY-----";
300
301
public static void main(String[] args) {
302
try {
303
String correspondPath = getCorrespondPath(String.format("refresh_%d", System.currentTimeMillis()), PUBLIC_KEY);
304
System.out.println(correspondPath);
305
} catch (Exception e) {
306
e.printStackTrace();
307
}
308
}
309
310
public static String getCorrespondPath(String plaintext, String publicKeyStr) throws Exception {
311
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
312
publicKeyStr = publicKeyStr
313
.replace("-----BEGIN PUBLIC KEY-----", "")
314
.replace("-----END PUBLIC KEY-----", "")
315
.replace("\n", "")
316
.trim();
317
byte[] publicBytes = Base64.getDecoder().decode(publicKeyStr);
318
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicBytes);
319
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
320
321
String algorithm = "RSA/ECB/OAEPPadding";
322
Cipher cipher = Cipher.getInstance(algorithm);
323
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
324
325
// Encode the plaintext to bytes
326
byte[] plaintextBytes = plaintext.getBytes("UTF-8");
327
328
// Add OAEP padding to the plaintext bytes
329
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
330
cipher.init(Cipher.ENCRYPT_MODE, publicKey, oaepParams);
331
// Encrypt the padded plaintext bytes
332
byte[] encryptedBytes = cipher.doFinal(plaintextBytes);
333
// Convert the encrypted bytes to a Base64-encoded string
334
return new BigInteger(1, encryptedBytes).toString(16);
335
}
336
}
337
```
338
339
```
340
f87666152da692735123f4e49053e5a98c16854673b2e632f31a3ff0c029640772873661a9a8412db6be447a0bfa03a295d15548cbfd2bb35634e98ba5f25b1205519d6e6119b483f4c516c1e106d45b04ff98c73560949d379d3edaf3c0ecd10a1d46134fb9ca443122ab33c16d1dd48280496f949ed960a2fbcd65f10935e
341
```
342
281
343
#### vercel云函数
282
344
283
345
```bash