欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 16:11:34
1:xxtea支持中文; 2:支持js和c#加解密之间的互转; 一:c#部分 复制代码 代码如下:class xxtea2 {    ...

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 << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
            }
            y = v[0];
            z = unchecked(v[n] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
        }
        return v;
    }

    private static uint32[] decrypt(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, e;
        int32 p, q = 6 + 52 / (n + 1);
        sum = unchecked((uint32)(q * delta));
        while (sum != 0)
        {
            e = sum >> 2 & 3;
            for (p = n; p > 0; p--)
            {
                z = v[p - 1];
                y = unchecked(v[p] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
            }
            z = v[n];
            y = unchecked(v[0] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
            sum = unchecked(sum - delta);
        }
        return v;
    }

    private static uint32[] touint32array(byte[] data, boolean includelength)
    {
        int32 n = (((data.length & 3) == 0) ? (data.length >> 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) << 3);
        }
        return result;
    }

    private static byte[] tobytearray(uint32[] data, boolean includelength)
    {
        int32 n;
        if (includelength)
        {
            n = (int32)data[data.length - 1];
        }
        else
        {
            n = data.length << 2;
        }
        byte[] result = new byte[n];
        for (int32 i = 0; i < n; i++)
        {
            result[i] = (byte)(data[i >> 2] >> ((i & 3) << 3));
        }
        return result;
    }

    public static string base64decode(string data)
    {
        try
        {
            var encoder = system.text.encoding.utf8;

            byte[] todecode_byte = convert.frombase64string(data);
            return encoder.getstring(todecode_byte);
        }
        catch (exception e)
        {
            throw new exception("error in base64decode" + e.message);
        }
    }

    public static string base64encode(string data)
    {
        try
        {
            byte[] encdata_byte = new byte[data.length];
            encdata_byte = system.text.encoding.utf8.getbytes(data);
            string encodeddata = convert.tobase64string(encdata_byte);
            return encodeddata;
        }
        catch (exception e)
        {
            throw new exception("error in base64encode" + e.message);
        }
    }


 

二:js部分

//***************************************************************************************
// autor:tecky
// var prefix = xt
//  update:
//***************************************************************************************

function xxtea(privatekey) {
    this._keystring = privatekey;
}

//将长整形转换为string,private
xxtea.prototype._long2str = function (v, w) {
    var vl = v.length;
    var n = (vl - 1) << 2;
    if (w) {
        var m = v[vl - 1];
        if ((m < n - 3) || (m > n)) return null;
        n = m;
    }
    for (var i = 0; i < vl; i++) {
        v[i] = string.fromcharcode(v[i] & 0xff,
                                   v[i] >>> 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) << 8
                  | s.charcodeat(i + 2) << 16
                  | s.charcodeat(i + 3) << 24;
    }
    if (w) {
        v[v.length] = len;
    }
    return v;
}

//function: encrypt str with private key by xxtea
xxtea.prototype.xxtea_encrypt = function (str) {
    if (str == "") {
        return "";
    }
    str = base64.encode64(utfparser.utf16to8(str));
    var v = this._str2long(str, true);
    var k = this._str2long(this._keystring, false);
    if (k.length < 4) {
        k.length = 4;
    }
    var n = v.length - 1;

    var z = v[n], y = v[0], delta = 0x9e3779b9;
    var mx, e, p, q = math.floor(6 + 52 / (n + 1)), sum = 0;
    while (0 < q--) {
        sum = sum + delta & 0xffffffff;
        e = sum >>> 2 & 3;
        for (p = 0; p < n; p++) {
            y = v[p + 1];
            mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
            z = v[p] = v[p] + mx & 0xffffffff;
        }
        y = v[0];
        mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
        z = v[n] = v[n] + mx & 0xffffffff;
    }

    return base64.encode64(this._long2str(v, false));
}

//function: decrypt str with private key by xxtea
xxtea.prototype.xxtea_decrypt = function (str) {
    if (str == "") {
        return "";
    }
    str = base64.decode64(str);
    var v = this._str2long(str, false);
    var k = this._str2long(this._keystring, false);
    if (k.length < 4) {
        k.length = 4;
    }
    var n = v.length - 1;

    var z = v[n - 1], y = v[0], delta = 0x9e3779b9;
    var mx, e, p, q = math.floor(6 + 52 / (n + 1)), sum = q * delta & 0xffffffff;
    while (sum != 0) {
        e = sum >>> 2 & 3;
        for (p = n; p > 0; p--) {
            z = v[p - 1];
            mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
            y = v[p] = v[p] - mx & 0xffffffff;
        }
        z = v[n];
        mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
        y = v[0] = v[0] - mx & 0xffffffff;
        sum = sum - delta & 0xffffffff;
    }

    return utfparser.utf8to16(base64.decode64(this._long2str(v, true)));
}

//class:utf16 to utf8, utf8 ot utf16
//author:tecky
//date:2008-06-03
function utfparser() {
    //all method is static
}

//function:change utf16 to utf8
//parms(str):string that you want to change
utfparser.utf16to8 = function (str) {
    var out, i, len, c;

    out = "";
    len = str.length;
    for (i = 0; i < len; i++) {
        c = str.charcodeat(i);
        if ((c >= 0x0001) && (c <= 0x007f)) {
            out += str.charat(i);
        }
        else if (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) | (char2 & 0x3f));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                char2 = str.charcodeat(i++);
                char3 = str.charcodeat(i++);
                out += string.fromcharcode(((c & 0x0f) << 12) |
                               ((char2 & 0x3f) << 6) |
                               ((char3 & 0x3f) << 0));
                break;
        }
    }

    return out;
}

// class:base64 encode & decode
// autor:tecky
// date:2008-06-03
function base64() {
    //all method is static
}

//static
base64._keystr = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=";
base64.encode64 = function (input) {
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;

    do {
        chr1 = input.charcodeat(i++);
        chr2 = input.charcodeat(i++);
        chr3 = input.charcodeat(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 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 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + string.fromcharcode(chr1);

        if (enc3 != 64) {
            output = output + string.fromcharcode(chr2);
        }
        if (enc4 != 64) {
            output = output + string.fromcharcode(chr3);
        }

        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

    } while (i < input.length);

    return output;
}
[/code]

上一篇:

下一篇: