[ Log On ]
  • Home
  • Tst
  • Cha
  • Enc
  • Code
  • IP
  • Fun
  • Sub
  • DigF
  • Cis
  • Com
  • Db
  • About
  • Netsim

RSA Example

[Back] RSA is an asymmetric encyption algorithm, which uses two keys, one to encrypt and the other to decrypt. It was created in 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman, and is still one of the most widely used encryption methods. A typical application is in authenticating a sender, where the senders private key is used to encrypt a message, and then is decrypted by the receiver with the senders public key (which is known to anyone who wants it). It is also typically used for encrypting disks/files, such as for EFS.

Message:

Public Key (n,e)
Private key (n,d)
Encrypted
Decrypted

Code used

  public void rsa2()
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

                RSAParameters publickey = rsa.ExportParameters(false); // don't export private key 
                RSAParameters privatekey = rsa.ExportParameters(true); // export private key 

                tbPublic = "e=" + ByteToString(publickey.Exponent) + ", n=" + ByteToString(publickey.Modulus);
                tbPrivate = "d=" + ByteToString(privatekey.D) + ", n=" + ByteToString(publickey.Modulus);

                rsa.ImportParameters(publickey);
                byte[] encryptedData = rsa.Encrypt(StringToByte(message), true);
                encrypted = ByteToString(encryptedData);

                rsa.ImportParameters(privatekey);
                byte[] decryptedData = rsa.Decrypt(encryptedData, true);

                decrypted = ByteToAscii(decryptedData);
            }
            catch (Exception ex)
            {

                encrypted = ex.Message.ToString();
            }
        }