一段aes加密代码

科大webvpn的url加解密流程

//种子和偏移量
wrdvpnIV: "wrdvpnisthebest!"
wrdvpnKey: "wrdvpnisthebest!"

var utf8 = aesjs.utils.utf8;
var hex = aesjs.utils.hex
var AesCfb = aesjs.ModeOfOperation.cfb
var wrdvpnKey = ''
var wrdvpnIV = ''
var textRightAppend = function (text, mode) {
    var segmentByteSize = mode === 'utf8' ? 16 : 32

    if (text.length % segmentByteSize === 0) {
        return text
    }

    var appendLength = segmentByteSize - text.length % segmentByteSize
    var i = 0
    while (i++ < appendLength) {
        text += '0'
    }
    return text
}

var encrypt = function (text, key, iv) {
    var textLength = text.length
    text = textRightAppend(text, 'utf8')
    var keyBytes = utf8.toBytes(key)
    var ivBytes = utf8.toBytes(iv)
    var textBytes = utf8.toBytes(text)
    var aesCfb = new AesCfb(keyBytes, ivBytes, 16)
    var encryptBytes = aesCfb.encrypt(textBytes)
    return hex.fromBytes(ivBytes) + hex.fromBytes(encryptBytes).slice(0, textLength * 2)
}
var decrypt = function (text, key) {
    var textLength = (text.length - 32) / 2
    text = textRightAppend(text, 'hex')
    var keyBytes = utf8.toBytes(key)
    var ivBytes = hex.toBytes(text.slice(0, 32))
    var textBytes = hex.toBytes(text.slice(32))
    var aesCfb = new AesCfb(keyBytes, ivBytes, 16)
    var decryptBytes = aesCfb.decrypt(textBytes)
    return utf8.fromBytes(decryptBytes).slice(0, textLength)
}
var encrypUrl = function (protocol, url) {
    var port = "";
    var segments = "";

    if (url.substring(0, 7) == "http://") {
        url = url.substr(7);
    } else if (url.substring(0, 8) == "https://") {
        url = url.substr(8);
    }


    var v6 = "";
    var match = /\[[0-9a-fA-F:]+?\]/.exec(url);
    if (match) {
        v6 = match[0];
        url = url.slice(match[0].length);
    }
    segments = url.split("?")[0].split(":");
    if (segments.length > 1) {
        port = segments[1].split("/")[0]
        url = url.substr(0, segments[0].length) + url.substr(segments[0].length + port.length + 1);
    }

    if (protocol != "connection") {
        var i = url.indexOf('/');
        if (i == -1) {
            if (v6 != "") {
                url = v6;
            }
            url = encrypt(url, wrdvpnKey, wrdvpnIV)
        } else {
            var host = url.slice(0, i);
            var path = url.slice(i);
            if (v6 != "") {
                host = v6;
            }
            url = encrypt(host, wrdvpnKey, wrdvpnIV) + path;
        }
    }
    if (port != "") {
        url = "/" + protocol + "-" + port + "/" + url;
    } else {
        url = "/" + protocol + "/" + url;
    }
    return url;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是Unity中AES加密和解密的示例代码: 加密代码: ```csharp using System.Security.Cryptography; using UnityEngine; public static class AESEncryption { private static readonly byte[] Key = new byte[16] { 215, 254, 100, 91, 10, 69, 23, 146, 12, 175, 237, 65, 38, 205, 85, 111 }; private static readonly byte[] IV = new byte[16] { 183, 221, 44, 251, 126, 7, 77, 234, 154, 150, 97, 24, 165, 2, 153, 201 }; public static string Encrypt(string plainText) { byte[] encrypted; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Key; aesAlg.IV = IV; ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } return System.Convert.ToBase64String(encrypted); } } ``` 解密代码: ```csharp using System.Security.Cryptography; using UnityEngine; public static class AESEncryption { private static readonly byte[] Key = new byte[16] { 215, 254, 100, 91, 10, 69, 23, 146, 12, 175, 237, 65, 38, 205, 85, 111 }; private static readonly byte[] IV = new byte[16] { 183, 221, 44, 251, 126, 7, 77, 234, 154, 150, 97, 24, 165, 2, 153, 201 }; public static string Decrypt(string cipherText) { byte[] cipherBytes = System.Convert.FromBase64String(cipherText); using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Key; aesAlg.IV = IV; ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(cipherBytes)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt)) { return srDecrypt.ReadToEnd(); } } } } } } ``` 注意:这个示例代码中的密钥和向量都是为了演示而写死的,请根据实际需求使用更安全的方式生成密钥和向量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值