Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

在线急C#代码转PHP 自己写了一个 发现还是有出入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace study_test4
{
    class Program
    {
        static void Main(string[] args)
        {

            string s;                                //定义存储输入字符串变量
            int L;                                    //定义存储字符串长度的变量


            /*对输入的字符串完成加密*/
            Console.WriteLine("please input a string:");
            s = Console.ReadLine();           //输入字符串

            L = s.Length;                         //计算字符串长度
            char[] arr=new char[L];           //定义一个字符数组,存储加密后的字符串
            arr[0] = s[L - 1];                    //把输入字符串的末尾字符存储到字符数组的第一位置

            for (int i = 1; i < L; i++)
               arr[i]= (char)((int)(s[i-1])+3);/*从输入字符串的第二个字符起到输入字符串的倒数第二个字符,

                                                        *每个字符依次加三,然后存储到字符数组剩余的位置*/

            Console.Write("加密后:");
 
            for (int i = 0; i < L; i++)
            Console.Write("{0}",arr[i]);      //输出加密后的字符

            /*对加密的字符串进行解密*/
            char[] arr_decode = new char[L];
            arr_decode[L - 1] = arr[0];
            for(int i = 1; i < L; i++)
                arr_decode[i-1] = (char)((int)(arr[i])-3);

            Console.Write("解密后:");
    
            for (int i = 0; i < L; i++)
                Console.Write("{0}", arr_decode[i]);//输出加密后的字符
               
            Console.ReadLine();                          //让控制台停在显示界面
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
function testEncrypt($s)
{
    $len = mb_strlen($s);
    $arr[0] = $s[$len-1];
    $ret = '';
    for ($i = 1; $i < $len; $i++){
        $arr[$i] = chr(ord($s[$i-1])+3);
    }
    for ($i = 0; $i < $len; $i++) {
        $ret .= $arr[$i];
    }
    var_dump($ret);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...