首页 新闻 赞助 找找看

C#关于System.Text.ASCIIEncoding 加密后怎么解密问题?

1
悬赏园豆:50 [待解决问题]
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string s = new Authentication().EncryptionReview("abcdefg");
    }



    //密码验证
    public class Authentication
    {
        /// <summary>
        /// 加密签字密码,和OA用户名密码加密机制一样
        /// </summary>
        /// <param name="CodeStr"></param>
        /// <returns></returns>
        public string EncryptionReview(string CodeStr)
        {
            double NewCode = 1;
            int CodeLen = 30;
            int CodeSpace = CodeLen - CodeStr.Length;
            int Been = 0;
            string returnstring = "";

            for (int i = 1; i <= CodeSpace; i++)
            {
                CodeStr = CodeStr + Chr(21);
            }

            for (int i = 1; i <= CodeLen; i++)
            {
                Been = CodeLen + Convert.ToInt32(Asc(CodeStr.Substring(i - 1, 1))) * i;
                NewCode = NewCode * Been;

            }
            double NewCodes = double.Parse(NewCode.ToString("0.00"));
            string NewCodee = "";
            NewCode = 0;

            for (int i = 1; i <= Convert.ToString(NewCodes).Length; i++)
            {
                if (Convert.ToString(NewCodes).Length - i > 1) { NewCodee = NewCodee + EncryptionReviewa(Convert.ToString(NewCodes).Substring(i - 1, 3)); }
                else if (Convert.ToString(NewCodes).Length - i == 1) { NewCodee = NewCodee + EncryptionReviewa(Convert.ToString(NewCodes).Substring(i - 1, 2)); }
                else if (Convert.ToString(NewCodes).Length - i == 0) { NewCodee = NewCodee + EncryptionReviewa(Convert.ToString(NewCodes).Substring(i - 1, 1)); }
            }
            for (int i = 20; i <= Convert.ToString(NewCodee).Length - 18; i = i + 2)
            {
                returnstring = returnstring + NewCodee.Substring(i - 1, 1);
            }
            return returnstring;

        }
        private string EncryptionReviewa(string PassStr)
        {
            string CfsCode = "";
            for (int i = 1; i <= PassStr.Length; i++)
            {
                CfsCode = CfsCode + Asc(PassStr.Substring(i - 1, 1));
            }
            return int.Parse(CfsCode).ToString("X");
        }
        private static int Asc(string character)
        {
            if (character.Length == 1)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("Character is not valid.");
            }

        }
        private static string Chr(int asciiCode)
        {
            if (asciiCode >= 0 && asciiCode <= 255)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                string strCharacter = asciiEncoding.GetString(byteArray);
                return (strCharacter);
            }
            else
            {
                throw new Exception("ASCII Code is not valid.");
            }
        }
    }
}
cheng_doom的主页 cheng_doom | 初学一级 | 园豆:30
提问于:2014-04-09 23:19
< >
分享
所有回答(3)
0

繒經最羙 | 园豆:178 (初学一级) | 2014-04-10 08:37
0

Encoding不是加密`是编码,编码和加密是完全的两个东西.

你去查下怎么算http://baike.baidu.com/link?url=62gCnm5dgWdJX8wmOUmbAUToXrX_VTvsOc1JBAV-W3S7dJua0vz6J5M6tMrYep-W

至于Encoding只要Decoding就行了

吴瑞祥 | 园豆:29449 (高人七级) | 2014-04-10 09:46

怎么根据现有的加密方式在反解密了。

支持(0) 反对(0) cheng_doom | 园豆:30 (初学一级) | 2014-05-06 14:25
0

int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];

我可不可以理解为这一步是加密的?

解码的方式就是反过来做一次,

加密是:GetBytes(Char[])/GetBytes(string)

解码是:GetChars(Byte[])

seaconch | 园豆:4823 (老鸟四级) | 2014-04-10 09:51

怎么根据现有的加密方式在反解密了。

支持(0) 反对(0) cheng_doom | 园豆:30 (初学一级) | 2014-05-06 14:24

@cheng_doom: 有加密方式就会有相应的解密方式.要看你用的什么加密方法

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2014-05-06 14:25

@吴瑞祥: 就是我贴出的代码啊。

支持(0) 反对(0) cheng_doom | 园豆:30 (初学一级) | 2014-05-06 14:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册