Professional Documents
Culture Documents
21DH112514 NguyenNhutHuy
21DH112514 NguyenNhutHuy
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaHoaCeasar
{
internal class CCeasar
{
public string Plaintext { get; set; }//Văn bản gốc
public int key { get; set; }//khóa
public string Ciphertext { get; set; }//văn bản đã đã được mã hóa
public CCeasar(string plaintext, int key)
{
this.Plaintext = plaintext;
this.key = key;
this.Ciphertext = "";
}
public void MaHoa()
{
foreach (char c in Plaintext) //duyệt từng ký tự trong văn bản gốc
{
if (c != 32)//nếu kh là khoảng trắng
{
int so = c - 33;//đổi từ ký tự sang số
so = (so + key) % (65500);//mã hóa ký tự đang xét
Ciphertext += (char)(so + 33);//gán ký tự vừa mã hóa vào chuỗi đã
}
else { Ciphertext += c; }//nếu là khoảng trắng
}
}
public string GiaiMa()
{
string s = string.Empty;
foreach (char c in Ciphertext)
{
if (c != 32)//nếu kh la khoảng trắng
{
int so = c - 33;//đổi từ ký tự sang số
so = (so - key + 65500) % (65500);//mã hóa ký tự đang xét
s += (char)(so + 33);//gán ký tự vừa mã hóa vào chuỗi đã
}
else
{
s += c;
}
}
return s;
}
}
}
namespace MaHoaCeasar
{
internal class Program
{
static void Main(string[] args)
{
Console.InputEncoding = System.Text.Encoding.UTF8;
Console.OutputEncoding= System.Text.Encoding.UTF8;
Console.Write("Mời bạn nhập chuỗi cần mã hóa: ");
string p = Console.ReadLine();
Console.Write("Mời bạn nhập key= ");
int key = int.Parse(Console.ReadLine());
CCeasar cCassar = new CCeasar(p, key);
cCassar.MaHoa();
Console.WriteLine("Văn bản sau khi mã hóa: " + cCassar.Ciphertext);
string s = cCassar.GiaiMa();
Console.WriteLine("Văn bàn sau sau khi giải mã: " + s);
}
}
}
Python
#========================================
def MaHoa(plaintext, key):
ciphertext=""
for c in plaintext:
if c!=' ':
so = ord(c) - 65;
so = (so+key) % 26
ciphertext = ciphertext+ chr(so+ 65)
else:
ciphertext=ciphertext+c
return ciphertext
#========================================
def GiaiMa(ciphertext,key):
plaintext = ""
for c in ciphertext:
if c != ' ':
so = ord(c)- 65
so = (so - key + 26) % 26
plaintext = plaintext+ chr(so+65)
else:
plaintext = plaintext+c
return plaintext
#========================================
def main():
p = input("Moi nhap chuoi can ma hoa: ")
p=p.upper()
key=3
c = MaHoa(p, key)
print("Sau khi ma hoa= ", c)
s= GiaiMa(c,key)
print("Sau khi giai ma= ",s)
#========================================
if __name__=="__main__": #entry point
main()
#========================
class CCeasar:
def __init__(self, plaintext, key, ciphertext=""):
self.plaintext = plaintext
self.key = key
self.ciphertext=ciphertext
#========================================
def MaHoa(self):
self.ciphertext=""
for c in self.plaintext:
if c!=' ':
so = ord(c) - 65;
so = (so+self.key) % 26
self.ciphertext = self.ciphertext+ chr(so+ 65)
else:
self.ciphertext=self.ciphertext+c
return self.ciphertext
#========================================
def GiaiMa(self):
self.plaintext = ""
for c in self.ciphertext:
if c != ' ':
so = ord(c)- 65
so = (so - self.key + 26) % 26
self.plaintext = self.plaintext+ chr(so+65)
else:
self.plaintext = self.plaintext+c
return self.plaintext
#========================
def main():
p = input("Moi nhap chuoi can ma hoa: ")
p=p.upper()
key=3
cCeasar = CCeasar(p,key)
c = cCeasar.MaHoa()
print("Sau khi ma hoa= ", c)
s= cCeasar.GiaiMa()
print("Sau khi giai ma= ",s)
#========================================
if __name__=="__main__": #entry point
main()
ECLIPSE
package MaHoa;
package MaHoa;
import java.util.Scanner;
public class CCeasar {