读书人

Convert.ToBase64String(Encoding.Uni

发布时间: 2012-04-24 14:15:38 作者: rapoo

Convert.ToBase64String(Encoding.Unicode.GetBytes(userName))
asp.net做的base64加密,在asp下如何解密?是不是用Unicode编码的不好解?要用ascii编码?

[解决办法]
Convert.FromBase64String ???
[解决办法]
Convert.ToBase64String,你用的这不都是吗
[解决办法]
base64解码很容易自己写。比如你可以从下帖的实现入手:

http://topic.csdn.net/u/20120202/10/f9e9bc8b-238c-479c-927b-1ac70657eb0d.html
[解决办法]

探讨

我是asp.net里加密,asp里想取出来

[解决办法]
探讨

引用:

我是asp.net里加密,asp里想取出来

自己写一个base64加密解密算法好一点,自己定制规则,就不会受语言环境的限制了

[解决办法]
建议自己写,没有什么难度,
[解决办法]
我靠, 密码竟然是可解码的, 第一次听说
[解决办法]
探讨

这些都是.net的,我是想做一个给其他网站传递一些数据的功能,这些网站有asp的,不知道怎么解码,网上的有点乱,有些都是自己编码的自己可以解码,.net编码的asp解码总是不对,自己去实现又有点犯不着

[解决办法]
探讨

我是觉得既然微软提供了Convert.ToBase64String(Encoding.Unicode.GetBytes(userName))
那么应该有各种平台与他兼容的代码出现,我就不用费心再去写一遍了吧

[解决办法]
js看看
JScript code
// by anzhiqiang add.2009-2-27.用于base64与反var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var base64DecodeChars = new Array(    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,    -1,  0,  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, -1, -1, -1, -1, -1,    -1, 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, -1, -1, -1, -1, -1);function base64encode(str) {    var out, i, len;    var c1, c2, c3;    len = str.length;    i = 0;    out = "";    while(i < len) {    c1 = str.charCodeAt(i++) & 0xff;    if(i == len)    {        out += base64EncodeChars.charAt(c1 >> 2);        out += base64EncodeChars.charAt((c1 & 0x3) << 4);        out += "==";        break;    }    c2 = str.charCodeAt(i++);    if(i == len)    {        out += base64EncodeChars.charAt(c1 >> 2);        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));        out += base64EncodeChars.charAt((c2 & 0xF) << 2);        out += "=";        break;    }    c3 = str.charCodeAt(i++);    out += base64EncodeChars.charAt(c1 >> 2);    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));    out += base64EncodeChars.charAt(c3 & 0x3F);    }    return out;}function base64decode(str) {    var c1, c2, c3, c4;    var i, len, out;    len = str.length;    i = 0;    out = "";    while(i < len) {    /* c1 */    do {        c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];    } while(i < len && c1 == -1);    if(c1 == -1)        break;    /* c2 */    do {        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];    } while(i < len && c2 == -1);    if(c2 == -1)        break;    out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));    /* c3 */    do {        c3 = str.charCodeAt(i++) & 0xff;        if(c3 == 61)        return out;        c3 = base64DecodeChars[c3];    } while(i < len && c3 == -1);    if(c3 == -1)        break;    out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));    /* c4 */    do {        c4 = str.charCodeAt(i++) & 0xff;        if(c4 == 61)        return out;        c4 = base64DecodeChars[c4];    } while(i < len && c4 == -1);    if(c4 == -1)        break;    out += String.fromCharCode(((c3 & 0x03) << 6) | c4);    }    return out;}function utf16to8(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 utf8to16(str) {    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;} 


[解决办法]
相应的C# base64

C# code
/********************************************************************************** Author: anzhiqiang** Created On: 05/18/2010 16:30** Comments: Encrypt&Decrypt component.*********************************************************************************/using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;using System.Web.Security;using System.Text.RegularExpressions;using System.Security.Cryptography;namespace EShipping.Common{    public class Security    {        public Security()        {        }        /// <summary>        /// 用MD5或SHA1加密方式        /// </summary>        /// <param name="PasswordString"></param>        /// <param name="PasswordFormat"></param>        /// <returns></returns>        public static string EncryptPassword(string PasswordString, string PasswordFormat)        {            string EncryptPassword = "";            if (PasswordFormat == "SHA1")            {                EncryptPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");            }            else if (PasswordFormat == "MD5")            {                EncryptPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");                            }            return EncryptPassword;        }        #region BASE64 Encoding.by anzhiqiang add 2009-2-27        /// <summary>        /// Encode64        /// </summary>        /// <param name="Message"></param>        /// <returns></returns>        public string Encode64(string Message)        {            char[] Base64Code = new char[]              {                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',                  'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',                  'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',                  'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',                  '4', '5', '6', '7', '8', '9', '+', '/', '='              };            byte empty = (byte)0;            System.Collections.ArrayList byteMessage = new              System.Collections.ArrayList(System.Text.Encoding.Default.GetBytes              (Message));            System.Text.StringBuilder outmessage;            int messageLen = byteMessage.Count;            int page = messageLen / 3;            int use = 0;            if ((use = messageLen % 3) > 0)            {                for (int i = 0; i < 3 - use; i++)                    byteMessage.Add(empty);                page++;            }            outmessage = new System.Text.StringBuilder(page * 4);            for (int i = 0; i < page; i++)            {                byte[] instr = new byte[3];                instr[0] = (byte)byteMessage[i * 3];                instr[1] = (byte)byteMessage[i * 3 + 1];                instr[2] = (byte)byteMessage[i * 3 + 2];                int[] outstr = new int[4];                outstr[0] = instr[0] >> 2;                outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4);                if (!instr[1].Equals(empty))                    outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6);                else                    outstr[2] = 64;                if (!instr[2].Equals(empty))                    outstr[3] = (instr[2] & 0x3f);                else                    outstr[3] = 64;                outmessage.Append(Base64Code[outstr[0]]);                outmessage.Append(Base64Code[outstr[1]]);                outmessage.Append(Base64Code[outstr[2]]);                outmessage.Append(Base64Code[outstr[3]]);            }            return outmessage.ToString();        }        /// <summary>        /// Decode64        /// </summary>        /// <param name="Message"></param>        /// <returns></returns>        public string Decode64(string Message)        {            if (Message == null || Message == "")               {                return null;            }            string Base64Code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";            int page = Message.Length / 4;            System.Collections.ArrayList outMessage = new System.Collections.ArrayList(page * 3);            char[] message = Message.ToCharArray();            for (int i = 0; i < page; i++)            {                byte[] instr = new byte[4];                instr[0] = (byte)Base64Code.IndexOf(message[i * 4]);                instr[1] = (byte)Base64Code.IndexOf(message[i * 4 + 1]);                instr[2] = (byte)Base64Code.IndexOf(message[i * 4 + 2]);                instr[3] = (byte)Base64Code.IndexOf(message[i * 4 + 3]);                byte[] outstr = new byte[3];                outstr[0] = (byte)((instr[0] << 2) ^ ((instr[1] & 0x30) >> 4));                if (instr[2] != 64)                {                    outstr[1] = (byte)((instr[1] << 4) ^ ((instr[2] & 0x3c) >> 2));                }                else                {                    outstr[2] = 0;                }                if (instr[3] != 64)                {                    outstr[2] = (byte)((instr[2] << 6) ^ instr[3]);                }                else                {                    outstr[2] = 0;                }                outMessage.Add(outstr[0]);                if (outstr[1] != 0)                    outMessage.Add(outstr[1]);                if (outstr[2] != 0)                    outMessage.Add(outstr[2]);            }            byte[] outbyte = (byte[])outMessage.ToArray(Type.GetType("System.Byte"));            return System.Text.Encoding.Default.GetString(outbyte);        }        #endregion    }} 


[解决办法]
Base64不是加密,是编码,就是把二进制编码成可见字符,用于在网上传送。和原始二进制数据是Unicode,UTF-8,GB2312没有任何关系

算法都是固定的,任何语言都可以进行编码和解码,只不过有的标准类库提供,有的没有提供罢了。
[解决办法]
Base64编码啊。可以自己写一个的 a-Z 0-9 /= 什么的,也可以用别的东西。
这样来回编码/解码可以非常省劲的跨这个跨那个的。

读书人网 >C#

热点推荐