📜  根据给定的技术对字符串进行加密和解密

📅  最后修改于: 2021-06-28 20:31:28             🧑  作者: Mango

给定一个字符串S,任务是加密的字符串,并再次解密字符串到原来的形式。
加密技术:如果L是字符串的长度,则取两个值,一个为√ L的ceil(例如b),另一个为√ L的底面(例如a),然后使二维矩阵具有行= a,列= b。
如果row * columns
解密技术:如果L是加密字符串的长度,则再次找到两个值a和b,其中a是√ L的ceil值,b是√ L的下限值。类似地,创建一个2D矩阵,其中将字符串列存储,并按行读取矩阵以获取原始形式的字符串。

加密方式:

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

解密方式:

  • 找到字符串的长度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


输出:
Gsree  keFGskoe 
Geeks For Geeks