当前位置:  编程技术>.net/c#/asp.net

c#和avascript加解密之间的互转代码分享

    来源: 互联网  发布时间:2014-10-29

    本文导语:  1:Xxtea支持中文; 2:支持JS和C#加解密之间的互转; 一:C#部分 代码如下:class XXTEA2 {     public static string Encrypt(string source, string key)     {         System.Text.Encoding encoder = System.Text.Encoding.UTF8;         //UTF8==>BASE64==>XXTEA=...

1:Xxtea支持中文;

2:支持JS和C#加解密之间的互转;

一:C#部分

代码如下:

class XXTEA2
{
    public static string Encrypt(string source, string key)
    {
        System.Text.Encoding encoder = System.Text.Encoding.UTF8;
        //UTF8==>BASE64==>XXTEA==>BASE64
        byte[] bytData = encoder.GetBytes(base64Encode(source));
        byte[] bytKey = encoder.GetBytes(key);
        if (bytData.Length == 0)
        {
            return "";
        }
        return System.Convert.ToBase64String(ToByteArray(Encrypt(ToUInt32Array(bytData, true), ToUInt32Array(bytKey, false)), false));
    }
    public static string Decrypt(string source, string key)
    {
        if (source.Length == 0)
        {
            return "";
        }
        // reverse
        System.Text.Encoding encoder = System.Text.Encoding.UTF8;
        byte[] bytData = System.Convert.FromBase64String(source);
        byte[] bytKey = encoder.GetBytes(key);

        return base64Decode(encoder.GetString(ToByteArray(Decrypt(ToUInt32Array(bytData, false), ToUInt32Array(bytKey, false)), true)));
    }

    private static UInt32[] Encrypt(UInt32[] v, UInt32[] k)
    {
        Int32 n = v.Length - 1;
        if (n < 1)
        {
            return v;
        }
        if (k.Length < 4)
        {
            UInt32[] Key = new UInt32[4];
            k.CopyTo(Key, 0);
            k = Key;
        }
        UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum = 0, e;
        Int32 p, q = 6 + 52 / (n + 1);
        while (q-- > 0)
        {
            sum = unchecked(sum + delta);
            e = sum >> 2 & 3;
            for (p = 0; p < n; p++)
            {
                y = v[p + 1];
                z = unchecked(v[p] += (z >> 5 ^ y > 3 ^ z > 5 ^ y > 3 ^ z > 2 & 3;
            for (p = n; p > 0; p--)
            {
                z = v[p - 1];
                y = unchecked(v[p] -= (z >> 5 ^ y > 3 ^ z > 5 ^ y > 3 ^ z > 2) : ((Data.Length >> 2) + 1));
        UInt32[] Result;
        if (IncludeLength)
        {
            Result = new UInt32[n + 1];
            Result[n] = (UInt32)Data.Length;
        }
        else
        {
            Result = new UInt32[n];
        }
        n = Data.Length;
        for (Int32 i = 0; i < n; i++)
        {
            Result[i >> 2] |= (UInt32)Data[i] > ((i & 3) >> 8 & 0xff,
                                   v[i] >>> 16 & 0xff,
                                   v[i] >>> 24 & 0xff);
    }
    if (w) {
        return v.join('').substring(0, n);
    }
    else {
        return v.join('');
    }
}

//将string转换为long,private
Xxtea.prototype._str2long = function (s, w) {
    var len = s.length;
    var v = [];
    for (var i = 0; i < len; i += 4) {
        v[i >> 2] = s.charCodeAt(i)
                  | s.charCodeAt(i + 1) 2 & 3;
        for (p = 0; p < n; p++) {
            y = v[p + 1];
            mx = (z >>> 5 ^ y >> 3 ^ z >> 5 ^ y >> 3 ^ z >> 2 & 3;
        for (p = n; p > 0; p--) {
            z = v[p - 1];
            mx = (z >>> 5 ^ y >> 3 ^ z >> 5 ^ y >> 3 ^ z = 0x0001) && (c 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
        else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
}

//function:change utf8 to utf16
//parms(str):string that you want to change
UtfParser.utf8to16 = function (str) {
    str = str.toString();

    var out, i, len, c;
    var char2, char3;

    out = "";
    len = str.length;
    i = 0;
    while (i < len) {
        c = str.charCodeAt(i++);
        switch (c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                out += str.charAt(i - 1);
                break;
            case 12: case 13:
                // 110x xxxx   10xx xxxx
                char2 = str.charCodeAt(i++);
                out += String.fromCharCode(((c & 0x1F) 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output = output +
           Base64._keyStr.charAt(enc1) +
           Base64._keyStr.charAt(enc2) +
           Base64._keyStr.charAt(enc3) +
           Base64._keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } while (i < input.length);

    return output;
}

Base64.decode64 = function (input) {
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    var base64test = /[^A-Za-z0-9+/=n]/g;
    if (base64test.exec(input)) {
        alert("There were invalid base64 characters in the input text.n" +
              "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='n" +
              "Expect errors in decoding.");
    }
    input = input.replace(/[^A-Za-z0-9+/=]/g, "");

    do {
        enc1 = Base64._keyStr.indexOf(input.charAt(i++));
        enc2 = Base64._keyStr.indexOf(input.charAt(i++));
        enc3 = Base64._keyStr.indexOf(input.charAt(i++));
        enc4 = Base64._keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 > 4);
        chr2 = ((enc2 & 15) > 2);
        chr3 = ((enc3 & 3)


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐




  • 特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3