Cifru de tip Caesar

Stefanescu Mihai 9 months ago Security

Cifrul de tip Cezar este una dintre primele si cele mai simple metode de cifrare. Este un cifru de substitutie, adica fiecare litera unu text dat este inlocuita cu o litare aflata la un anumit numar de pozitii de ea in alfabet. De exemplu, daca am lua ca exemplu numarul de pozitii 1, litera A va fi inlocuita de litera B, litera B va fi inlocuita de litera C sia asa mai departe. Aceasta tehnica a fost numita dupa Iulius Cezar, pentru ca se spune ca asa vorbea (in scrisori) cu oamenii lui.

Deci, acest tip de cifru are nevoie de un text si un int care sa indice numarul de caractere cu care sa schimbe pozitia fiecarei litere. Acest tip de criptare poate fi reprezentat si printr-o formula matematica transformand literele in numere, conform schemei A = 0, B = 1, ... Z = 25. Criptarea unei litere poate fi descrisa in mod matematic astfel:

En(x) = (x+n)mod26
iar decriptarea
Dn(x) = (x-n)mod26

Exemple:

Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ
Shift: 23
Cifru: XYZABCDEFGHIJKLMNOPQRSTUVW

Text : ATTACKATONCE
Shift: 4
Cifru: EXXEGOEXSRGI

Algortmul pentru criptare

Date de intrare:
1. un string cu litere mici
2. un int in intervalul 0-25 ce reprezinta shift-ul de caractere

Procedura:
1. iteram prin textul data caracter cu caracter
2. pentru fiecare caracter, transformam caracterul conform regulilor, in functie de tipul actiuni, criptare sau decriptare
3. intoarcem noul string generat
Un exemplu de asemenea program:

<?php

// Un program PHP ce ilustreaza tehnica
// Caesar Cipher de criptare
 
// Functia primeste un string si un int pentru numarul de pozitii
function caesarCipher($textToEncrypt, $shiftPositionsNumber = 13) {
	// Literele alfabetului
    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    // Pozitia noua
    $shiftPositionsNumber = (int)$shiftPositionsNumber % 26;
    // Daca nu avem o pozitie noua, iesim din functie cu string-ul dat
	if (!$shiftPositionsNumber){
		return $textToEncrypt;
	}

	// Ne asiguram ca daca pozitia este negativa ne uitam in randul numerelor pozitive
    if ($shiftPositionsNumber < 0) {
    	$shiftPositionsNumber += 26;
    }
    // Daca trebuie sa shiftam numele cu 13 pozitii folosim functia existenta in PHP
    if ($shiftPositionsNumber == 13) {
		return str_rot13($textToEncrypt);
    }

    // Inlocuirea efectiva a caracterelor
    $rep = substr($letters, $shiftPositionsNumber * 2) . substr($letters, 0, $shiftPositionsNumber * 2);
    return strtr($textToEncrypt, $letters, $rep);
}
 
// Executia
$text = "INVATAPROGRAMARE";
$s = 13;
echo "Text : " . $text;
echo "\nShift: " . $s;
echo "\nCifru: " . caesarCipher($text, $s);


iar rezultatul acestui program va fi:
Text : INVATAPROGRAMARE
Shift: 13
Cifru: VAINGNCEBTENZNER

Cum decriptam?
Putem fie sa folosim alta functie pentru decriptare, fie putem folosi proprietatea ciclica a cifrului sub modul, de unde putem observa

Cifru(n) = De-Cifru(26-n)

Deci, putem folosi aceasi frunctie pentru descriptare.

Programator de ~8 ani, am lucrat la proiecte din mai multe industrstrii, de la eCommerce la telecomunicatii la automatizari. In acest timp am folosi diferite tehnologii, de la PHP, MySQL, PostgreSql, RabbitMq, Redis, Memcached si altele.


Get in touch
Pentru nelamuriri, dubii, comentarii si chestii de pe suflet ne putem auzi pe Discord, Reddit sau poti deschide o discutie noua pe forum

real estate investing for doctors # 6 months ago online courses for medical professionals passive income for medical specialists quick ways to earn money from home ways for doctors to monetize their medical blogs
Bruce # 3 weeks ago I'm really loving the theme/design of your weblog. Do you ever run into any browser compatibility issues? A handful of my blog visitors have complained about my blog not working correctly in Explorer but looks great in Opera. Do you have any recommendations to help fix this problem?
Ben # 3 weeks ago I have fun with, cause I discovered exactly what I was having a look for. You've ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye
Best hair loss treatments for men # 2 weeks ago Thanks for finally talking about >Cifru de tip Caesar - Invata-Programare <Liked it!
commander du evista en Belgique en ligne # 2 weeks ago Fantastic beat ! I wish to apprentice while you amend your website, how could i subscribe for a blog site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear concept
precio de orlistat en Perú # 2 weeks ago Pretty! This was a really wonderful article. Thanks for providing this information.
Kein Rezept erforderlich für loratadine # 2 weeks ago What's up i am kavin, its my first time to commenting anyplace, when i read this piece of writing i thought i could also make comment due to this brilliant piece of writing.
Koop fluconazole online in Europa # 1 week ago Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.
Online-Kauf von Medikamenten in Brüssel # 1 week ago If you wish for to get a great deal from this paragraph then you have to apply these techniques to your won website.
urispas en vente libre en France # 1 week ago Greetings from Idaho! I'm bored to death at work so I decided to check out your blog on my iphone during lunch break. I enjoy the information you present here and can't wait to take a look when I get home. I'm shocked at how fast your blog loaded on my cell phone .. I'm not even using WIFI, just 3G .. Anyhow, excellent site!
Bestellung von medroxyprogesterone in Belgien # 1 week ago Your style is so unique in comparison to other people I've read stuff from. Thank you for posting when you've got the opportunity, Guess I'll just bookmark this web site.
penglobe zonder voorschrift in Zwitserland # 6 days ago Great site you have got here.. It's hard to find excellent writing like yours nowadays. I truly appreciate individuals like you! Take care!!
rabeprazole zonder recept nodig # 5 days ago I am curious to find out what blog system you're utilizing? I'm experiencing some small security problems with my latest website and I'd like to find something more secure. Do you have any recommendations?
achat de somnifères diabestat # 5 days ago certainly like your website however you need to check the spelling on quite a few of your posts. Many of them are rife with spelling problems and I in finding it very bothersome to inform the reality however I will certainly come back again.
prix du baclofen en pharmacie # 4 days ago Greetings! Very useful advice within this article! It is the little changes that will make the most important changes. Many thanks for sharing!
Sebastian # 3 days ago Very nice post. I simply stumbled upon your weblog and wanted to mention that I've really enjoyed browsing your weblog posts. In any case I'll be subscribing for your feed and I am hoping you write once more very soon!
Herbert # 2 days ago I am now not positive the place you are getting your info, but good topic. I needs to spend a while finding out more or working out more. Thank you for fantastic info I used to be searching for this information for my mission.
Carlton # 2 hours ago It's actually a nice and helpful piece of information. I'm satisfied that you shared this helpful info with us. Please stay us up to date like this. Thank you for sharing.
Club-ul este dedicat membrilor si ofera access la mai multe zone ale website-ului.
Login Register

🔖 Bookmarks
✨ Pentru a sustine aceasta comunitate am sa te rog sa te autentifici sau sa te inregistrezi!

🌪️ Discord
Back to top
Folosim cookie-uri pentru a oferi functionalitatile critice ale aplicatiei Invata-Programare. Folosim cookie-uri si pentru a analiza traficul, pentru care e nevoie de consimtamantul dvs. explicit.