Dada una string S , la tarea es cifrar la string y descifrarla nuevamente a la forma original.
Técnica de cifrado: si L es la longitud de la string, entonces tome dos valores, uno el techo de √L (digamos b) y el otro mínimo de √L (digamos a), y haga una array bidimensional con filas = a, y columnas = b.
Si filas*columnas < L, aumente el valor de a o b, el que sea mínimo. Rellene la array con los caracteres de la string original de forma secuencial. Después de obtener la array, lea la array por columnas e imprima la string obtenida.
Técnica de descifrado: si L es la longitud de la string cifrada, vuelva a encontrar los dos valores a y b, donde a es el valor máximo de √L y b es el valor mínimo de √L. Del mismo modo, cree una array 2D en la que almacene la string en forma de columna y lea la array en forma de fila para obtener la string en la forma original.
Enfoque de cifrado:
- Encuentra la longitud L de la cuerda.
- Encuentre los valores máximo y mínimo de √Longitud y asígnelos a las variables.
- Compruebe si el producto de las dos variables >= Longitud, si no, entonces incrementa la variable que tiene un valor menor en 1.
- Cree una array 2D y rellene los caracteres de la string por filas.
- Lea la array en forma de columna para obtener la string cifrada.
Enfoque de descifrado:
- Encuentra la longitud L de la cuerda.
- Encuentre los valores máximo y mínimo de √Longitud y asígnelos a las variables.
- Cree una array 2D y llene la array con caracteres de string en forma de columna.
- Lea la array por filas para obtener la string descifrada.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation for Custom // Encryption and Decryption of String #include <bits/stdc++.h> using namespace std; // Function to encrypt the string string encryption(string s) { int l = s.length(); int b = ceil(sqrt(l)); int a = floor(sqrt(l)); string encrypted; if (b * a < l) { if (min(b, a) == b) { b = b + 1; } else { a = a + 1; } } // Matrix to generate the // Encrypted String char arr[a][b]; memset(arr, ' ', sizeof(arr)); int k = 0; // Fill the matrix row-wise for (int j = 0; j < a; j++) { for (int i = 0; i < b; i++) { if (k < l){ arr[j][i] = s[k]; } k++; } } // Loop to generate // encrypted string for (int j = 0; j < b; j++) { for (int i = 0; i < a; i++) { encrypted = encrypted + arr[i][j]; } } return encrypted; } // Function to decrypt the string string decryption(string s){ int l = s.length(); int b = ceil(sqrt(l)); int a = floor(sqrt(l)); string decrypted; // Matrix to generate the // Encrypted String char arr[a][b]; memset(arr, ' ', sizeof(arr)); int k = 0; // Fill the matrix column-wise for (int j = 0; j < b; j++) { for (int i = 0; i < a; i++) { if (k < l){ arr[j][i] = s[k]; } k++; } } // Loop to generate // decrypted string for (int j = 0; j < a; j++) { for (int i = 0; i < b; i++) { decrypted = decrypted + arr[i][j]; } } return decrypted; } // Driver Code int main() { string s = "Geeks For Geeks"; string encrypted; string decrypted; // Encryption of String encrypted = encryption(s); cout << encrypted << endl; // Decryption of String decrypted = decryption(encrypted); cout << decrypted; return 0; }
Java
// Java implementation for Custom // Encryption and Decryption of String class GFG { // Function to encrypt the String static String encryption(char[] s) { int l = s.length; int b = (int) Math.ceil(Math.sqrt(l)); int a = (int) Math.floor(Math.sqrt(l)); String encrypted = ""; if (b * a < l) { if (Math.min(b, a) == b) { b = b + 1; } else { a = a + 1; } } // Matrix to generate the // Encrypted String char [][]arr = new char[a][b]; int k = 0; // Fill the matrix row-wise for (int j = 0; j < a; j++) { for (int i = 0; i < b; i++) { if (k < l) { arr[j][i] = s[k]; } k++; } } // Loop to generate // encrypted String for (int j = 0; j < b; j++) { for (int i = 0; i < a; i++) { encrypted = encrypted + arr[i][j]; } } return encrypted; } // Function to decrypt the String static String decryption(char []s) { int l = s.length; int b = (int) Math.ceil(Math.sqrt(l)); int a = (int) Math.floor(Math.sqrt(l)); String decrypted=""; // Matrix to generate the // Encrypted String char [][]arr = new char[a][b]; int k = 0; // Fill the matrix column-wise for (int j = 0; j < b; j++) { for (int i = 0; i < a; i++) { if (k < l) { arr[j][i] = s[k]; } k++; } } // Loop to generate // decrypted String for (int j = 0; j < a; j++) { for (int i = 0; i < b; i++) { decrypted = decrypted + arr[i][j]; } } return decrypted; } // Driver Code public static void main(String[] args) { String s = "Geeks For Geeks"; String encrypted; String decrypted; // Encryption of String encrypted = encryption(s.toCharArray()); System.out.print(encrypted +"\n"); // Decryption of String decrypted = decryption(encrypted.toCharArray()); System.out.print(decrypted); } } // This code is contributed by PrinciRaj1992
Python3
# Python3 implementation for Custom # Encryption and Decryption of String from math import ceil,floor,sqrt # Function to encrypt the def encryption(s): l = len(s) b = ceil(sqrt(l)) a = floor(sqrt(l)) encrypted="" if (b * a < l): if (min(b, a) == b): b = b + 1 else: a = a + 1 # Matrix to generate the # Encrypted String arr = [[' ' for i in range(a)] for j in range(b)] k = 0 # Fill the matrix row-wise for j in range(a): for i in range(b): if (k < l): arr[j][i] = s[k] k += 1 # Loop to generate # encrypted for j in range(b): for i in range(a): encrypted = encrypted + arr[i][j] return encrypted # Function to decrypt the def decryption(s): l = len(s) b = ceil(sqrt(l)) a = floor(sqrt(l)) decrypted="" # Matrix to generate the # Encrypted String arr = [[' ' for i in range(a)] for j in range(b)] k = 0 # Fill the matrix column-wise for j in range(b): for i in range(a): if (k < l): arr[j][i] = s[k] k += 1 # Loop to generate # decrypted for j in range(a): for i in range(b): decrypted = decrypted + arr[i][j] return decrypted # Driver Code s = "Geeks For Geeks" encrypted="" decrypted="" # Encryption of String encrypted = encryption(s) print(encrypted) # Decryption of String decrypted = decryption(encrypted) print(decrypted) # This code is contributed by mohit kumar 29
C#
// C# implementation for Custom // Encryption and Decryption of String using System; class GFG { // Function to encrypt the String static String encryption(char[] s) { int l = s.Length; int b = (int) Math.Ceiling(Math.Sqrt(l)); int a = (int) Math.Floor(Math.Sqrt(l)); String encrypted = ""; if (b * a < l) { if (Math.Min(b, a) == b) { b = b + 1; } else { a = a + 1; } } // Matrix to generate the // Encrypted String char [,]arr = new char[a, b]; int k = 0; // Fill the matrix row-wise for (int j = 0; j < a; j++) { for (int i = 0; i < b; i++) { if (k < l) { arr[j, i] = s[k]; } k++; } } // Loop to generate // encrypted String for (int j = 0; j < b; j++) { for (int i = 0; i < a; i++) { encrypted = encrypted + arr[i, j]; } } return encrypted; } // Function to decrypt the String static String decryption(char []s) { int l = s.Length; int b = (int) Math.Ceiling(Math.Sqrt(l)); int a = (int) Math.Floor(Math.Sqrt(l)); String decrypted=""; // Matrix to generate the // Encrypted String char [,]arr = new char[a, b]; int k = 0; // Fill the matrix column-wise for (int j = 0; j < b; j++) { for (int i = 0; i < a; i++) { if (k < l) { arr[j, i] = s[k]; } k++; } } // Loop to generate // decrypted String for (int j = 0; j < a; j++) { for (int i = 0; i < b; i++) { decrypted = decrypted + arr[i, j]; } } return decrypted; } // Driver Code public static void Main(String[] args) { String s = "Geeks For Geeks"; String encrypted; String decrypted; // Encryption of String encrypted = encryption(s.ToCharArray()); Console.Write(encrypted +"\n"); // Decryption of String decrypted = decryption(encrypted.ToCharArray()); Console.Write(decrypted); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // JavaScript implementation for Custom // Encryption and Decryption of let // Function to encrypt the let function encryption(s) { let l = s.length; let b = Math.ceil(Math.sqrt(l)); let a = Math.floor(Math.sqrt(l)); let encrypted = ''; if (b * a < l) { if (Math.min(b, a) == b) { b = b + 1; } else { a = a + 1; } } // Matrix to generate the // Encrypted let let arr = new Array(); for (let i = 0; i < a; i++) { let temp = []; for (let j = 0; j < b; j++) { temp.push([]) } arr.push(temp) } for (let i = 0; i < a; i++) { for (let j = 0; j < b; j++) { arr[i][j] = " " } } let k = 0; // Fill the matrix row-wise for (let j = 0; j < a; j++) { for (let i = 0; i < b; i++) { if (k < l) { arr[j][i] = s[k]; } k++; } } // Loop to generate // encrypted let for (let j = 0; j < b; j++) { for (let i = 0; i < a; i++) { encrypted = encrypted + arr[i][j]; } } return encrypted; } // Function to decrypt the let function decryption(s) { let l = s.length; let b = Math.ceil(Math.sqrt(l)); let a = Math.floor(Math.sqrt(l)); let decrypted = ''; // Matrix to generate the // Encrypted let let arr = new Array(); for (let i = 0; i < a; i++) { let temp = []; for (let j = 0; j < b; j++) { temp.push([]) } arr.push(temp) } for (let i = 0; i < a; i++) { for (let j = 0; j < b; j++) { arr[i][j] = " " } } let k = 0; // Fill the matrix column-wise for (let j = 0; j < b; j++) { for (let i = 0; i < a; i++) { if (k < l) { arr[j][i] = s[k]; } k++; } } // Loop to generate // decrypted let for (let j = 0; j < a; j++) { for (let i = 0; i < b; i++) { decrypted = decrypted + arr[i][j]; } } return decrypted; } // Driver Code let s = "Geeks For Geeks"; let encrypted; let decrypted; // Encryption of let encrypted = encryption(s); document.write(encrypted + "<br>"); // Decryption of let decrypted = decryption(encrypted); document.write(decrypted); // This code is contributed by gfgking </script>
Gsree keFGskoe Geeks For Geeks
Publicación traducida automáticamente
Artículo escrito por mishrakishan1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA