You are on page 1of 7

Nguyễn Nhựt Huy – 21DH112514

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;

public class CMaHoaCeasar {


private String plaintext;
private int key;
private String ciphertext;
public String GetPlaintext() {
return plaintext;
}
public void SetPlaintext(String p) {
plaintext = p;
}
public int Getkey() {
return key;
}
public void Setkey(int k) {
key = k;
}
public String GetCiphertext() {
return ciphertext;
}
public void setCiphertext(String c) {
ciphertext = c;
}
public CMaHoaCeasar(String sVBG, int k) {
plaintext = sVBG;
key = k;
ciphertext = "";
}
public void MaHoa() {
for (char c : plaintext.toCharArray()) {
if(c != 32) {
int so = c -33;
so = (so + key) % 65500;
ciphertext += (char)(so + 33);
}
else {
ciphertext += c;
}
}
}
public String GiaiMa() {
String s = "";
for (char c : ciphertext.toCharArray()) {
if (c != 32) {
int so = c - 33;
so = (so - key + 65500) % 65500;
s += (char)(so + 33);
}
else {
s += c;
}
}
return s;
}
}

package MaHoa;
import java.util.Scanner;
public class CCeasar {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.out.print("Mời nhập chuỗi p: ");
Scanner sc = new Scanner(System.in);//Khai báo đối tượng sc nhập từ bàn
phím
String p = sc.nextLine();//Nhập chuỗi
System.out.print("Mời nhập key: ");
int key = sc.nextInt();//Nhập số nguyên4
CMaHoaCeasar obj = new CMaHoaCeasar(p, key);
obj.MaHoa();
System.out.printf("Sau khi mã hóa: %s", obj.GetCiphertext());
String s = obj.GiaiMa();
System.out.printf("\nSau khi giải mã : %s", s);
}

You might also like