Python String Encryption Techniques
Python String Encryption Techniques
We want to encrypt a given string CH whose size does not exceed 50.
characters in a string result Res as follows: traverse the string CH
from left to right counting the number of successive occurrences of each
character of the string CH, then to sort the string Res, this number followed by the character e
question.
Write a Python program that allows the input of the string CH which must be non-empty.
and formed solely by alphabetical letters, then to create and display the
Res chain according to the principle described above.
Example
def crypter(CH):
res =''
if len(CH) <= 50:
cpt = 1
for i in range(len(CH)-1):
if CH[i] == CH[i+1]:
cpt += 1
else:
res += str(cpt) + CH[i]
cpt = 1
res += str(cpt) + CH[-1]
return res
print(crypter("aaaFyBssssssssssssazz"))
Exercise 2
We propose to write a program that allows entering and encrypting a word M.
not seen, composed only of uppercase letters and to display the encrypted word
MC.
The encryption method is as follows:
For each letter, determine its number of occurrences.
(appearing in the word M. Determine K which is equal to 2*n*sin is odd
and will be equal to (n DIV 2) is even. Replace each letter with Kth
letter that follows it in the interval of the alphabet ['A'...'Z']. For the last ones
letters, we return from the beginning, for example if K=3, we will replace 'A' with
'D' becomes 'E', 'C' becomes 'E'... 'Y' becomes 'B' and 'Z' becomes 'C'.
Example
print(crypter_mc("HAPPY"))
Exercise 3
One of the oldest cryptographic systems (easily decipherable) consists of shifting the
letters of a message to render it unreadable. Thus, A becomes B, B becomes C, etc. And
the Z become A
Example
def crypter(ch):
alpha ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
res =''
for character in ch:
index = [Link](character)
pos = index + 1
if pos == 26:
pos = 0
res += alpha[pos]
return res
print(crypter("ABCCZABEY"))
Exercise 4
A (relative) improvement of the principle used in exercise 3 consists of operating with
shift not by 1, but by any number of letters. Thus, for example, if
if we choose a shift of 3, A becomes E, B becomes E, etc. And Z
become C
Create a python program based on the same principle as the previous one, but that
request additionally what is the offset to use.
def cesar(s, d):
alpha ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
res =''
for letter in s:
order = [Link](letter)
index = order + d
if index > 25:
index = index % 26
res += alpha[index]
return res
print(cesar("ABCDEFGH", 3))
Exercise 5
Exercise 6
A cryptography system much harder to break than the previous ones was
invented in the 16th century by the French Vigenère. It consisted of a combination of
different Caesar ciphers.
Indeed, we can write 25 alphabets shifted in relation to the normal alphabet:
The alphabet that starts with B and ends with ...YZA
The alphabet that starts with C and ends with ...ZAB
etc.
The encoding will be based on the principle of the Caesar cipher: we replace the letter
originally by the letter occupying the same place in the shifted alphabet.
But unlike the Caesar cipher, the same message will use not one, but
several shifted alphabets. To know which alphabets should be used, and in
what order, we use a key.
If this key is 'VIGENERE' and the message is 'We must encode this phrase', we will proceed
as follows:
The first letter of the message, I, is the 9th letter of the normal alphabet. It must be
coded using the alphabet starting with the first letter of the key, V. In this
alphabet, the 9th letter is D. I thus becomes D.
The second letter of the message, L, is the 12th letter of the normal alphabet. It must be
coded using the alphabet starting with the second letter of the key, I. In this
alphabet, the 12th letter is S. L becomes S, etc.
When we reach the last letter of the key, we start again from the first.
Write a Python program that performs Vigenère encryption, asking properly.
sure at the start the key to the user.
def Vigenere(msg, key):
alpha ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
res =''
i=0
for character in msg:
debut = [Link](cle[i])
pos = [Link](character)
index = pos + start
if index > 25:
index -= 26
res += alpha[index]
i += 1
if i >= len(cle):
i=0
return res
def NbcMaj(password):
nb = 0
for i in pass
if A <= i <='Z':
nb += 1
return nb
def NbcAlpha(password):
return len(pass)-NbcMaj(pass)-NbcMin(pass)
def longMaj(password):
d=0
s=0
i=0
while i < len(passe):
if 'A' < passe[i] < 'Z':
s += 1
else:
if s > d:
d=s
s=0
i += 1
return d
def longMin(pass):
d=0
s=0
i=0
while i < len(passe):
if a if passe[i] is less than 'z':
s += 1
else:
if s > d:
d=s
s=0
i += 1
return d
def score(password):
bonus = (len(password)-NbcMin(password))*3+(len(password) -
NbcMaj(password))*2+(len(password)-NbcAlpha(password))*5
penalites = longMaj(password)*3+longMin(password)*2
val = bonus-penalties
if val < 20:
Very low
elif val < 40:
Weak
elif val < 80:
Fort
else:
Very strong
pas ="P@SI_promo2016"
score(pas)
Exercise 8
A happy number is an integer that, when you add the squares of each
of these numbers, then the squares of the numbers of this result and so on until
the obtaining of a single digit number equal to 1 (one).
Example:
N=7 is happy, since:
7 2= 49
4 2+9 2=97
9 2+ 72 = 130
1 2+ 3 2+ 0 2= 10
1 2+ 02 = 1
We arrived at a single digit number that equals 1, so N=7 is happy.
Work requested:
print(happy(7))
Exercise 9
robinson(5)
Exercise 10
In an arithmetic context, prime factorials and
prime primordial numbers as indicated below.
A PF number is said to be a factorial prime if it satisfies the following two properties:
PF is a prime number
And PF is written in the form of a factorial increased or decreased by 1.
(PF=F! + 1 or PF=F! - 1), knowing that the factorial of F noted F! is equal to
F*(F-1) *…*1
Example
def first_fact(n):
etat= False
if first
f= 1
ordre= 2
while f < n:
f= f*order
order+= 1
if f+1 == nor f-1 == n:
etat= True
return state
def premier_primoriel(n):
etat= False
if first (n) True:
p= 3
s= 0
while s < n:
s= 1
for iin range(2, p+1):
if first(i)== True
s= s*i
p+= 1
if s+1 == nor s-1 == n:
etat= True
break
return state
n= 719
print(prime_fact(n))
The Vigenère cipher differs from the Caesar cipher in that it uses a series of different shift alphabets, determined by a key, to encrypt the message. Each letter of the message is encrypted using a different shifted alphabet, as dictated by the corresponding letter in the keyword. In contrast, the Caesar cipher involves shifting all characters in the plaintext by the same amount .
The encrypted values in the random substitution technique are computed by mapping each character in the original message to a character in a user-provided key alphabet. Each letter of the message corresponds to the position of its regular alphabetical occurrence, then aligns with the respective character in the key alphabet, resulting in a jumbled but consistent mapping for decryption .
A factorial prime number is defined as a prime number that can be expressed in the form of F! + 1 or F! - 1, where F! is the factorial of F. The criteria for determining factorial primality involve verifying the primality of the number and its conformity to one of the described factorial expressions. Examples include numbers like 7 and 719, which meet both conditions .
In the modified Caesar cipher, the user-defined offset determines the number of positions each character in the plaintext is shifted along the alphabet. Unlike the standard Caesar cipher, which always uses a shift of one, this method allows for customization of the shift size, thereby increasing the complexity and variety of the ciphered text .
The password strength calculation involves both bonuses and penalties. Bonuses include multiplying the total character count by 4, subtracting the number of uppercase letters from the total and multiplying by 2, subtracting the number of lowercase letters from the total and multiplying by 3, and multiplying the number of non-alphabetic characters by 5. Penalties are applied based on the length of the longest sequence of consecutive lowercase and uppercase letters, multiplied by 2 and 3 respectively. The final score determines password strength, categorized into 'Very Weak', 'Weak', 'Strong', or 'Very Strong' based on the score thresholds .
The algorithm for determining if a number is happy involves repeatedly summing the squares of its digits until a single-digit number is obtained. If this number is 1, the original number is considered happy. The process starts by converting the number into a string to iterate over each digit, then computes the sum of the squares of these digits. This result is applied recursively until either '1' is reached, indicating happiness, or it falls into a cycle that avoids '1', signifying the number is not happy .
The encryption method determines the shift key by calculating for each letter its number of occurrences in the word. If the occurrence is even, the shift is n/2 (integer division); if it's odd, the shift is n*2. This alteration leads to the replacement of each letter with the kth letter that follows it in the alphabet, wrapping around if necessary, thus creating a uniquely encrypted text .
A password score of 127 is interpreted as 'Very Strong' according to the scoring criteria, as it exceeds the 80-point threshold required for the top strength category. This indicates a robust password, supported by a combination of character types and sequences that maximize the bonus and minimize penalties .
Prime primordial numbers hold cryptographic significance as numbers that can be expressed as P# + 1 or P# - 1, where P# is the product of all prime numbers up to P. Verification involves confirming the number's primality and its alignment with the primordial condition. For example, 211 and 30029 are verified as prime primordial numbers because they fulfill these criteria .
The Robinson sequence is developed by analyzing the frequency of each digit appearing in a term, then constructing the next term by concatenating these frequencies followed by the digit itself in descending order. This process begins with U0, set to 0, and each subsequent term Un is derived from the digit counts of Un-1, repeated in increasing sequence complexity as n increases .