📜  根据给定技术加密和解密字符串

📅  最后修改于: 2021-09-08 15:07:14             🧑  作者: Mango

给定一个字符串S ,任务是加密该字符串并再次解密该字符串为原始形式。
加密技术:如果L是字符串的长度,则取两个值,一个是√L的ceil(比如b),另一个是√L的地板(比如a),并制作一个二维矩阵,行数= a,列 = b。
如果行*列 < L,则增加 a 或 b 的值,以最小值为准。充满原始字符串顺序的字符矩阵。得到矩阵后,按列读取矩阵并打印得到的字符串。

解密技巧:如果L是加密字符串的长度,则再次求出两个值a和b,其中a是√L的ceil值,b是√L的下限值。同样创建一个二维矩阵,其中按列存储字符串并按行读取矩阵以获取原始形式的字符串。

加密方法:

  • 求字符串的长度L。
  • 找到√Length 的ceil 和floor 值并将它们分配给变量。
  • 检查两个变量的乘积是否 >= 长度,如果不是,则将具有较小值的变量增加 1。
  • 创建一个二维矩阵并按行填充字符串的字符。
  • 逐列读取矩阵以获取加密字符串。

解密方法:

  • 求字符串的长度L。
  • 找到√Length 的ceil 和floor 值并将它们分配给变量。
  • 创建一个 2D 矩阵并按列的字符串字符填充矩阵。
  • 逐行读取矩阵以获取解密后的字符串。

下面是上述方法的实现:

C++
// C++ implementation for Custom
// Encryption and Decryption of String
 
#include 
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


输出:
Gsree  keFGskoe 
Geeks For Geeks

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程