اذهب إلى المحتوى
  • 0

كيفية عمل Mono alphabetic cipher

Hawa Osman

السؤال

Recommended Posts

  • 0

تعمل هذه الخوارزيمة على استبدال كل حرف بحرف آخر:

simple_substitution_cipher.jpg.a32a4857f70f8563a2d04a50c7d01614.jpg

ويمكن استخدام Hash Map لحل المشكلة:

#include <bits/stdc++.h>
using namespace std;
unordered_map<char,char> hashMap;

string encrypt(string msg)
{
  string ciphertext;
  for(int i=0; i<msg.size(); i++)
  {
    ciphertext.push_back(hashMap[msg[i]]);
  }
  
  return ciphertext;
}

string decrypt(string msg)
{
  string plaintext;
  for(int i=0; i<msg.size(); i++)
  {
    plaintext.push_back(hashMap[msg[i]]);
  }
  
  return plaintext;
}

void hashFn(string a, string b)
{
  hashMap.clear();
  for(int i=0; i<a.size(); i++)
  {
    hashMap.insert(make_pair(a[i],b[i]));
  }
}

int main() 
{
    string alphabet = "abcdefghijklmnopqrstuvwxyz";
    string substitution = "qwertyuiopasdfghjklzxcvbnm";
    string msg = "absdhj";
    
    hashFn(alphabet, substitution);
    
    string cipher = encrypt(msg);
    cout<<"Encrypted Cipher Text: "<<cipher<<endl;
    
    hashFn(substitution, alphabet);
    string plain = decrypt(cipher);
    cout<<"Decrypted Plain Text: "<<plain<<endl;
}

لاحظ سيتم ربط كل حرف بحرف آخر ومن ثم المرور على الأجرف في السلسلة النصية و استبدال كل حرف بالحرف المقابل له.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...