Explorar E-books
Categorias
Explorar Audiolivros
Categorias
Explorar Revistas
Categorias
Explorar Documentos
Categorias
Estruturas de dados Algoritmos Preparação para entrevista Prática de tópicos C++ Java P
Cifra de Vigenère
Nível de dificuldade: Médio ● Última atualização: 19 de março de 2022
Vigenere Cipher é um método de criptografia de texto alfabético. Ele usa uma forma
A tabela consiste nos alfabetos escritos 26 vezes em linhas diferentes, cada alfabeto
Exemplo :
Palavra-chave: AYUSH
explicado abaixo.
se
https://www.geeksforgeeks.org/vigenere-cipher/ 1/15
28/03/22, 12:59 Cifra Vigenère - GeeksforGeeks
Criptografia
forma, para a segunda letra do texto simples, a segunda letra da chave é usada, a letra
semelhante.
Descriptografia A
rótulo da coluna como o texto simples. Por exemplo, na linha A (de AYUSH), o texto
vamos para a linha Y (de AYUSH), localize o texto cifrado C que se encontra na coluna E,
Comece sua jornada de Conecte-
Registro
codificação agora!
Criptografia
se
https://www.geeksforgeeks.org/vigenere-cipher/ 2/15
28/03/22, 12:59 Cifra Vigenère - GeeksforGeeks
E i = (P i + K i ) mod 26
Descriptografia
D i = (E i - K i + 26) mod 26
Nota: D i
denota o deslocamento do i-ésimo caractere do texto simples. Como offset de
C++
https://www.geeksforgeeks.org/vigenere-cipher/ 4/15
28/03/22, 12:59 Cifra Vigenère - GeeksforGeeks
class GFG
{
// This function generates the key in
// a cyclic manner until it's length isi'nt
// equal to the length of original text
static String generateKey(String str, String key)
{
int x = str.length();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.length() == str.length())
break;
key+=(key.charAt(i));
}
return key;
}
// This function returns the encrypted text
// generated with the help of the key
static String cipherText(String str, String key)
{
String cipher_text="";
for (int i = 0; i < str.length(); i++)
{
// converting in range 0-25
int x = (str.charAt(i) + key.charAt(i)) %26;
// convert into alphabets(ASCII)
x += 'A';
cipher_text+=(char)(x);
}
return cipher_text;
}
// This function decrypts the encrypted text
// and returns the original text
static String originalText(String cipher_text, String key)
{
String orig_text="";
Comece sua jornada de Conecte-
Registro
codificação agora!
for (int i = 0 ; i < cipher_text.length() &&
i < key.length(); i++)
se
{
// converting in range 0-25
https://www.geeksforgeeks.org/vigenere-cipher/ 5/15
28/03/22, 12:59 Cifra Vigenère - GeeksforGeeks
int x = (cipher_text.charAt(i) -
key.charAt(i) + 26) %26;
// convert into alphabets(ASCII)
x += 'A';
orig_text+=(char)(x);
}
return orig_text;
}
// This function will convert the lower case character to Upper case
static String LowerToUpper(String s)
{
StringBuffer str =new StringBuffer(s);
for(int i = 0; i < s.length(); i++)
{
if(Character.isLowerCase(s.charAt(i)))
{
str.setCharAt(i, Character.toUpperCase(s.charAt(i)));
}
}
s = str.toString();
return s;
}
// Driver code
public static void main(String[] args)
{
String Str = "GEEKSFORGEEKS";
String Keyword = "AYUSH";
String str = LowerToUpper(Str);
String keyword = LowerToUpper(Keyword);
String key = generateKey(str, keyword);
String cipher_text = cipherText(str, key);
System.out.println("Ciphertext : "
+ cipher_text + "\n");
System.out.println("Original/Decrypted Text : "
+ originalText(cipher_text, key));
}
}
Comece sua jornada de
// This code has been contributed by 29AjayKumar
Conecte-
Registro
codificação agora! se
https://www.geeksforgeeks.org/vigenere-cipher/ 6/15
28/03/22, 12:59 Cifra Vigenère - GeeksforGeeks
P ython3
cipher_text = cipherText(string,key)
print("Ciphertext :", cipher_text)
print("Original/Decrypted Text :",
originalText(cipher_text, key))
# This code is contributed
# by Pratik Somwanshi
C#
https://www.geeksforgeeks.org/vigenere-cipher/ 8/15
28/03/22, 12:59 Cifra Vigenère - GeeksforGeeks
cipher_text+=(char)(x);
}
return cipher_text;
}
// This function decrypts the encrypted text
// and returns the original text
static String originalText(String cipher_text, String key)
{
String orig_text="";
for (int i = 0 ; i < cipher_text.Length &&
i < key.Length; i++)
{
// converting in range 0-25
int x = (cipher_text[i] -
key[i] + 26) %26;
// convert into alphabets(ASCII)
x += 'A';
orig_text+=(char)(x);
}
return orig_text;
}
// Driver code
public static void Main(String[] args)
{
String str = "GEEKSFORGEEKS";
String keyword = "AYUSH";
String key = generateKey(str, keyword);
String cipher_text = cipherText(str, key);
Console.WriteLine("Ciphertext : "
+ cipher_text + "\n");
Console.WriteLine("Original/Decrypted Text : "
+ originalText(cipher_text, key));
}
}
/* This code contributed by PrinciRaj1992 */
https://www.geeksforgeeks.org/vigenere-cipher/ 9/15
28/03/22, 12:59 Cifra Vigenère - GeeksforGeeks
<script>
// JavaScript code to implement Vigenere Cipher
// This function generates the key in
// a cyclic manner until it's length isi'nt
// equal to the length of original text
function generateKey(str,key)
{
key=key.split("");
if(str.length == key.length)
return key.join("");
else
{
let temp=key.length;
for (let i = 0;i<(str.length-temp) ; i++)
{
key.push(key[i % ((key).length)])
}
}
return key.join("");
}
// This function returns the encrypted text
// generated with the help of the key
function cipherText(str,key)
{
let cipher_text="";
for (let i = 0; i < str.length; i++)
{
// converting in range 0-25
let x = (str[i].charCodeAt(0) + key[i].charCodeAt(0)) %26;
// convert into alphabets(ASCII)
x += 'A'.charCodeAt(0);
cipher_text+=String.fromCharCode(x);
}
return cipher_text;
}
// This function decrypts the encrypted text
Saída
Geeks.
Por favor, escreva comentários se encontrar algo incorreto ou se quiser compar tilhar
Como 18
Anterior Próximo
https://www.geeksforgeeks.org/vigenere-cipher/ 12/15
28/03/22, 12:59 Cifra Vigenère - GeeksforGeeks
polialfabética
08, 20 de junho
Cifra Nula
06
XOR Cifra 13, 17 de julho
02 18, 16 de novembro
GeeksforGeeks
Vote na dificuldade
Dificuldade atual:
Médio
Escrevendo código no comentário? Por favor, use ide.geeksforgeeks.org , gere link e compartilhe o link aqui.
Carregar comentários
5º Andar, A-118,
feedback@geeksforgeeks.org
Companhia Aprender
Sobre nós Algoritmos
Carreiras Estruturas de dados
Em média Folha de dicas SDE
Contate-Nos Aprendizado de máquina
Política de Privacidade Assuntos CS
Política de direitos autorais Tutoriais em vídeo
Notícias línguas
Manchetes Pitão
Tecnologia Java
JavaScript Estágios
Bootstrap Estágio em vídeo
https://www.geeksforgeeks.org/vigenere-cipher/ 15/15