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