Sunday, January 22, 2017

Cryptography in a minute! (how to encrypt\decrypt data of Wired or Wireless)


Cryptography is the art and science of secure communication. “kryptos” is Greek word, which means ‘secret writing’.

Cryptography is not a modern invention nor because of dawn of computers, in fact its almost 3 or 4 thousand years old practice of communicating securely. Kings as part of warfare or strategy were sending messages from one king to another in secret writing so that his\her enemy shouldn’t able to read the messages by capturing the messenger. Secret writing like, write message in hidden ink, substitution cipher (with shift of 3 A will be C, C will F) etc.

Almost all modern encryption\decryption of data, whether it’s WIRED (web, file system, messages) or WIRELESS (cell, sensors) done by three ways:

  •    Symmetric key
  •    Asymmetric key
  •    Mix of Symmetric and Asymmetric keys



Symmetric key cryptography


Symmetric (reflection or mirror) uses a single secret\shared key for both encryption and decryption. In this type of crypto both sender and receiver should know the secret key up front.
Sender encrypts the message using secret key, message transferred over network, Receiver decrypts the message using same secret key used for encryption.




pic courtesy Oracle corp.


The main drawback of shared-key crypto is the difficulty of securing the secret-key, which needed by both sender and receiver, so high chances of leaking the key. It’s called key distribution problem.

Asymmetric key cryptography


Also called public-key or public private key cryptography. Due to the drawbacks of symmetric key, made the way to invention of asymmetric key.
  
Public-key cryptography, proposed by Diffie and Hellman in 1976, is the idea of having two separate keys, a public key for encryption of a message and a secret key for its decryption; a party can privately construct the two keys and then make the encryption key public without thereby revealing the decryption key. Subsequently, anyone can encrypt messages intended for the creator of the keys, but only the creator can decrypt. The first realization of this idea was due to Rivest, Shamir, and Adleman in 1978.

At the heart of RSA – public key crypto is depend upon the properties of the product of two large prime numbers. This algorithm is based on some principles from Number theory, which states that determining the prime factors of a large number is extremely difficult.






pic courtesy Wiki


Differences


Symmetric key
Asymmetric key
Encrypts 256 bits
Encrypts 1024 bits or 2048 bits
The size of ciphered text is less or equal to plain text
The size of ciphered text is heavy
Less computing power
Need lot of computing power
Used : client and server
Used for: digital signature, session key …
DES, AES algorithm
RSA algorithm
Same key for encrypt and decrypt
Public key for encrypt and private key for decrypt



Cryptanalysis is the art and science of breaking the encrypted codes that are created by applying some cryptographic algorithms.

Sunday, January 8, 2017

Friday, January 6, 2017

A Simple TCP listening Server - .Net core application

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TCPServerAndClient
{
    public class Program
    {
        public static void Main(string[] args)
        {
            TcpServer();
        }
     
//thought here is, code as simple as possible so that for beginners it should be easy to grasp the concept quickly.

        public static void TcpServer()
        {
             
                IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1234); //ipaddress : is like Hotel name, you are intended to deliver msg only to this hotel. Port number: tells inside that hotel to which particular room you want to send  msg

                // Create a TCP/IP socket.
                Socket listener = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);

               try
                {
                    listener.Bind(localEndPoint); //start listening for msg
                    listener.Listen(25); //The maximum length of the pending connections queue

                 
                    while (true)
                    {
                     
                        Socket handler = listener.Accept(); //snync call

                        byte[] requestBytes = new byte[handler.SendBufferSize]; //max bytes can  send by a client
                        handler.Receive(requestBytes);
                        string clientSentData = Encoding.ASCII.GetString(requestBytes); //convert bin to ascii, human readable format

                        Console.WriteLine($"This is what client sent {clientSentData}" );

                        string responseToClient = "Thank you, we got your request.";
                        byte[] responseBytes = Encoding.ASCII.GetBytes(responseToClient); //convert string to bin

                        handler.Send(responseBytes);
                        handler.Shutdown(SocketShutdown.Both);
                        handler.Dispose();
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine($" Oops something went wrong -> {e.ToString()} ");
                }

            }
 
    }
}

Monday, January 2, 2017

Sunday, January 1, 2017

My favorite quotes

I have started to collect inspiring quotes. Its living post, I will be keep updating. Earlier I have came across lot of beautiful quotes but forgot to save but not any more. Pls read and pass it on.

“If you wish to understand the Universe, think of energy, frequency and vibration” - Nikola Tesla


"Make a habit of two things: to help or at least todo no harm" - Hippocrates


“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”  ― Albert Einstein


“I seem to have been only like a boy playing on the seashore … while the great ocean of truth lay all undiscovered before me.” Sir Newton


"Education is not the learning of facts, But training the mind to think" - Albert Einstein


“The reading of all good books is like a conversation with the finest minds of past centuries.” - Rene Descartes


“Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.”

― Albert Einstein

“Everything is determined, the beginning as well as the end, by forces over which we have no control. It is determined for the insect, as well as for the star. Human beings, vegetables, or cosmic dust, we all dance to a mysterious tune, intoned in the distance by an invisible piper.”

― Albert Einstein

“It is the supreme art of the teacher to awaken joy in creative expression and knowledge.”

― Albert Einstein

Beauty of style and harmony and grace and good rhythm depends on simplicity. - Plato


Simplicity is the ultimate sophistication. — Leonardo da Vinci


We do not act rightly because we have virtue or excellence, but we rather have those because we have acted rightly. We are what we repeatedly do. Excellence, then, is not an act but a habit. —Aristotle, Nichomachean Ethics


"The most important attitude that can be formed is that of desire to go on learning." —John Dewey, Experience and Education


If I had asked people what they wanted, they would have said faster horses. —Henry Ford


“I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world.”

― Albert Einstein

“If you want your children to be intelligent, read them fairy tales. If you want them to be more intelligent, read them more fairy tales.”

― Albert Einstein

“I have no special talents. I am only passionately curious.”

― Albert Einstein

“A human being is a part of the whole called by us universe, a part limited in time and space. He experiences himself, his thoughts and feeling as something separated from the rest, a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and to affection for a few persons nearest to us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole of nature in its beauty.”

― Albert Einstein

"God does not play dice with the universe." - A Einstein


A scientific man ought to have no wishes, no affections, - a mere heart of stone. - Charles Darwin


It is not the strongest of the species that survive, nor the most intelligent, but the one most responsive to change.


In der Beschränkung zeigt sich erst der Meister. (In simplicity one recognizes true skill.) —J.W. von Goethe


A thorough knowledge of the elements takes us more than half the road to mastership.

—Aron Nimzowitsch

Engineering is art of converting science into useful things


 "Sometimes, the laws of Nature are so simple, we have to rise above the complexity of scientific thought to see them."

 —Richard Feynman Nobel Prize- winning physicist

There is an almost universal quest for easy answers and half-baked solutions.

—Martin Luther King

Only a life lived for others is a life worth living - Albert Einstein.


Life's most persistent and urgent question is, 'what are you doing for others' - Martin Luther King


The future depends on what you do today. — Gandhi


Self-destructive hell has three gates– lust, anger, and greed. - Bagvad Gita


Only when a person responds to the joys and sorrows of others as if they were his own, he has attained the highest state of spiritual union.  - Bagvad Gita


If you think in terms of a year, plant a seed; if in terms of ten years, plant trees; if in terms of 100 years, teach the people.

- Confucius (Philosopher, 551 BC-479 BC)

I hear and I forget. I see and I remember. I do and I understand.

- Confucius (Philosopher, 551 BC-479 BC)
We need men who can dream of things that never were. —John F. Kennedy

Sour, sweet, bitter, pungent, all must be tasted.

—Chinese Proverb

Unless we change direction, we are likely to end up where we are going.

—Anonymous

Real knowledge is to know the extent of one’s ignorance.

—Confucius
“Each work has to pass through these stages—ridicule, opposition, and then acceptance. Those who think ahead of their time are sure to be misunderstood.” - Swamy Vivekananda

“Take up one idea. Make that one idea your life - think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone. This is the way to success”. - Swamy Vivekananda


"But when you give to the needy, do not let your left hand know what your right hand is doing" - Mathew 6:3


Its what you do in the dark,

That puts you in the LIGHT - Under Armour ad

"And in the end it's not the years in your life that count; 

it's the life in your years."  - Abraham Lincoln

Men ought to know that from nothing else but the brain come joys, delights, laughter and sports, and sorrows, griefs, despondency, and lamentations. And by this, in an especial manner, we acquire wisdom and knowledge, and see and hear and know what are foul and what are fair, what are bad and what are good, what are sweet and what are unsavory . . . . And by the same organ we become mad and delirious, and fears and terrors assail us . . . . All these things we endure from the brain when it is not healthy . . . . In these ways I am of the opinion that the brain exercises the greatest power in the man.

—Hippocrates, On the Sacred Disease (Fourth century B.C.) 

"Give me your tired, your poor, your huddled masses yearning to breathe free, the wretched refuse of your teeming shore. Send these, the homeless, tempest-tossed to me, I lift my lamp beside the golden door!" - statue of liberty.

What a beautiful words!. America land of opportunities, hope, inspiration.

All you need is ignorance and confidence and the success is sure.

- Mark Twain

Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better.

- Edsger Dijkstra

Tell me, and I will forget. Show me, and I may remember. Involve me, and I will understand.

— Confucius

Iron rusts from disuse; water loses its purity from stagnation... even so does inaction sap the vigor of the mind.

— Leonardo da Vinci

Thinking well is wise; planning well, wiser; doing well, wisest and best of all.

-Persian Proverb

A little learning is a dangerous thing; Drink deep, or taste not the Pierian spring.

-Alexander Pope


“The important thing is not to stop questioning. Curiosity has its own reason for existing. One cannot help but be in awe when he contemplates the mysteries of eternity, of life, of the marvelous structure of reality. It is enough if one tries merely to comprehend a little of this mystery every day.”-
Attributed to Albert Einstein
"It's not a solution if it's not affordable" Dr. Devi Prasad Shetty


"Practice yourself, for heaven’s sake in little things, and then proceed to greater." – Epictetus


"Wisdom and beauty form a very rare combination" 

"Education is not the learning of facts But the training of the mind to Think" A Einstein

“Genius is one percent inspiration and ninety-nine percent perspiration.”-
Thomas Edison


"Gratitude is not only the greatest of virtues, but the parent of all the others." -
Marcus Tullius Cicero