11,485,965 members (78,841 online)

# Image Cryptography using RSA Algorithm in C#

, 21 Apr 2014 CPOL 34.7K 2.4K 48
 Rate this:
Image cryptography using RSA algorithm in C#

## Introduction

In this article's project, Image Cryptography concepts are used. This project is made in Visual Studio 2010 C#.NET platform. Now a days, Privacy & Security issues of the transmitted data is an important concern in multimedia technology, so this project understands how encryption and decryption happens?

In this project, cryptography gets used to hide images. In this, RSA (Ron Rivest, Adi Shamir, and Leonard Adleman ) algorithms are used.

### The Main Concepts

In this project, Image takes as input, in properties section, image properties are shown, like file name, image resolution, image size. After loading image, HEX function extracts the image HEX code, HEX code is converted into cipher text depending on RSA settings. An opposite, Cipher text gets loaded, then apply RSA algorithm, then decipher the text, resultant string is converted into image.

This project has three class files:

1. RSAalgorithm.cs

In this class file, RSA algorithm related functions are available like calculating phi, n, square, and modulus. RSA algorithm is step in this project to encrypt and decrypt images. This algorithm encrypts and decrypts the images, i.e., each frame gets encrypted and decrypted.

2. library.cs

In this class file, hex decoding, checking prime number, byte to image conversion, image to byte conversion, vice versa are available. Library is used for basic conversion, processing and decoding Hex values, byte array to image conversion, and opposite as well, i.e. image to byte array conversion.

Flow Chart for Encryption and Decryption

The above flow chart shows how the project in this article works. The flow chart explains in a step by step manner the processing of the encryption and decryption, using C#. In this project, we only consider the images not audio. Images are encrypted, and create the *.txt file, the same file is used for decryption.

## Library

Library is used for basic conversion, processing and decoding Hex values, byte array to image conversion, and opposite as well, i.e., image to byte array conversion. Following four functions are part of the library.cs file:

1. `DecodeHex`: This function decodes the hex, means accepts hex string, converts it to byte array for image conversion.
2. `IsPrime`: This function checks parameter value, prime or not. This function is useful for validation purpose. Prime number accept from user.
3. `ConvertByteToImage`: This function converts the byte array to image. Image is a bitmap image.
4. `ConvertImageToByte`: This function converts the image to byte array. Image is JPEG format image.
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace ImageCrypto
{
class library
{
public static byte[] DecodeHex(string hextext)
{
String[] arr = hextext.Split('-');
byte[] array = new byte[arr.Length];
for (int i = 0; i < arr.Length; i++)
array[i] = Convert.ToByte(arr[i], 16);
return array;
}

public static bool IsPrime(int number)
{
if (number < 2) return false;
if (number % 2 == 0) return (number == 2);
int root = (int)Math.Sqrt((double)number);
for (int i = 3; i <= root; i += 2)
{
if (number % i == 0)
return false;
}
return true;
}

public static Bitmap ConvertByteToImage(byte[] bytes)
{
return (new Bitmap(Image.FromStream(new MemoryStream(bytes))));
}

public static byte[] ConvertImageToByte(Image My_Image)
{
MemoryStream m1 = new MemoryStream();
new Bitmap(My_Image).Save(m1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] header = new byte[] { 255, 216 };
}
}
} ```

### How It Works?

RSA is an encryption and authentication system, an algorithm developed in 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman. RSA is a cryptosystem, which is also known as public-key cryptosystems. RSA is normally used for secure data transmission.

A user of RSA creates product of two large prime numbers, along with an auxiliary value, as their public key. The prime factors kept secret. The public key is used to encrypt a message, and private key is used to decrypt a message.

In the following RSA algorithm, it is clearly shown how to encrypt and decrypt message using RSA with sample numeric example. But in the project given in this article, instead of numeric values we encrypt the Hex string value of images frames. In using the code, section all RSA algorithm related functions are explained in detail.

### RSA Algorithm with Sample Example

Step 1: Start

Step 2: Choose two prime numbers

p = 3 and q = 11

Step 3: Compute the value for ‘n’

n = p * q = 3 * 11 = 33

Step 4: Compute the value for ? (n)

? (n) = (p - 1) * (q -1) = 2 * 10 = 20

Step 5: Choose e such that 1 < e < ? (n) and e and n are coprime. Let e = 7

Step 6: Compute a value for d such that (d * e) % ? (n) = 1. d = 3

Public key is (e, n) => (7, 33)

Private Key is (d, n) => (3, 33)

Step 7: Stop.

Let M, is plain text (message), M= 2.

Encryption of M is: C = Me % n.

Cipher text is, C = 27 % 33.

C = 29.

Decryption of C is: M = Cd % n.

Plain text (message), M= 293 % 33.

M= 2

### RSA Program

In this class file, RSA algorithm related functions are available like calculating phi, n, square, and modulus. RSA algorithm is first layer step in this project to encrypt and decrypt video images or frames. This algorithm encrypts and decrypts the video frame by frame, i.e., each frame gets encrypted and decrypted.

• `square`: This function calculates the square value.
• `BigMod`: This function is a recursive function. It calls itself. It works for encryption as well as decryption purpose.
• `n_value`: This function calculates value of ‘`n`’.
• `cal_phi`: This function calculates phi, i.e., ? (n) value.
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;

namespace ImageCrypto
{
class RSAalgorithm
{
public static long square(long a)
{
return (a * a);
}

public static long BigMod(int b, int p, int m) //b^p%m=?
{
if (p == 0)
return 1;
else if (p % 2 == 0)
return square(BigMod(b, p / 2, m)) % m;
else
return ((b % m) * BigMod(b, p - 1, m)) % m;
}

public static int n_value(int prime1, int prime2)
{
return( prime1 * prime2);
}

public static int cal_phi(int prime1, int prime2)
{
return ( (prime1 - 1) * (prime2- 1) );
}

public static Int32 cal_privateKey(int phi, int e, int n )
{
int d = 0 ;
int RES = 0;

for (d = 1; ; d++)
{
RES = (d * e) % phi;
if (RES == 1) break;
}
return d;
}
}
} ```

## Encryption

The following function does the actual encryption task. This function accepts the string parameter. Image hex code is passed as parameter to this function. This string gets converted into character array. Each character is ciphered using RSA setting till the end of array. Cipher string is saved as *.txt extension.

```public string encrypt(string imageToEncrypt)
{
string hex = imageToEncrypt;
char[] ar = hex.ToCharArray();
String c = "";
progressBar1.Maximum = ar.Length;
for (int i = 0; i < ar.Length; i++)
{
Application.DoEvents();
progressBar1.Value = i;
if (c == "")
c = c + ImageCrypto.RSAalgorithm.BigMod(ar[i], RSA_E, n);
else
c = c + "-" + ImageCrypto.RSAalgorithm.BigMod(ar[i], RSA_E, n);
}
return c;
} ```

## Decryption

Following function does the actual decryption task. This function accepts the string parameter. Image cipher code is passed as parameter to this function. Cipher string gets converted into character array. Each character deciphers with RSA setting. Decipher character is converted into actual character value. Each character is cascaded, then the cascaded string is return back.

```public string decrypt(String imageToDecrypt)
{
char[] ar = imageToDecrypt.ToCharArray();
int i = 0, j = 0;
string c = "", dc = "";
progressBar2.Maximum = ar.Length;
try
{
for (; i < ar.Length; i++)
{
Application.DoEvents();
c = "";
progressBar2.Value = i;
for (j = i; ar[j] != '-'; j++)
c = c + ar[j];
i = j;
int xx = Convert.ToInt16(c);
dc = dc + ((char)ImageCrypto.RSAalgorithm.BigMod(xx, d, n)).ToString();
}
}
catch (Exception ex) { }
return dc;
} ```

## Points of Interest

• Learn how to encrypt and decrypt the images
• Learn RSA algorithm
• Learn how to extract get Hex code from image

## Sorry

Sorry for my English. If you notice errors or can suggest a more correct version, please let me know.

## History

• 8 Feb 2014 - First release

## Share

Software Developer Opulent Infotech
India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. (www.unipune.ac.in). I have been learning OpenCV, Enjoying it.
My research interest in Image Processing, Artificial Intelligence, Security Systems, Security Metrics. I work on OpenCV, EmguCV, Visual Studio 2008/10, Java platform.

You Tube Channel

 First PrevNext
 Decryption Problem Member 1087793621-May-15 5:29 Member 10877936 21-May-15 5:29
 Regarding decryption Member 1147946018-May-15 22:54 Member 11479460 18-May-15 22:54
 How to make the algorithm encrypt every letter separately? Member 1088470113-May-15 9:44 Member 10884701 13-May-15 9:44
 Problem with decryption Member 113129151-May-15 4:26 Member 11312915 1-May-15 4:26
 Re: Problem with decryption Member 113129151-May-15 5:01 Member 11312915 1-May-15 5:01
 decreption variable d firasooo8919-Apr-15 1:01 firasooo89 19-Apr-15 1:01
 Re: decreption variable d SOHAM_GANDHI20-Apr-15 22:23 SOHAM_GANDHI 20-Apr-15 22:23
 BigMod and Square Member 1088470125-Mar-15 15:08 Member 10884701 25-Mar-15 15:08
 Re: BigMod and Square SOHAM_GANDHI27-Mar-15 19:58 SOHAM_GANDHI 27-Mar-15 19:58
 decryption full of error Member 1136592918-Jan-15 12:45 Member 11365929 18-Jan-15 12:45
 My vote of 3 anu_soft17-Jan-15 8:03 anu_soft 17-Jan-15 8:03
 i found one error Member 974848814-Jan-15 16:16 Member 9748488 14-Jan-15 16:16
 Re: i found one error SOHAM_GANDHI14-Jan-15 19:24 SOHAM_GANDHI 14-Jan-15 19:24
 Re: i found one error Member 974848814-Jan-15 20:18 Member 9748488 14-Jan-15 20:18
 Re: i found one error Member 974848814-Jan-15 20:24 Member 9748488 14-Jan-15 20:24
 How to use modified RSA Member 108847012-Dec-14 15:08 Member 10884701 2-Dec-14 15:08
 Re: How to use modified RSA SOHAM_GANDHI2-Dec-14 19:34 SOHAM_GANDHI 2-Dec-14 19:34
 Re: How to use modified RSA Member 108847013-Dec-14 13:38 Member 10884701 3-Dec-14 13:38
 Re: How to use modified RSA SOHAM_GANDHI3-Dec-14 18:23 SOHAM_GANDHI 3-Dec-14 18:23
 Re: How to use modified RSA Member 108847014-Dec-14 6:21 Member 10884701 4-Dec-14 6:21
 Re: How to use modified RSA SOHAM_GANDHI4-Dec-14 18:30 SOHAM_GANDHI 4-Dec-14 18:30
 Re: How to use modified RSA Member 108847015-Dec-14 12:37 Member 10884701 5-Dec-14 12:37
 Re: How to use modified RSA SOHAM_GANDHI5-Dec-14 18:20 SOHAM_GANDHI 5-Dec-14 18:20
 How to make output as image not a txt ?? but its image output cannot read. Pahrul Irfan4-Nov-14 17:06 Pahrul Irfan 4-Nov-14 17:06
 Re: How to make output as image not a txt ?? but its image output cannot read. SOHAM_GANDHI4-Nov-14 18:17 SOHAM_GANDHI 4-Nov-14 18:17
 Re: How to make output as image not a txt ?? but its image output cannot read. Pahrul Irfan4-Nov-14 18:36 Pahrul Irfan 4-Nov-14 18:36
 Re: How to make output as image not a txt ?? but its image output cannot read. SOHAM_GANDHI4-Nov-14 18:59 SOHAM_GANDHI 4-Nov-14 18:59
 Re: How to make output as image not a txt ?? but its image output cannot read. Pahrul Irfan5-Nov-14 6:54 Pahrul Irfan 5-Nov-14 6:54
 Re: How to make output as image not a txt ?? but its image output cannot read. SOHAM_GANDHI10-Nov-14 19:29 SOHAM_GANDHI 10-Nov-14 19:29
 error during decryption Aman Sanghani30-Jul-14 8:33 Aman Sanghani 30-Jul-14 8:33
 Re: error during decryption SOHAM_GANDHI4-Aug-14 22:21 SOHAM_GANDHI 4-Aug-14 22:21
 Re: error during decryption Daniel Berrocal13-Sep-14 7:18 Daniel Berrocal 13-Sep-14 7:18
 Re: error during decryption Member 974848814-Jan-15 4:29 Member 9748488 14-Jan-15 4:29
 Re: error during decryption Member 974848814-Jan-15 16:14 Member 9748488 14-Jan-15 16:14
 Re: error during decryption SOHAM_GANDHI14-Jan-15 19:23 SOHAM_GANDHI 14-Jan-15 19:23
 Two errors Member 1088470118-Jun-14 10:09 Member 10884701 18-Jun-14 10:09
 Re: Two errors SOHAM_GANDHI18-Jun-14 19:10 SOHAM_GANDHI 18-Jun-14 19:10
 Hello Member 1088470117-Jun-14 9:14 Member 10884701 17-Jun-14 9:14
 Re: Hello SOHAM_GANDHI17-Jun-14 19:25 SOHAM_GANDHI 17-Jun-14 19:25
 Image Cryptography Member 1062002025-Apr-14 0:51 Member 10620020 25-Apr-14 0:51
 Re: Image Cryptography SOHAM_GANDHI25-Apr-14 7:17 SOHAM_GANDHI 25-Apr-14 7:17
 Re: Image Cryptography Member 1062002027-Apr-14 23:58 Member 10620020 27-Apr-14 23:58
 Re: Image Cryptography SOHAM_GANDHI1-May-14 20:07 SOHAM_GANDHI 1-May-14 20:07
 Gives error in Image type Image <Bgr, Byte> Tausif/Pintu7-Apr-14 17:01 Tausif/Pintu 7-Apr-14 17:01
 image cryptography Member 103497472-Apr-14 19:28 Member 10349747 2-Apr-14 19:28
 Re: image cryptography SOHAM GANDHI2-Apr-14 20:10 SOHAM GANDHI 2-Apr-14 20:10
 image cryptography Member 103497472-Apr-14 3:07 Member 10349747 2-Apr-14 3:07
 Re: image cryptography SOHAM GANDHI2-Apr-14 3:21 SOHAM GANDHI 2-Apr-14 3:21
 Re: image cryptography SOHAM GANDHI2-Apr-14 3:54 SOHAM GANDHI 2-Apr-14 3:54
 image cryptography Member 1034974729-Mar-14 1:24 Member 10349747 29-Mar-14 1:24
 Last Visit: 31-Dec-99 19:00     Last Update: 25-May-15 22:04 Refresh 12 Next »