📜  异或密码

📅  最后修改于: 2022-05-13 01:57:06.918000             🧑  作者: Mango

异或密码

XOR Encryption 是一种用于加密数据的加密方法,很难通过暴力破解,即生成随机加密密钥以匹配正确的密钥。

下面是一个简单的 C++ 实现。实现的概念是首先定义 XOR - 加密密钥,然后使用您要加密的密钥对 String 中的字符执行 XOR 操作。要解密加密字符,我们必须使用定义的密钥再次执行 XOR 操作。在这里,我们正在加密整个字符串。

C++
// C++ program to implement XOR - Encryption
#include
 
// The same function is used to encrypt and
// decrypt
void encryptDecrypt(char inpString[])
{
    // Define XOR key
    // Any character value will work
    char xorKey = 'P';
 
    // calculate length of input string
    int len = strlen(inpString);
 
    // perform XOR operation of key
    // with every character in string
    for (int i = 0; i < len; i++)
    {
        inpString[i] = inpString[i] ^ xorKey;
        printf("%c",inpString[i]);
    }
}
 
// Driver program to test above function
int main()
{
    char sampleString[] = "GeeksforGeeks";
 
    // Encrypt the string
    printf("Encrypted String: ");
    encryptDecrypt(sampleString);
    printf("\n");
 
    // Decrypt the string
    printf("Decrypted String: ");
    encryptDecrypt(sampleString);
 
    return 0;
}


Java
// Java program to implement XOR - Encryption
class XOREncryption
{
    // The same function is used to encrypt and
    // decrypt
    static String encryptDecrypt(String inputString)
    {
        // Define XOR key
        // Any character value will work
        char xorKey = 'P';
 
        // Define String to store encrypted/decrypted String
        String outputString = "";
 
        // calculate length of input string
        int len = inputString.length();
 
        // perform XOR operation of key
        // with every character in string
        for (int i = 0; i < len; i++)
        {
            outputString = outputString +
            Character.toString((char) (inputString.charAt(i) ^ xorKey));
        }
 
        System.out.println(outputString);
        return outputString;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String sampleString = "GeeksforGeeks";
 
        // Encrypt the string
        System.out.println("Encrypted String");
        String encryptedString = encryptDecrypt(sampleString);
 
        // Decrypt the string
        System.out.println("Decrypted String");
        encryptDecrypt(encryptedString);
    }
}
 
// This code is contributed by Vivekkumar Singh


Python3
# Python3 program to implement XOR - Encryption
 
# The same function is used to encrypt and
# decrypt
def encryptDecrypt(inpString):
 
    # Define XOR key
    # Any character value will work
    xorKey = 'P';
 
    # calculate length of input string
    length = len(inpString);
 
    # perform XOR operation of key
    # with every character in string
    for i in range(length):
     
        inpString = (inpString[:i] +
             chr(ord(inpString[i]) ^ ord(xorKey)) +
                     inpString[i + 1:]);
        print(inpString[i], end = "");
     
    return inpString;
 
# Driver Code
if __name__ == '__main__':
    sampleString = "GeeksforGeeks";
 
    # Encrypt the string
    print("Encrypted String: ", end = "");
    sampleString = encryptDecrypt(sampleString);
    print("\n");
 
    # Decrypt the string
    print("Decrypted String: ", end = "");
    encryptDecrypt(sampleString);
 
# This code is contributed by Princi Singh


C#
// C# program to implement XOR - Encryption
using System;
     
public class XOREncryption
{
    // The same function is used to encrypt and
    // decrypt
    static String encryptDecrypt(String inputString)
    {
        // Define XOR key
        // Any character value will work
        char xorKey = 'P';
 
        // Define String to store encrypted/decrypted String
        String outputString = "";
 
        // calculate length of input string
        int len = inputString.Length;
 
        // perform XOR operation of key
        // with every character in string
        for (int i = 0; i < len; i++)
        {
            outputString = outputString +
            char.ToString((char) (inputString[i] ^ xorKey));
        }
 
        Console.WriteLine(outputString);
        return outputString;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String sampleString = "GeeksforGeeks";
 
        // Encrypt the string
        Console.WriteLine("Encrypted String");
        String encryptedString = encryptDecrypt(sampleString);
 
        // Decrypt the string
        Console.WriteLine("Decrypted String");
        encryptDecrypt(encryptedString);
    }
}
 
// This code has been contributed by 29AjayKumar


输出:

Encrypted String: 55;#6?"55;#
Decrypted String: GeeksforGeeks

异或加密背后的基本思想是,如果在解密加密数据之前不知道异或加密密钥,就不可能解密数据。例如,如果您对两个未知变量进行异或运算,则无法判断这些变量的输出是什么。考虑操作 A XOR B,这将返回 true。现在,如果一个变量的值是已知的,我们就可以知道另一个变量的值。如果 A 为真,则 B 应为假,或者如果 A 为假,则 B 应为真,根据布尔异或运算的属性。在不知道其中一个值的情况下,我们无法解密数据,这个想法用于 XOR - 加密。