Who encryption and decryption of C language library ! !

Category: VC/MFC -> Resources Author: icoober Date: 2008-08-07 18:32:24
 
icoober
2008-08-07 18:32:24
RT, can not send copies to me, thank you ! ! ! [email protected]
qq122068352
2008-08-07 18:41:24
windows   vista later versions there is a corresponding api encryption function

Windows   Cryptography   API:   Next   Generation
jff19850202
2008-08-07 18:56:23
I have one , RC4 encryption algorithm, is written in C # , you can change to change the ( stream encryption algorithm )

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

/****************************************
    *  :Encrypt
    *  : , RC4
    *  : (yshen)
    *  :2008/8/23
    * *************************************/
namespace net.yshen.NetKit.Common
{
    /// <summary>
    /// 
    /// </summary>
    class rc4_key
    {
        /// <summary>
        /// 
        /// </summary>
        public byte[] state = new byte[256];
        /// <summary>
        ///  X
        /// </summary>      
        public byte x;
        /// <summary>
        ///  Y
        /// </summary>
        public byte y;
    }
    /// <summary>
    /// 
    /// </summary>
    public class Rc4Crypt
    {
        /// <summary>
        /// 
        /// </summary>
        private rc4_key key = new rc4_key();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyData"> </param>
        /// <param name="keyLen"> </param>
        public Rc4Crypt(byte[] keyData, int keyLen)
        {
            // ,
            prepare_key(keyData, keyLen);
        }
        /// <summary>
        ///  X,Y
        /// </summary>
        /// <param name="x">X</param>
        /// <param name="y">Y</param>
        private void swap_byte(ref byte x, ref byte y)
        {
            byte temp = x;
            x = y;
            y = temp;
        }

        /// <summary>
        ///  ,
        /// </summary>
        /// <param name="key_data_ptr"> </param>
        /// <param name="key_data_len"> </param>
        private void prepare_key(byte[] key_data_ptr, int key_data_len)
        {
            byte index1;
            byte index2;
            short counter;

            for (counter = 0; counter < 256; counter++)
            {
                key.state[counter] = (byte)counter;
            }
            key.x = 0;
            key.y = 0;
            index1 = 0;
            index2 = 0;
            for (counter = 0; counter < 256; counter++)
            {
                index2 =(byte)((key_data_ptr[index1] + key.state[counter] + index2) % 256);
                swap_byte(ref key.state[counter], ref key.state[index2]);
                index1 = (byte)((index1 + 1) % key_data_len);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer_ptr"> </param>
        /// <param name="buffer_ptr"> </param>
        /// <param name="buffer_len"> </param>
        /// <returns>true or false</returns>
        public void rc4(ref byte[] buffer_ptr,int index, int buffer_len)
        {
            byte x;
            byte y;
            byte xorIndex;
            uint counter;

            x = key.x;
            y = key.y;
            for (counter = 0; counter < buffer_len; counter++)
            {
                x = (byte)((x + 1) % 256);
                y = (byte)((key.state[x] + y) % 256);
                swap_byte(ref key.state[x], ref key.state[y]);
                xorIndex = (byte)((key.state[x] + key.state[y]) % 256);
                buffer_ptr[counter+index] ^= key.state[xorIndex];
            }
            key.x = x;
            key.y = y;
        }
    }
}