Register for your free account! | Forgot your password?

You last visited: Today at 00:22

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Reworked CO2 Cipher

Discussion on Reworked CO2 Cipher within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

Reply
 
Old   #1


 
CptSky's Avatar
 
elite*gold: 0
Join Date: Jan 2008
Posts: 1,443
Received Thanks: 1,175
Reworked CO2 Cipher

I made a new version of the CO2 cipher used by the auth server and the game server (5017 and before). I don't use any pointers in this version. The class is based on the dotNet AsymmetricAlgorithm class.

If you use this class in your projects, please mention who is the autor... Thanks.

Code:
// * ***************************************
// *       _                 _ _
// *      | |               (_) |
// *      | |     ___   __ _ _| | __
// *      | |    / _ \ / _` | | |/ /
// *      | |___| (_) | (_| | |   < _
// *      \_____/\___/ \__, |_|_|\_(_)
// *                    __/ |
// *                   |___/
// *
// *       Copyright (C) 2010 - 2011
// *
// * ***************************************
// *              CREDITS
// * ***************************************
// * Originally created by CptSky @ e*pvp, Copyright (C) 2010-2011,
// * Logik, All rights reserved.
// *  
// * ***************************************
// *              SPECIAL THANKS
// * ***************************************
// * Sparkie (Unknownone @ e*pvp)
// * 
// * ***************************************

using System;
using System.Security.Cryptography;

namespace Logik.Security.Cryptography
{
    /// <summary>
    /// Conquer Online Server Asymmetric Cipher
    /// </summary>
    public class COSAC : AsymmetricAlgorithm
    {
        const Int32 COSAC_KEY_SIZE = 4096;

        protected Byte[] IVValue;
        protected Byte[] KeyValue;
        protected UInt16 EncryptCounter;
        protected UInt16 DecryptCounter;

        protected String KeyExchangeAlgorithmValue;
        protected String SignatureAlgorithmValue;

        /// <summary>
        /// Create a new COSAC instance with a key size of 4096.
        /// </summary>
        public COSAC()
            : base()
        {
            this.KeyExchangeAlgorithmValue = "TQ_DIGITAL_CONQUER_ONLINE_KEY_EXCHANGE_ALGORITHM";
            this.SignatureAlgorithmValue = "TQ_DIGITAL_CONQUER_ONLINE_SERVER_ASYMMETRIC_CIPHER";

            this.LegalKeySizesValue = new KeySizes[] { new KeySizes(COSAC_KEY_SIZE, COSAC_KEY_SIZE, 0) };
            this.KeySizeValue = COSAC_KEY_SIZE;
        }

        ~COSAC()
        {
            KeyValue = null;
            IVValue = null;

            KeyExchangeAlgorithmValue = null;
            SignatureAlgorithmValue = null;
        }

        /// <summary>
        /// Releases the unmanaged resources used by the AsymmetricAlgorithm class and optionally releases the managed resources.
        /// </summary>
        protected override void Dispose(Boolean disposing)
        {
            Clear();
            if (disposing)
            {
                KeyValue = null;
                IVValue = null;

                KeyExchangeAlgorithmValue = null;
                SignatureAlgorithmValue = null;
            }
        }

        /// <summary>
        /// Reconstructs an AsymmetricAlgorithm object from an XML string. (Not Implemented!)
        /// </summary>
        public override void FromXmlString(String xmlString) { throw new NotImplementedException(); }

        /// <summary>
        /// Creates and returns an XML string representation of the current AsymmetricAlgorithm object. (Not Implemented!)
        /// </summary>
        public override String ToXmlString(Boolean includePrivateParameters) { throw new NotImplementedException(); }

        /// <summary>
        /// Gets the name of the key exchange algorithm.
        /// </summary>
        public override String KeyExchangeAlgorithm { get { return KeyExchangeAlgorithmValue; } }

        /// <summary>
        /// Gets the size, in bits, of the key modulus used by the asymmetric algorithm.
        /// </summary>
        public override Int32 KeySize { get { return base.KeySize; } }

        /// <summary>
        /// Gets the key sizes that are supported by the asymmetric algorithm.
        /// </summary>
        public override KeySizes[] LegalKeySizes { get { return base.LegalKeySizes; } }

        /// <summary>
        /// Gets the name of the signature algorithm.
        /// </summary>
        public override String SignatureAlgorithm { get { return SignatureAlgorithmValue; } }

        /// <summary>
        /// Generates an initialization vector (IV) to use for the algorithm.
        /// CO2(P: 0x13FA0F9D, G: 0x6D5C7962)
        /// </summary>
        public void GenerateIV(Int32 P, Int32 G)
        {
            IVValue = new Byte[COSAC_KEY_SIZE / 8];
            Int16 K = COSAC_KEY_SIZE / 16;

            Byte[] PArray = new Byte[4] { (Byte)(P), (Byte)(P >> 8), (Byte)(P >> 16), (Byte)(P >> 24) };
            Byte[] GArray = new Byte[4] { (Byte)(G), (Byte)(G >> 8), (Byte)(G >> 16), (Byte)(G >> 24) };
            for (Int16 i = 0; i < K; i++)
            {
                IVValue[i + 0] = PArray[0];
                IVValue[i + K] = GArray[0];
                PArray[0] = (Byte)((PArray[1] + (Byte)(PArray[0] * PArray[2])) * PArray[0] + PArray[3]);
                GArray[0] = (Byte)((GArray[1] - (Byte)(GArray[0] * GArray[2])) * GArray[0] + GArray[3]);
            }
        }

        /// <summary>
        /// Generates a key (Key) to use for the algorithm and reset the encrypt counter.
        /// In Conquer Online: A = Token, B = AccountUID
        /// </summary>
        public void GenerateKey(Int32 A, Int32 B)
        {
            if (IVValue == null)
                throw new NullReferenceException("IV needs to be generated before generating the key!");

            KeyValue = new Byte[COSAC_KEY_SIZE / 8];
            Int16 K = COSAC_KEY_SIZE / 16;

            UInt32 tmp = 0;
            tmp = (UInt32)(A + B);

            Byte[] tmpKey1 = new Byte[4] { (Byte)(tmp >> 24), (Byte)(tmp >> 16), (Byte)(tmp >> 8), (Byte)(tmp) };
            tmpKey1[2] ^= 0x43;
            tmpKey1[3] ^= 0x21;

            for (SByte i = 0; i < 4; i++)
                tmpKey1[i] ^= (Byte)(A >> (24 - (8 * i)));

            tmp = (UInt32)((tmpKey1[0] << 24) + (tmpKey1[1] << 16) + (tmpKey1[2] << 8) + tmpKey1[3]);
            tmp *= tmp;

            Byte[] tmpKey2 = new Byte[4] { (Byte)(tmp >> 24), (Byte)(tmp >> 16), (Byte)(tmp >> 8), (Byte)(tmp) };

            for (Int16 i = 0; i < K; i++)
            {
                KeyValue[i + 0] = (Byte)(IVValue[i + 0] ^ tmpKey1[3 - (i % 4)]);
                KeyValue[i + K] = (Byte)(IVValue[i + K] ^ tmpKey2[3 - (i % 4)]);
            }
            EncryptCounter = 0;
        }

        /// <summary>
        /// Decrypts data with the COSAC algorithm.
        /// </summary>
        public void Decrypt(ref Byte[] rgb)
        {
            if (rgb == null)
                throw new NullReferenceException("Buffer can't be null!");

            Int16 K = COSAC_KEY_SIZE / 16;
            for (Int32 i = 0; i < rgb.Length; i++)
            {
                rgb[i] ^= (Byte)0xAB;
                rgb[i] = (Byte)(rgb[i] >> 4 | rgb[i] << 4);
                if (KeyValue != null)
                {
                    rgb[i] ^= (Byte)(KeyValue[(Byte)(DecryptCounter & 0xFF) + 0]);
                    rgb[i] ^= (Byte)(KeyValue[(Byte)(DecryptCounter >> 8) + K]);
                }
                else if (IVValue != null)
                {
                    rgb[i] ^= (Byte)(IVValue[(Byte)(DecryptCounter & 0xFF) + 0]);
                    rgb[i] ^= (Byte)(IVValue[(Byte)(DecryptCounter >> 8) + K]);
                }
                DecryptCounter++;
            }
        }

        /// <summary>
        /// Encrypts data with the COSAC algorithm.
        /// </summary>
        public void Encrypt(ref Byte[] rgb)
        {
            if (rgb == null)
                throw new NullReferenceException("Buffer can't be null!");

            Int16 K = COSAC_KEY_SIZE / 16;
            for (Int32 i = 0; i < rgb.Length; i++)
            {
                rgb[i] ^= (Byte)0xAB;
                rgb[i] = (Byte)(rgb[i] >> 4 | rgb[i] << 4);
                if (IVValue != null)
                {
                    rgb[i] ^= (Byte)(IVValue[(Byte)(EncryptCounter & 0xFF) + 0]);
                    rgb[i] ^= (Byte)(IVValue[(Byte)(EncryptCounter >> 8) + K]);
                }
                EncryptCounter++;
            }
        }

        /// <summary>
        /// Reset the decrypt and the encrypt counters.
        /// </summary>
        public void ResetCounter() { DecryptCounter = 0; EncryptCounter = 0; }
    }
}
CptSky is offline  
Thanks
3 Users
Reply


Similar Threads Similar Threads
Atlantica Online Bot Package / Reworked
08/07/2011 - Atlantica Online - 61 Replies
This thread is ment for the Reworked Atlantica Online Bot done by me TheOnlyOne. If everything wents right, its getting Released somewhere in the next week.
[REL] Cipher encryption class (PHP)
12/27/2009 - CO2 PServer Guides & Releases - 6 Replies
Exactly as the title says, any questions will be answered and a thanks would be appreciated. #cipher.php <?php require_once 'PEAR.php'; class Crypt_Blowfish
[Intermediate] Creating a strong but simple cipher
08/31/2008 - CO2 Programming - 9 Replies
Basically, here's the idea, we have a 'key' that contains every value a byte supports (0 to 255). When you encrypt a byte for instance 171 (0xAB) it creates an "x" using the first 4 bits of the byte, and "y" using the last for bits of the byte Value = 171 (0xAB) X = 10 (0xA) Y = 11 (0xB) Then in the output of the encrypt routine, it it'll fill that index as Key Here's an illustration to make it simpler; http://img120.imageshack.us/img120/3282/cipheran4 .gif
[Intermediate] Creating a picture cipher
08/27/2008 - CO2 Programming - 0 Replies
This cipher relies on the 'hybrid cipher' which can be found here; http://www.elitepvpers.com/forum/co2-programming/1 60922-intermediate-creating-strong-but-simple-ciph er.html Include the library (dll) attached as a reference to a .NET project and then add "using PictureCryptLibrary;" or whatever it is to use a namespace in the .NET language of your choice. // enc PictureCrypt.Encrypt(encfile, outputfile, cipherkey);



All times are GMT +1. The time now is 00:22.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.