Translate

Thursday 24 January 2013

“Decimal not displaying in SUM” Issue in DevExpress XtraReport.

Hi.. I AM back again..  :).. This time with a concern which i faced at my work place while working on DevXpress XtraReport tool.

XtraReport is a tool used for generating reports. The Issue was: When i apply the SUM function of XtraReport on Some column data then i was facing concerns of my trailing zeroes getting compressed and for a finance domain client: Rs25,000.00 looks better than Rs25,000.. He He..

The Summary Functions provided by XtraReport includes: Aggregate,Sum,DCount(Distinct Count) etc..

For Eg: I have applied a SUM function on a label , bound to print Sum of a Column that has No.Of Shares.
The data at some instance of time in column is: 2.00,3.00,32.08,29.02,123.9

The Sum Function Gives me : 190 instead of 190.00 as its default behavior considers this number as integer and eats the Trailing zeroes.. But If the Output is Something of sort of 125.00256 then it displayes it with correct decimals But if the output is 2563.36200, then also it displays it like 2563.362.

After prolonged discussion on DevExpress XtraReport on Various forums.. I found the solution..

Steps:
Change the Summary_Function type of the XRLabel to CUSTOM:

Now Write Code on the following event:
And write the following Code:


private void xrLabel10_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {           
            int decimalCount = 0; 
            double sumOfValues = 0;
            string formatString = String.Empty;
            foreach (var value in e.CalculatedValues)
            {
                if (Convert.ToString(value) != String.Empty)   //check for null values
                {
                    sumOfValues = sumOfValues + Convert.ToDouble(value);   //calculate the sum of values
                      if (Convert.ToString(value).Split('.').Length == 2)  //check for only one decimal Place in data
                          if (Convert.ToString(value).Split('.')[1].Length > decimalCount) //calculating number of decimal places
                                decimalCount = Convert.ToString(value).Split('.')[1].Length;                   
                }
            }
            formatString = formatString.PadRight(decimalCount, '0');     
            e.Result =sumOfValues.ToString("#,##0." + formatString);  //thousand separator + decimal Count
            e.Handled = true;
        }


Hence i over-ride the default behavior of the Method to Solve this Issue.. And beleive me.. It Solved many Similar Concerns for my Client...


Wednesday 23 January 2013

RSA Cryptographic Algorithm- Implemented in C#

Symmetric Encryption Technique: PLAYFAIR:

As wiki says:

The Playfair cipher or Playfair square is a manual symmetric encryption technique and was the first literal digraph substitution cipher. The scheme was invented in 1854 by Charles Wheatstone, but bears the name of Lord Playfair who promoted the use of the cipher.
The technique encrypts pairs of letters (digraphs), instead of single letters as in the simple substitution cipher and rather more complex Vigenère cipher systems then in use. The Playfair is thus significantly harder to break since the frequency analysis used for simple substitution ciphers does not work with it. Frequency analysis can still be undertaken, but on the 600[1] possible digraphs rather than the 26 possible monographs. The frequency analysis of digraphs is possible, but considerably more difficult – and it generally requires a much larger ciphertext in order to be useful.

See:wiki-Playfair_cipher for details

I tried it at my end at my college times for network security subject. The C# code for the same is below:



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

namespace Playfair
{
   
    class Program
    {
        public static char[,] matrix = new char[5, 5];
        public static string cipher = "";
        static void Main(string[] args)
        {
            create_matrix(); // matrix created
          
           
            Console.WriteLine("enter plaintext:  ");

            int p = 0;
            string plaintext = Console.ReadLine();

            string nwplain = plaintext.Replace(" ", "");
            plaintext = nwplain;

            int size = plaintext.Length;



            plaintext = test(size, plaintext);



            if (size % 2 == 1)
            {
                plaintext = plaintext + "x";
            }
            double q = (double)size / 2;
            int s = Convert.ToInt32(Math.Ceiling(q));
            char[,] qw = new char[s, 2];
            for (int i = 0; i <= size - 1; i = i + 2)
            {
                if (plaintext.Substring(i, 1) == "i")
                {
                    qw[p, 0] = 'j';
                }
                else
                {
                    qw[p, 0] = Convert.ToChar(plaintext.Substring(i, 1));
                }

                if (plaintext.Substring(i + 1, 1) != null)
                {
                    if (plaintext.Substring(i + 1, 1) == "i")
                    {
                        qw[p, 1] = 'j';
                    }
                    else
                    {
                   qw[p, 1] = Convert.ToChar(plaintext.Substring(i + 1, 1));
                    }
                }
                p = p + 1;
            }

            for (int i = 0; i < p; i++)
            {
                string posi = pos(qw[i, 0]);
                string[] str = posi.Split('.');
                int[] first = new int[2];
                int[] second = new int[2];
                first[0] = Convert.ToInt32(str[0]);// x pos
                first[1] = Convert.ToInt32(str[1]);// y pos
                posi = pos(qw[i, 1]);
                string[] str1 = posi.Split('.');
                second[0] = Convert.ToInt32(str1[0]);// x pos
                second[1] = Convert.ToInt32(str1[1]);// y pos

                if (first[0] == second[0])// same row
                {
                    if (first[1] == 4)
                    {
                        first[1] = -1;
                    }
                    if (second[1] == 4)
                    {
                        second[1] = -1;
                    }
                    cipher = cipher + matrix[(first[0]), (first[1] + 1)];
                    cipher = cipher + matrix[(second[0]), (second[1] + 1)];

                }
                else if (first[1] == second[1])// same column
                {
                    if (first[0] == 4)
                    {
                        first[0] = -1;
                    }
                    if (second[0] == 4)
                    {
                        second[0] = -1;
                    }
                    cipher = cipher + matrix[(first[0] + 1), (first[1])];
                    cipher = cipher + matrix[(second[0] + 1), (second[1])];
                }
                else
                {
                    cipher = cipher + matrix[first[0], second[1]];
                    cipher = cipher + matrix[second[0], first[1]];


                }
            }
            Console.Write("Cipher Text:");

            Console.WriteLine(cipher.ToUpper());
            string df = Console.ReadLine();

        }// end main


        static string pos(char a)
        {
            string w="";
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (a == matrix[i, j])
                {
                     w = i.ToString() +"."+ j.ToString();
                     break;
                  
                }
               
            }
        }
        return (w);
        }

        static string test(int size, string pt) //doublet
        {
            ch:
            for (int i = 0; i <=size - 1; i = i + 2)
            {
                if (pt.Substring(i, 1) == pt.Substring(i + 1, 1))
                {
                   pt= pt.Insert(i+1, "x");
                    goto ch;
               
                }
           
            }
          
            return (pt);
        }


        static void create_matrix()
        {
            string k1 = "";
            Console.WriteLine("Enter Key (alphabetic): ");
            string key = Console.ReadLine();
            string reference = "abcdefghjklmnopqrstuvwxyz";
            int size = key.Length;
            string[] key_arry = new string[size];
            for (int i = 0; i < size; i++)
            {
                key_arry[i] = key.Substring(i, 1);
            }

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (i != j)
                    {
                        if (key_arry[i] == "i")
                        {
                            key_arry[i] = "j";
                        }
                        if (key_arry[i] == key_arry[j])
                        {
                            key_arry[j] = "1";
                        }
                    }
                }

            }
            for (int i = 0; i < size; i++)
            {
                k1 = k1 + key_arry[i].ToString();
            }
            k1 = k1.Replace('i', 'j');
            k1 = k1.Replace('1', ' ');
            k1 = k1.Replace(" ", "");
            foreach (string c in key_arry)
            {
                reference = reference.Replace(c, "");

            }
            int si = 0;
            int p=0;
            int len = k1.Length;
            for (int i = 0; i < 5; i++)
            {
               
                for (int j = 0; j < 5; j++)
                {
                    if (si < size)
                    {
                        if (key_arry[si] != "" && key_arry[si] != "1")
                        {                           
                                matrix[i, j] = Convert.ToChar(key_arry[si]);
                          
                            si++;
                        }
                        else
                        {
                            si++;
                            j--;
                        }
                    }
                    else
                    {

                        si++;
                        if (si >= size)
                        {
            matrix[i, j] = Convert.ToChar(reference.Substring(p, 1));
                            j++;
                            p++;
                        }
                        j--;
                    }
                }
              
            }
            Console.WriteLine("THE PLAYFAIR MATRIX IS:");
            for (int i = 0; i < 5; i++)
            {
                Console.Write("\n");
                for (int j = 0; j < 5; j++)
                {
                    Console.Write(" ");

                    Console.Write(matrix[i, j]);

                }
            }
            Console.WriteLine("");

        }


    }// end class program
}//end namespace



The Output:

hope It Helps U...

Public Key Encryption- RSA Cryptographic Algorithm Implemented in C#

RSA Cryptographic Algorithm:

As per wiki-

RSA is an algorithm for public-key cryptography that is based on the presumed difficulty of factoring large integers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described the algorithm in 1977. Clifford Cocks, an English mathematician, had developed an equivalent system in 1973, but it was classified until 1997.
A user of RSA creates and then publishes the product of two large prime numbers, along with an auxiliary value, as their public key. The prime factors must be kept secret. Anyone can use the public key to encrypt a message, but with currently published methods, if the public key is large enough, only someone with knowledge of the prime factors can feasibly decode the message.[1] Whether breaking RSA encryption is as hard as factoring is an open question known as the RSA problem.

See:wiki-RSA_(algorithm) for details

I have implemented it in C#, during my college times, and took help from net for a piece of code.  The COde is Below:


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

namespace RSA
{
    class Program
    {
        static void Main(string[] args)
        {
            long plaintext=0,ciphertext=0;
            long plaintext1 = 0, ciphertext1 = 0;
            Random rnd = new Random();
       long p = rnd.Next(1, 50);
            long q
        check:  q = rnd.Next(1, 70);
            if (p == q)
                goto check;

            long gcd = gcd_check(p, q);

            p = p / gcd;
            q = q / gcd;

            long n = p * q;
            long fi = (p - 1)*(q - 1);
            long e, gd;
            do
            {
                e = rnd.Next(Convert.ToInt32(fi));
                gd = gcd_check(fi, e);
            }
            while (gd != 1);

            long d = modinv(e, fi);
            long[] pub = new long[2];
            long[] pri = new long[2];
            pub[0] = e; pub[1] = n;
            pri[0] = d; pri[1] = n;           
            char c = 'y';
            while (c == 'y')
            {
                Console.WriteLine("Press E to encrypt and D to decrypt:");
                string choice = Console.ReadLine();
                switch (choice.ToUpper())
                {
                    case "E": Console.Write("enter Plain Text:(Numeric)< ");
                      Console.WriteLine(n);
                      plaintext = Convert.ToInt64(Console.ReadLine());
                     ciphertext =Convert.ToInt64(Math.Pow(Convert.ToDouble(plaintext), Convert.ToDouble(e)) % n);
                       Console.WriteLine("Ciphertext=");
                       Console.WriteLine(ciphertext);
                        break;
                    case "D": Console.WriteLine("enter Cipher Text:(Numeric)");
                        ciphertext1 = Convert.ToInt64(Console.ReadLine());
                        plaintext1 =Convert.ToInt64(Math.Pow(Convert.ToDouble(ciphertext1), Convert.ToDouble(d)) % n);
                        Console.Write("plaintext=");
                        Console.WriteLine(plaintext1);
                        break;
                    default: Console.WriteLine("wrong entry..!!");
                        break;


                }
                Console.WriteLine("press y to continue..:");
                c =Convert.ToChar( Console.ReadLine());
            }
        }
        static long gcd_check(long a, long b)
        {
            long c;
            if (a < b)
            {
                c = a;
                a = b;
                b = c;
            }
            while (true)
            {
                c = a % b;
                if (c == 0)
                    return b;
                a = b;
                b = c;
            }
        }

       static long modinv( long u,  long v)
          {
        long inv, u1, u3, v1, v3, t1, t3, q;
        long iter;
        /* Step X1. Initialise */
        u1 = 1;
        u3 = u;
        v1 = 0;
        v3 = v;
        /* Remember odd/even iterations */
        iter = 1;
        /* Step X2. Loop while v3 != 0 */
        while (v3 != 0)
        {
            /* Step X3. Divide and "Subtract" */
            q = u3 / v3;
            t3 = u3 % v3;
            t1 = u1 + q * v1;
            /* Swap */
            u1 = v1; v1 = t1; u3 = v3; v3 = t3;
            iter = -iter;
         }
                /* Make sure u3 = gcd(u,v) == 1 */
                if (u3 != 1)
                    return 0;   /* Error: No inverse exists */
                /* Ensure a positive result */
                if (iter < 0)
                    inv = v - u1;
                else
                    inv = u1;
                return inv;
           }  /// end modinv

       
       
    }
}
----------------------------------------------------