Encryption - Decryption program with VB

Encryption is the process of transforming information (referred to as plaintext) using an algorithm (called cipher) to make it unreadable to anyone except those possessing special knowledge, usually referred to as a key.
The result of the process is encrypted information (in cryptography, referred to as ciphertext).
In here, we would shown sample program with application that displayed like the following:


















This program will be encrypted all words are input by user at textboxt text label. Then the output will be display at textboxt with label Hasil enkripsi and vice versa. The output of decryption will be display at textbox Hasil Deskripsi label.

The following is sample of main code:

Private Sub cmd_enkrip_Click()
Dim Enkrip, Output, Inputan As String
Dim Panjang_Input As Integer
Inputan = Text1.Text
Panjang_Input = Len(Text1.Text)
For i = 1 To Panjang_Input
Enkrip = Mid(Inputan, i, 1)
Enkrip = Asc(Enkrip)
Enkrip = (Enkrip + 5) - 13
Enkrip = Chr(Enkrip)
Output = Output & Enkrip
Next i
Text2.Text = Output
End Sub

Private Sub cmd_dekrip_Click()
Dim Dekrip, Output, Inputan As String
Dim Panjang_Input, Pesan As Integer
Inputan = Text3.Text
Panjang_Input = Len(Text3.Text)
For i = 1 To Panjang_Input
Dekrip = Mid(Inputan, i, 1)
Dekrip = Asc(Dekrip)
Dekrip = (Dekrip - 5) + 13
Dekrip = Chr(Dekrip)
Output = Output & Dekrip
Next i
Text4.Text = Output
End Sub

Private Sub cmd_hapus_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub

Private Sub Command3_Click()
End
End Sub

C# Tutorial For Beginner (IV) --> The Last

You will need a C compiler, I advise gcc for windows called MinGW, it's free, it's good, it's GCC!
We will create 3 file:
echo.c

#include

#define DLLOBJECT __declspec(dllexport)

DLLOBJECT void writeln(char* s)
{
printf("%s\n", s);
}


echo.cs

using System;
using System.Runtime.InteropServices;

namespace HelloUtil
{
public class Echo
{
[DllImport("echo.native.dll", CallingConvention=CallingConvention.Cdecl)]
static extern void writeln(string s);

string myString;

public Echo(string aString)
{
myString = aString;
}

public void Tell()
{
writeln(myString);
}
}
}


hello.cs

using System;
using HelloUtil;

public class Hello
{
public static void Main()
{
Echo h = new Echo("Hello my 1st interop code !");
h.Tell();
}
}


Hehe, here you discover a completly new thing, Attribute.
"[DllImport(.." is an attribute.
You could tag any method/field/class with any number of attribute.
They generate extra information that could be used by anyone who could understand them.
This DllImport attribute is understand by the compiler and told him that the function below is in fact in a DLL whose name is "echo.native.dll". I add a calling convention parameter as the default .NET calling convention is __stdcall whereas, in C, it's __cdecl.
By the way, if you look for DllImport in the documentation, look for DllImportAttribute, because you remove "Attribute" to attribute classname when using them, it's like this.

And now let's compile this!

> csc /nologo /t:library /out:echo.dll echo.cs
> csc /nologo /out:hello.exe /r:echo.dll hello.cs
>
> rem "if the following line don't work, read bellow.."
> gcc -shared -o echo.native.dll echo.c
> strip echo.native.dll


the 2 last line (the gcc & strip command) are for building the "C-DLL".
If they don't work maybe gcc is not in a directory listed in your path environment variable ? check with:
%lt; echo %PATH%
Well it's probably not,anyway, so type, assumin mingc is in C:\MinGW:
set PATH=C:\MinGW;%PATH%
And try again... you sure it's not a syntax error ?
If it compile test it now: hello
Great isn't it ?

Now I should admit I didn't tell you all the truth. echo.dll and echo.native.dll are not the same kind of DLL. It's not just the language (C / C#) the C one is a plain executable full of, probably, x86 instruction, whereas the C# one is what MS call a portable executable.. anyway they are different.
If you install echo.dll in the GAC it wont work because it won't find echo.native.dll except if you put in into the PATH (like C:\Windows\System32).
In the same manner when you add the reference in VS.NET echo.native.dll is overlooked and your program won't work....
So either put the native one in your path or copy it in the debug/release directory of VS.NET.
Or do everything by hand (makefile? build.bat?) and put all your dll in you build directory, and everything work fine..

C# Tutorial For Beginners (Part III)

Now you become to be pretty confident, I guess, so we could start using multiple file, and even a dll ? go into an other directory (or stay in this one, I won't mind) and create 2 file:

hello.cs

using System;

public class Hello
{
public static void Main()
{
HelloUtil.Echo h = new HelloUtil.Echo("Hello my 1st C# object !");
h.Tell();
}
}


echo.cs

using System;

namespace HelloUtil
{
public class Echo
{
string myString;

public Echo(string aString)
{
myString = aString;
}

public void Tell()
{
Console.WriteLine(myString);
}
}
}


Note in hello.cs I have used the syntax "HelloUtil.Echo" it's because Echo is in the namespace HelloUtil, you could have typed (at he start of the file) using HelloUtil and avoid HelloUtil., that's the way namespace work.

Now you could compile both in one .exe with
> csc /nologo /out:hello.exe *.cs
But it's not my intention, no.
Well.
(Have you tried?)
Let's go building a DLL:
> csc /nologo /t:library /out:echo.dll echo.cs
that's it (dir will confirm).
Now we could use it ...
> csc /out:hello.exe /r:echo.dll hello.cs
if you typed "hello" it will worked as usual..., but if you delete "echo.dll" the program will now crash: it use the DLL. You could also change Echo.cs, rebuild the DLL and see... that's the advantage of DLL!

You could also put your DLL in the global assembly cache (GAC), and any program would be able to access it, even if the DLL is not in its directory!
to put it in the GAC, I sugest you read MS doc but here are the unexplained step:

1. create your assembly key, create it once and use it for every version. you create it with:
sn -k myKeyName.snk
the .snk file should be in your compilation directory (the one where your run csc)
2. create a strong asssembly title by adding in any .cs source file the following directive at top level:

using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyTitle("My Lib Title")]
[assembly: AssemblyVersion("1.2.3.4")]
[assembly: AssemblyKeyFile("myKeyName.snk")]


3. now add it to the GAC:
> gacutil.exe /if myLib.dll

By the way, did I tell you ? when I referenced the hello.dll while compiling, remember? csc /out:hello.exe /r:echo.dll hello.cs, it could have been any assembly, even a .exe !!!

← Previous Page

Awards

100 Blog Indonesia Terbaik


IP
Powered by Blogger.