Creating a Base64 encoder/decoder with C# and Python Integration (IronPython)

Hello everyone, I'm Kruvazör.
I would like to thank @Helmsys for their long-standing support in the Python part of this project.
The use of various programming languages together is a common occurrence in today's modern software development processes.

Therefore, I have conducted a study on integrating C# and Python languages and in this article, we will look at IronPython, which enables C# and Python to work together seamlessly, and undertake a project.
IronPython official site

IronPython preserves the main features of the Python language while providing integration on the C# side. This combination enables the use and interaction of C# and Python code together.
For example, you can create a Python module called from C# or vice versa, create a C# class called from Python.
You can also process data entered in a C# form in Python or vice versa.


hg2ueaq.jpg

IronPython is built on the Dynamic Language Runtime (DLR). DLR provides a communication platform that enables dynamically written languages to operate more effectively.
Now that we understand IronPython and the general structure, we can proceed with the installations and write our code.
Firstly, let's open a WinForm project through Visual Studio.


blxnocx.jpg

Next, let's download the packages.



dnv1c97.jpg


Now, let's write our form code.
1 Label
4 textboxes

1 button


9wuf1uc.jpg


Now, let's write our Python code.
Kod:
import base64

originalString = str(originalString)

print("Orjinal metin: " + originalString)

encodedString = base64.b64encode(originalString.encode()).decode()
print("Şifreli metin: " + encodedString)

decodedString = base64.b64decode(encodedString).decode()
print("Şifresi açık metin: " + decodedString)

encodedString, decodedString

Now, let's write our C# code to retrieve the data sent to Python.
Let's define it in the global area like this:


Kod:
   private ScriptEngine pythonEngine;  //python engine

Kod:
  public Form1()
  {
      InitializeComponent();
      pythonEngine = Python.CreateEngine();

      // kütüphanelerin kurulu olduğu dizini gösteriyoruz ki kütüphaneleri bulsun.
      var searchPaths = pythonEngine.GetSearchPaths();
      searchPaths.Add("..\\..\\Lib");
      pythonEngine.SetSearchPaths(searchPaths);
  }

Kod:
   private void button1_Click(object sender, EventArgs e)
   {
 
       string originalString = textBox1.Text;

       // Kullanıcıdan gelen bilgiyi python tarafına yolluyoruz.
       var scope = pythonEngine.CreateScope();

       scope.SetVariable("originalString", originalString);
       // scriptPathte belirtilen Python kodunu çalıştır

       string scriptPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "base64.py");  //proje klasöründe ara
       pythonEngine.ExecuteFile(scriptPath, scope);


       // Get the result from Python script
       string encodedString = scope.GetVariable<string>("encodedString");
       string decodedString = scope.GetVariable<string>("decodedString");

       // Display results in labels
       textBox2.Text = "Original string: " + originalString;
       textBox3.Text = "Encoded string: " + encodedString;
       textBox4.Text = "Decoded string: " + decodedString;
   }

After moving our Python file to the project folder, we can open the application.


57noqos.jpg




Topic Owner : @Kruvazör C# ve Python Entegrasyonu ile Base64 encoder/decoder Yapımı (IronPython)

213.png
 
Son düzenleme:
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.