c++ XOR 密码

        XOR密码,也被称为异或密码,是一种简单和常见的加密算法。XOR操作(异或操作)是一种逻辑运算,它比较两个输入并输出一个结果,结果为1的条件是两个输入不相等。

        XOR密码的原理是将明文与密钥按位进行异或操作,生成加密后的密文。解密时,再将密文与相同的密钥按位进行异或操作,就能还原出原始的明文。

XOR密码有以下特点:

  1. 异或操作是可逆的,即加密和解密使用相同的操作。
  2. 如果使用的密钥是随机且和明文长度相等的比特串,XOR密码可以提供良好的保密性。
  3. 如果使用的密钥是可预测的,XOR密码容易受到密码分析的攻击。

下面是两个简单的示例代码,展示了如何使用XOR密码进行加密和解密: 

示例一:

/**
 * @file xor_cipher.cpp
 * @brief Implementation of [XOR cipher](https://en.wikipedia.org/wiki/XOR_cipher) algorithm.
 *
 * @details
 * In cryptography, the simple XOR cipher is a type of additive cipher, an encryption 
 * algorithm that operates according to the principles: 
 *
 * * \f$A {\oplus} 0 = A\f$
 * * \f$A {\oplus} A = 0\f$
 * * \f$ (A {\oplus} B) {\oplus} C = A {\oplus} (B {\oplus} C)\f$
 * * \f$ (B {\oplus} A) {\oplus} B = B {\oplus} 0 = B \f$
 * 
 * 
 * where \f$\oplus\f$ symbol denotes the exclusive disjunction (XOR) operation.
 * This operation is sometimes called modulus 2 addition (or subtraction, which is identical).
 * With this logic, a string of text can be encrypted by applying the bitwise XOR operator to
 * every character using a given key. To decrypt the output, merely reapplying the XOR function 
 * with the key will remove the cipher.
 * 
 * ### Algorithm
 * Choose the key for encryption and apply XOR operation to each character of a string. 
 * Reapplying XOR operation to each character of encrypted string will give original string back.
 * 
 * \note This program implements XOR Cipher for string with ASCII characters. 
 * 
 * @author [Deep Raval](https://github.com/imdeep2905)
 */
#include <iostream>
#include <string>
#include <cassert>

/** \namespace ciphers
 * \brief Algorithms for encryption and decryption
 */
namespace ciphers {
    /** \namespace XOR
     * \brief Functions for [XOR cipher](https://en.wikipedia.org/wiki/XOR_cipher) algorithm.
     */
    namespace XOR {   
        /**
         * Encrypt given text using XOR cipher.
         * @param text text to be encrypted
         * @param key to be used for encyption
         * @return new encrypted text
         */
        std::string encrypt (const std::string &text, const int &key) {
            std::string encrypted_text = ""; // Empty string to store encrypted text
            for (auto &c: text) { // Going through each character
                char encrypted_char = char(c ^ key); // Applying encyption
                encrypted_text += encrypted_char; // Appending encrypted character
            }
            return encrypted_text; // Returning encrypted text
        }
        /**
         * Decrypt given text using XOR cipher.
         * @param text text to be encrypted
         * @param key to be used for decryption
         * @return new decrypted text
         */        
        std::string decrypt (const std::string &text, const int &key) {
            std::string decrypted_text = ""; // Empty string to store decrypted text
            for (auto &c : text) { // Going through each character
                char decrypted_char = char(c ^ key); // Applying decryption
                decrypted_text += decrypted_char; // Appending decrypted character
            }
            return decrypted_text; // Returning decrypted text
        }
    } // namespace XOR
} // namespace ciphers

/**
 * Function to test above algorithm
 */
void test() {
    // Test 1
    std::string text1 = "Whipalsh! : Do watch this movie...";
    std::string encrypted1 = ciphers::XOR::encrypt(text1, 17);
    std::string decrypted1 = ciphers::XOR::decrypt(encrypted1, 17);
    assert(text1 == decrypted1);
    std::cout << "Original text : " << text1;
    std::cout << " , Encrypted text (with key = 17) : " << encrypted1;
    std::cout << " , Decrypted text : "<< decrypted1 << std::endl;
    // Test 2
    std::string text2 = "->Valar M0rghulis<-";
    std::string encrypted2 = ciphers::XOR::encrypt(text2, 29);
    std::string decrypted2 = ciphers::XOR::decrypt(encrypted2, 29);
    assert(text2 == decrypted2);
    std::cout << "Original text : " << text2;
    std::cout << " , Encrypted text (with key = 29) : " << encrypted2;
    std::cout << " , Decrypted text : "<< decrypted2 << std::endl;
}

/** Driver Code */
int main() {
    // Testing
    test();
    return 0;
}

示例二:

#include <iostream>
#include <string>

std::string xorEncryptDecrypt(const std::string& text, const std::string& key)
{
    std::string result = text;
    for (int i = 0; i < text.length(); i++)
    {
        result[i] = text[i] ^ key[i % key.length()];
    }
    return result;
}

int main()
{
    std::string plainText = "Hello World";
    std::string key = "SecretKey";

    std::string encryptedText = xorEncryptDecrypt(plainText, key);
    std::cout << "Encrypted Text: " << encryptedText << std::endl;

    std::string decryptedText = xorEncryptDecrypt(encryptedText, key);
    std::cout << "Decrypted Text: " << decryptedText << std::endl;

    return 0;
}
        在上面的代码中,`xorEncryptDecrypt`函数接受明文和密钥作为参数,并将其转换为密文或解密为明文。加密和解密使用相同的操作:针对明文和密钥的每个字符,执行异或操作。最后,将结果返回为字符串。

        请注意,XOR密码并不是一个强大的加密算法,不适用于保护敏感数据。在实际应用中,应该使用更安全的加密算法,如AES或RSA。

  • 21
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值