vod普通加密
This commit is contained in:
@@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapp
|
||||
import com.peanut.modules.common.dao.VodAesTokenDao;
|
||||
import com.peanut.modules.common.entity.VodAesTokenEntity;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import java.util.Base64;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -52,7 +52,7 @@ public class PlayToken {
|
||||
long expire = System.currentTimeMillis() + 60000L;
|
||||
base += "_" + expire; //自定义字符串,base的长度为16位字符(此例中,时间戳占13位,下划线(_)占1位,则还需传入2位字符。实际配置时也可按需全部更改,最终保证base为16、24或32位字符串即可。)
|
||||
//生成token
|
||||
String token = encrypt(base, ENCRYPT_KEY); //arg1为要加密的自定义字符串,arg2为加密Key
|
||||
String token = encrypt(base); //arg1为要加密的自定义字符串,arg2为加密Key
|
||||
//保存token,用于解密时校验token的有效性,例如:过期时间、token的使用次数
|
||||
saveToken(token);
|
||||
return token;
|
||||
@@ -71,7 +71,7 @@ public class PlayToken {
|
||||
if (null == token || "".equals(token)) {
|
||||
return false;
|
||||
}
|
||||
String base = decrypt(token,ENCRYPT_KEY); //arg1为解密字符串,arg2为解密Key
|
||||
String base = decrypt(token); //arg1为解密字符串,arg2为解密Key
|
||||
//先校验token的有效时间
|
||||
Long expireTime = Long.valueOf(base.substring(base.lastIndexOf("_") + 1));
|
||||
// System.out.println("时间校验:" + expireTime);
|
||||
@@ -128,6 +128,45 @@ public class PlayToken {
|
||||
//TODO 校验信息的有效性,例如UID是否有效等
|
||||
return true;
|
||||
}
|
||||
|
||||
public static String encrypt(String value) {
|
||||
try {
|
||||
IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(ENCRYPT_KEY.getBytes("UTF-8"), "AES");
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
|
||||
|
||||
byte[] encrypted = cipher.doFinal(value.getBytes());
|
||||
|
||||
// 使用 URL 安全的 Base64 编码
|
||||
return Base64.getUrlEncoder().encodeToString(encrypted);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String decrypt(String encrypted) {
|
||||
try {
|
||||
IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(ENCRYPT_KEY.getBytes("UTF-8"), "AES");
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
||||
|
||||
// 使用 URL 安全的 Base64 解码
|
||||
byte[] original = cipher.doFinal(Base64.getUrlDecoder().decode(encrypted));
|
||||
|
||||
return new String(original);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* AES加密生成Token
|
||||
*
|
||||
@@ -136,32 +175,34 @@ public class PlayToken {
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String encrypt(String encryptStr, String encryptKey) throws Exception {
|
||||
IvParameterSpec e = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(encryptKey.getBytes("UTF-8"), "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, e);
|
||||
byte[] encrypted = cipher.doFinal(encryptStr.getBytes());
|
||||
return Base64.encodeBase64String(encrypted);
|
||||
}
|
||||
/**
|
||||
* AES解密token
|
||||
*
|
||||
* @param encryptStr 解密字符串
|
||||
* @param decryptKey 解密Key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String decrypt(String encryptStr, String decryptKey) throws Exception {
|
||||
|
||||
IvParameterSpec e = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(decryptKey.getBytes("UTF-8"), "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec, e);
|
||||
|
||||
byte[] encryptByte = Base64.decodeBase64(encryptStr);
|
||||
byte[] decryptByte = cipher.doFinal(encryptByte);
|
||||
return new String(decryptByte);
|
||||
}
|
||||
// public String encrypt(String encryptStr, String encryptKey) throws Exception {
|
||||
// IvParameterSpec e = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
|
||||
// SecretKeySpec skeySpec = new SecretKeySpec(encryptKey.getBytes("UTF-8"), "AES");
|
||||
// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||
// cipher.init(Cipher.ENCRYPT_MODE, skeySpec, e);
|
||||
// byte[] encrypted = cipher.doFinal(encryptStr.getBytes());
|
||||
// return Base64.encodeBase64String(encrypted);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * AES解密token
|
||||
// *
|
||||
// * @param encryptStr 解密字符串
|
||||
// * @param decryptKey 解密Key
|
||||
// * @return
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public String decrypt(String encryptStr, String decryptKey) throws Exception {
|
||||
//
|
||||
// IvParameterSpec e = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
|
||||
// SecretKeySpec skeySpec = new SecretKeySpec(decryptKey.getBytes("UTF-8"), "AES");
|
||||
// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||
// cipher.init(Cipher.DECRYPT_MODE, skeySpec, e);
|
||||
//
|
||||
// byte[] encryptByte = Base64.decodeBase64(encryptStr);
|
||||
// byte[] decryptByte = cipher.doFinal(encryptByte);
|
||||
// return new String(decryptByte);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ public class SpdbUtil {
|
||||
submitTranscodeJobsRequest.setTemplateGroupId("d346d2609a058b9dfd9b8bb392175721");
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("CipherText",edk);
|
||||
jsonObject.put("DecryptKeyUri","http://api.nuttyreading.com/sys/vodAli/vodAliVideoRe?CipherText="+edk);
|
||||
jsonObject.put("DecryptKeyUri","https://api.nuttyreading.com/sys/vodAli/vodAliVideoRe?CipherText="+edk);
|
||||
jsonObject.put("KeyServiceType","KMS");
|
||||
submitTranscodeJobsRequest.setEncryptConfig(jsonObject.toJSONString());
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
|
||||
@@ -200,15 +200,15 @@ public class CourseController {
|
||||
|
||||
@RequestMapping("/mytt")
|
||||
public R mytt() throws Exception {
|
||||
// String s = playToken.generateToken();
|
||||
// System.out.println(s);
|
||||
// boolean b = playToken.validateToken(s);
|
||||
// System.out.println(b);
|
||||
String s = playToken.generateToken();
|
||||
System.out.println(s);
|
||||
boolean b = playToken.validateToken(s);
|
||||
System.out.println(b);
|
||||
|
||||
GenerateKMSDataKeyResponseBody kms = SpdbUtil.KMS();
|
||||
|
||||
return R.ok().put("result",kms);
|
||||
// return R.ok();
|
||||
// GenerateKMSDataKeyResponseBody kms = SpdbUtil.KMS();
|
||||
//
|
||||
// return R.ok().put("result",kms);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.peanut.common.utils.SpdbUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@@ -15,7 +16,7 @@ public class VodAliController {
|
||||
private PlayToken playToken;
|
||||
|
||||
@GetMapping("/vodAliVideoRe")
|
||||
public String vodAliVideoRe(@RequestParam("CipherText") String cipherText,
|
||||
public byte[] vodAliVideoRe(@RequestParam("CipherText") String cipherText,
|
||||
@RequestParam("MtsHlsUriToken") String mtsHlsUriToken){
|
||||
|
||||
boolean validRe = false;
|
||||
@@ -25,13 +26,13 @@ public class VodAliController {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (!validRe) {
|
||||
return "Illegal access";
|
||||
return Base64.decodeBase64("Illegal access");
|
||||
}
|
||||
if (null == cipherText) {
|
||||
return Base64.decodeBase64("Illegal access !");
|
||||
}
|
||||
if (null == cipherText)
|
||||
return "Illegal access !";
|
||||
|
||||
DecryptKMSDataKeyResponseBody decryptKMSDataKeyResponseBody = SpdbUtil.enKMS(cipherText);
|
||||
return decryptKMSDataKeyResponseBody.getPlaintext();
|
||||
return Base64.decodeBase64(decryptKMSDataKeyResponseBody.getPlaintext());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ connection-timeout: 6000000ms
|
||||
spring:
|
||||
# 环境 dev/dev1|test|prod
|
||||
profiles:
|
||||
active: dev1
|
||||
active: prod
|
||||
# jackson时间格式化
|
||||
jackson:
|
||||
time-zone: GMT+8
|
||||
|
||||
Reference in New Issue
Block a user