c#加密和解密

ant*_*009 1 c# encryption

C#2005我正在使用简单的加密和解密来获取IP地址.远程服务器上的应用程序将加密IP地址,客户端将对其进行解密.但是,当客户端解密IP时,我只返回一些IP地址.其余的都是垃圾.之前:123.456.78.98之后:fheh&^ G.78.98

非常感谢,

 /// Encrypt the SIP IP address in the remote server
        private void encryptSIP_IP(string sip_ip)
        {
            TripleDESCryptoServiceProvider encrypt = new TripleDESCryptoServiceProvider();

        /// Private key
        byte[] key = { 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, 0};

        encrypt.Key = key;
        byte[] byteSIP = System.Text.Encoding.Default.GetBytes(sip_ip);

            ICryptoTransform encryptor = encrypt.CreateEncryptor();
             byte[] encrypted_sip = encryptor.TransformFinalBlock(byteSIP, 0, byteSIP.Length);



/// This will decrypt in the client application
        private void decryptSIP_IP(byte[] encrypted_sip)
        {
            TripleDESCryptoServiceProvider decrypt = new TripleDESCryptoServiceProvider();
            /// Private key
            byte[] key = { 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, 0 };
            decrypt.Key = key;
            ICryptoTransform decryptor = decrypt.CreateDecryptor();

            byte[] original = decryptor.TransformFinalBlock(encrypted_sip, 0, encrypted_sip.Length);
            string ip = System.Text.Encoding.Default.GetString(original);
        }
        }
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 8

除了一个键,你需要一个初始化向量(8个字节为您的情况):

// To Encrypt
encrypt.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

// Use same IV to decrypt
decrypt.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Run Code Online (Sandbox Code Playgroud)