请教.NET高手们一个加密算法的问题

mark560 2014-11-29 08:13:41
各位大神,小弟只会PHP,看不懂.NET,有一段加密算法能否帮我解释一下。

public static string MakePassword(int len)
{
string strPwChar = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string strRe = "";
int iRandNum;
Random rnd = new Random();
for (int i = 0; i < len; i++)
{
iRandNum = rnd.Next(strPwChar.Length);
strRe += strPwChar[iRandNum];
}
return strRe;
}


public static int GetJStr(string str)
{
if (str.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(str)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}

}

public 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.");
}
}

public static string GetEnStr(string str)
{
string temp = "";
for (int i = 0; i < str.Length; i++)
{
temp += Chr(GetJStr(str.ToString()) - 1);
}
temp +=MakePassword(4);
return new SymmetricMethod().Encrypto(temp);
}



运行最后一个函数GetEnStr('123456')的话,会返回字符串A04E23ixO1kTyDmdvcgeAg==,不知道咋整的,望大神指教,谢谢谢谢谢谢!
...全文
137 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mark560 2014-11-29
  • 打赏
  • 举报
回复
引用 7 楼 github_22161131 的回复:
首先,你贴的代码里面有错, temp += Chr(GetJStr(str.ToString()) - 1); 应该是 temp += Chr(GetJStr(str[i].ToString()) - 1); 才对 其次,这里的SymmetricMethod().Encrypto能baidu搜到,相当于用了通用的密码,这个加密十分不安全,可以随便解密。 你的A04E23ixO1kTyDmdvcgeAg==解密回来是012345zuRU 这个算法就是把123456,先变成012345,然后拼上4个随机字符,然后用了一个通用的AES加密的。
感谢!
winnowc 2014-11-29
  • 打赏
  • 举报
回复
首先,你贴的代码里面有错, temp += Chr(GetJStr(str.ToString()) - 1); 应该是 temp += Chr(GetJStr(str[i].ToString()) - 1); 才对 其次,这里的SymmetricMethod().Encrypto能baidu搜到,相当于用了通用的密码,这个加密十分不安全,可以随便解密。 你的A04E23ixO1kTyDmdvcgeAg==解密回来是012345zuRU 这个算法就是把123456,先变成012345,然后拼上4个随机字符,然后用了一个通用的AES加密的。
ymj921128 2014-11-29
  • 打赏
  • 举报
回复
引用 5 楼 mark560 的回复:
[quote=引用 4 楼 ymj921128 的回复:] 帮你加了注释
十分感谢!!![/quote] ok了就麻烦结下帖吧
mark560 2014-11-29
  • 打赏
  • 举报
回复
引用 4 楼 ymj921128 的回复:
帮你加了注释
 public static string MakePassword(int len)//生成长度为len的随机密码
        {
            string strPwChar = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string strRe = "";
            int iRandNum;
            Random rnd = new Random();
            for (int i = 0; i < len; i++)
            {
                iRandNum = rnd.Next(strPwChar.Length);
                strRe += strPwChar[iRandNum];
            }
            return strRe;
        }


        public static int GetJStr(string str)//将长度超过1的字符串中的所有字符编码为一个字节序列
        {
            if (str.Length == 1)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(str)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("Character is not valid.");
            }

        }

        public 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.");
            }
        }

        public static string GetEnStr(string str)
        {
            string temp = "";//存放密码
            for (int i = 0; i < str.Length; i++)
            {
                temp += Chr(GetJStr(str.ToString()) - 1);//将字符串字节序列-1,然后重新编码成字符串给到temp
            }
            temp += MakePassword(4);//temp加上4位自动生成的密码
            return new SymmetricMethod().Encrypto(temp);//调用SymmetricMethod类中的方法再加密(这个是你自己写的加密算法)
        }
十分感谢!!!
ymj921128 2014-11-29
  • 打赏
  • 举报
回复
帮你加了注释
 public static string MakePassword(int len)//生成长度为len的随机密码
        {
            string strPwChar = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string strRe = "";
            int iRandNum;
            Random rnd = new Random();
            for (int i = 0; i < len; i++)
            {
                iRandNum = rnd.Next(strPwChar.Length);
                strRe += strPwChar[iRandNum];
            }
            return strRe;
        }


        public static int GetJStr(string str)//将长度超过1的字符串中的所有字符编码为一个字节序列
        {
            if (str.Length == 1)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(str)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("Character is not valid.");
            }

        }

        public 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.");
            }
        }

        public static string GetEnStr(string str)
        {
            string temp = "";//存放密码
            for (int i = 0; i < str.Length; i++)
            {
                temp += Chr(GetJStr(str.ToString()) - 1);//将字符串字节序列-1,然后重新编码成字符串给到temp
            }
            temp += MakePassword(4);//temp加上4位自动生成的密码
            return new SymmetricMethod().Encrypto(temp);//调用SymmetricMethod类中的方法再加密(这个是你自己写的加密算法)
        }
mark560 2014-11-29
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
Encrypto这个函数内部调用了类似Base64Encode之类的方法,做了base64编码。
求版主帮助
mark560 2014-11-29
  • 打赏
  • 举报
回复
谢谢,我直接把字符串作了base64 encode但得出来的和上述例子中返回值不一样。 我看加密算法里好像是把输入的字符串往ascii码都往前移了一位是吗?不好意思我太菜了。。。。
threenewbee 2014-11-29
  • 打赏
  • 举报
回复
Encrypto这个函数内部调用了类似Base64Encode之类的方法,做了base64编码。

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧