云点播token

Example

云点播token

1
2
3
4
5
6
7
8
9
$authKey='ucSzWPYPGydaT8mc';//Tk-authkey
$aes_key='abcdefghijklmnop';//机构自定义的16位字符串
$time=time();//Timestamp
$key = substr(openssl_digest(openssl_digest($authKey, 'sha1', true), 'sha1', true), 0, 16);
$keys = base64_encode(openssl_encrypt(strval($time), "AES-128-CBC", $key, OPENSSL_RAW_DATA, $aes_key));
$token = $aes_key . $keys;//生成Tk-token
var_dump(sprintf("Tk-authkey:%s", $authKey));
var_dump(sprintf("Timestamp:%s", $time));
var_dump(sprintf("Tk-token:%s", $token));
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.hcmdonline.util.security;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;


public class vod {

    public static final String DEFAULT_ENCODE = "UTF-8";
    public static final String KEY = "ucSzWPYPGydaT8mc";
    public static final String IV = "abcdefghijklmnop";

    /**
     * <p>加密编码UTF-8</p>
     * @param data 待加密数据
     * @return 加密数据
     */
    public static String encrypt(String data){

        String strs = null;
        try {
            byte[] bytes = doAES(Cipher.ENCRYPT_MODE, data.getBytes(), KEY.getBytes());
            // base64编码字节
            strs = new String(Base64.getEncoder().encode(bytes), DEFAULT_ENCODE);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return strs;
    }

    public static byte[] doAES(int mode, byte[] data, byte[] key){

        try{
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(key);
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat,"AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(mode, keySpec, new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8)));// 初始化
            return cipher.doFinal(data);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String data = "1111111";//Timestamp
        String encryptData = encrypt(data);
        System.out.println("明文是: " + data);
        System.out.println("加密后: " + encryptData);
    }
}

© 2016-2023 北京拓课网络科技有限公司 版权所有  京ICP备17018423号-1 京公网安备11010502043461号