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

RSA Example

[Back] RSA is an asymmetric encryption 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. In this example we use a range of key sizes from 512-bits (which is seen to be insecure) right up to 4,096 bits. We could go up to 16,384 bits, but it would take a long time to generate the keys:

Message:
Key size:

512:1024: 2,048:3,072 (Takes approx 5 seconds): 4,096 (takes approx 30 seconds):

Key Pair
Public key
Encrypted
Decrypted

Key formats

A public key has the format:

SEQUENCE                  // PublicKeyInfo
+- SEQUENCE               // AlgorithmIdentifier
   +- OID                 // 1.2.840.113549.1.1.1
   +- NULL                // Optional Parameters
+- BITSTRING              // PublicKey
   +- SEQUENCE            // RSAPublicKey
      +- INTEGER(N)       // N
      +- INTEGER(E)       // E
    

and for the private key:

    
SEQUENCE                  // PrivateKeyInfo
+- INTEGER                // Version - 0 (v1998)
+- SEQUENCE               // AlgorithmIdentifier
   +- OID                 // 1.2.840.113549.1.1.1
   +- NULL                // Optional Parameters
+- OCTETSTRING            // PrivateKey
   +- SEQUENCE            // RSAPrivateKey
      +- INTEGER(0)       // Version - v1998(0)
      +- INTEGER(N)       // N
      +- INTEGER(E)       // E
      +- INTEGER(D)       // D
      +- INTEGER(P)       // P
      +- INTEGER(Q)       // Q
      +- INTEGER(DP)      // d mod p-1
      +- INTEGER(DQ)      // d mod q-1
      +- INTEGER(Inv Q)   // INV(q) mod p
    

Code used

I have integrated the library from Here

        [HttpPost]
        public ActionResult rsa3(hashing h, FormCollection form)
        {
            h.message = form["message"];

          int [] val = {512,1024,2048,3072,4096,6144,8192,10240,16384};
     

            RSACryptoServiceProvider csp = new RSACryptoServiceProvider(val[0]);
            string str = csp.ToXmlString(true).Replace("><", ">\r\n<");
            string str2= csp.ToXmlString(false).Replace("><", ">\r\n<");
            try
            {
                RSAx rsax = new RSAx(str, val[0]);
                rsax.RSAxHashAlgorithm = RSAxParameters.RSAxHashAlgorithm.SHA1;
                byte[] CT = rsax.Encrypt(Encoding.UTF8.GetBytes(h.message), false, false);
                string str3 = Convert.ToBase64String(CT);
                ViewData["encrypted"] = str3;

                rsax = new RSAx(str, val[0]);
                rsax.RSAxHashAlgorithm = RSAxParameters.RSAxHashAlgorithm.SHA1;
                byte[] PT = rsax.Decrypt(Convert.FromBase64String(str3), true, false);
                string str4 = Encoding.UTF8.GetString(PT);
                ViewData["decrypted"] = str4;
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }

            ViewData["tbPublic"] = str;
            ViewData["tbPrivate"] = str2;
            
            
            return PartialView("ParticalRSA3Keys");

        }