Introduction



Home Screen :

Name of Experiment : Encryption and Decryption

Exp No : MC13

Background : Student should have basic knowledge of C#.

Summary : Encryption and Decryption techniques are required to solve the confidential issues related to mobile communication such as SMS, Emails etc. Some information is very critical and need to be confined between sender and recipient only.

Learning Objective : Student should get to know the process of securing the message and emails in order to maintain the confidentiality.

Target

Target Platform :This experiment is tested on Windows Phone Emulator and Nokia Lumia 800.

Procedure

SourceCode

Source Code : Comments :
MainPage.xaml
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ExperimentTitle" Text="ExpNO MC13" TextAlignment="Right" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="ApplicationTitle" Text="Mobile Computing" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Mobile Security" Margin="9,-7,0,0" Style="{StaticResource
PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,28,0,0" Name="textBlock1" Text="Message" VerticalAlignment="Top" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="6,64,0,0" Name="txtMessg" Text="" VerticalAlignment="Top" Width="450" />
<Button Content="Encrypt" Height="72" HorizontalAlignment="Left" Margin="128,142,0,0" Name="bEncrypt" VerticalAlignment="Top" Width="160" Click="bEncrypt_Click"/>
<TextBox Height="159" HorizontalAlignment="Left" Margin="9,205,0,0" Name="txtEncrypt" Text=" " VerticalAlignment="Top" Width="438" AcceptsReturn="False" BorderBrush="Green" Background="Black" Foreground="Gray" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeSmall}" />
<Button Content="Decrypt" Height="72" HorizontalAlignment="Left" Margin="141,354,0,0" Name="bDecrypt" VerticalAlignment="Top" Width="160" Click="bDecrypt_Click" />
<TextBox Height="136" HorizontalAlignment="Left" Margin="3,412,0,0" Name="txtDecrypt" Text=" " VerticalAlignment="Top" Width="447" BorderBrush="Green" Background="Black" Foreground="Gray" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeSmall}"/>
<Button Content="Reset" Height="72" HorizontalAlignment="Right" Margin="0,530,181,0" Name="bReset" VerticalAlignment="Top" Width="134" Click="bReset_Click" />
</Grid>
</Grid>









 Exp No MC13(Experiment title)


 Mobile Computing(Application title)
 Mobile Security (Page Title)



MainPage.xaml.cs
using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Security.Cryptography;
using System.Text;

namespace Mobile_Security
{
public partial class MainPage : PhoneApplicationPage
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
byte[] plaintxt;
byte[] encryptedtxt;

// Constructor
public MainPage()
{
InitializeComponent();
}

static public byte[] RSAEncrypt(byte[] byteEncrypt, RSAParameters RSAInfo, bool isOAEP)
{
try
{
byte[] encryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{ RSA.ImportParameters(RSAInfo);

//Encrypt the passed byte array.
encryptedData = RSA.Encrypt(byteEncrypt, isOAEP);
}
return encryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.Message);

return null;
}

}
static public byte[] RSADecrypt(byte[] byteDecrypt, RSAParameters RSAInfo, bool isOAEP)
{
try
{
byte[] decryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAInfo);

//Decrypt the passed byte array and specify OAEP padding.
decryptedData = RSA.Decrypt(byteDecrypt, isOAEP);
}
return decryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());

return null;
}
}
public void bEncrypt_Click(object sender, RoutedEventArgs e)
{
if (Validate())
{
plaintxt = ByteConverter.GetBytes(txtMessg.Text);
encryptedtxt = RSAEncrypt(plaintxt, RSA.ExportParameters(false), false);
txtEncrypt.Text = ByteConverter.GetString(encryptedtxt, 0, encryptedtxt.Length);

}
}

public void bDecrypt_Click(object sender, RoutedEventArgs e)
{
if (Validate1())
{
byte[] decryptedtxt = RSADecrypt(encryptedtxt, RSA.ExportParameters(true), false);
txtDecrypt.Text = ByteConverter.GetString(decryptedtxt, 0, decryptedtxt.Length);
}
}

#region UIValidation
private bool Validate() {
if (string.IsNullOrWhiteSpace(txtMessg.Text)) {
MessageBox.Show("Please Enter some mesage. ");
return false;
}
return true;
}
private bool Validate1() {
if (string.IsNullOrWhiteSpace(txtEncrypt.Text)) {
MessageBox.Show("Please do encryption first");
return false;

}
return true;
}
#endregion


private void bReset_Click(object sender, RoutedEventArgs e)

{
txtMessg.Text = string.Empty;
txtEncrypt.Text = string.Empty;
txtDecrypt.Text = string.Empty;

}
}
}












 instance for RSACryptoServiceProvider
 byte [ ] arrays

Snapshots






Observation

Observation : It is observed by the developer that RSA algorithm proved to be useful for the encryption/decryption of message and emails.

Video Link

For Windows Phone

Download

Right Click Here And Select Save Target For Entire Download :

Mobile Security Experiment