c++ 凯撒密码

这篇具有很好参考价值的文章主要介绍了c++ 凯撒密码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

        凯撒密码(Caesar Cipher)是一种简单的替代加密技术,以罗马大帝凯撒·尤利乌斯·凯撒(Julius Caesar)的名字命名。它通过将每个字母按照字母表向后移动固定数量的位置来进行加密。

示例一:

/**
 * @file caesar_cipher.cpp
 * @brief Implementation of [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher) algorithm.
 *
 * @details
 * In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, 
 * Caesar's code or Caesar shift, is one of the simplest and most widely known encryption 
 * techniques. It is a type of substitution cipher in which each letter in the plaintext 
 * is replaced by a letter some fixed number of positions down the alphabet. For example, 
 * with a left shift of 3, D would be replaced by A, E would become B, and so on. 
 * The method is named after Julius Caesar, who used it in his private correspondence.
 *
 * ### Algorithm
 * The encryption can also be represented using modular arithmetic by first transforming 
 * the letters into numbers, according to the scheme, A → 0, B → 1, ..., Z → 25.
 * Encryption of a letter x by a shift n can be described mathematically as,
 * \f[ E(x) = (x + n)\;\mbox{mod}\; 26\f]
 * while decryption can be described as,
 * \f[ D(x) = (x - n) \;\mbox{mod}\; 26\f]
 * 
 * \note This program implements caesar cipher for only uppercase English alphabet characters (i.e. A-Z). 
 * 
 * @author [Deep Raval](https://github.com/imdeep2905)
 */
#include <iostream>
#include <string>
#include <cassert>

/** \namespace ciphers
 * \brief Algorithms for encryption and decryption
 */
namespace ciphers {
    /** \namespace caesar
     * \brief Functions for [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher) algorithm.
     */
    namespace caesar {   
        namespace {
            /**
             * This function finds character for given value (i.e.A-Z)
             * @param x value for which we want character 
             * @returns  corresponding character for perticular value
             */        
            inline char get_char(const int x) {
                // By adding 65 we are scaling 0-25 to 65-90. 
                // Which are in fact ASCII values of A-Z. 
                return char(x + 65); 
            }
            /**
             * This function finds value for given character (i.e.0-25)
             * @param c character for which we want value
             * @returns returns corresponding value for perticular character
             */  
            inline int get_value(const char c) {
                // A-Z have ASCII values in range 65-90.
                // Hence subtracting 65 will scale them to 0-25.
                return int(c - 65);
            }
        } // Unnamed namespace
        /**
         * Encrypt given text using caesar cipher.
         * @param text text to be encrypted
         * @param shift number of shifts to be applied
         * @returns new encrypted text
         */
        std::string encrypt (const std::string &text, const int &shift) {
            std::string encrypted_text = ""; // Empty string to store encrypted text
            for (char c : text) { // Going through each character
                int place_value = get_value(c); // Getting value of character (i.e. 0-25)
                place_value = (place_value + shift) % 26; // Applying encryption formula
                char new_char = get_char(place_value); // Getting new character from new value (i.e. A-Z)
                encrypted_text += new_char; // Appending encrypted character
            }
            return encrypted_text; // Returning encrypted text
        }
        /**
         * Decrypt given text using caesar cipher.
         * @param text text to be decrypted
         * @param shift number of shifts to be applied
         * @returns new decrypted text
         */        
        std::string decrypt (const std::string &text, const int &shift) {
            std::string decrypted_text = ""; // Empty string to store decrypted text
            for (char c : text) { // Going through each character
                int place_value = get_value(c); // Getting value of character (i.e. 0-25)
                place_value = (place_value - shift) % 26;// Applying decryption formula
                if(place_value < 0) { // Handling case where remainder is negative 
                    place_value = place_value + 26;
                }
                char new_char = get_char(place_value); // Getting original character from decrypted value (i.e. A-Z)
                decrypted_text += new_char; // Appending decrypted character
            }
            return decrypted_text; // Returning decrypted text
        }
    } // namespace caesar
} // namespace ciphers

/**
 * Function to test above algorithm
 */
void test() {
    // Test 1
    std::string text1 = "ALANTURING";
    std::string encrypted1 = ciphers::caesar::encrypt(text1, 17);
    std::string decrypted1 = ciphers::caesar::decrypt(encrypted1, 17);
    assert(text1 == decrypted1);
    std::cout << "Original text : " << text1;
    std::cout << " , Encrypted text (with shift = 21) : " << encrypted1;
    std::cout << " , Decrypted text : "<< decrypted1 << std::endl;
    // Test 2
    std::string text2 = "HELLOWORLD";
    std::string encrypted2 = ciphers::caesar::encrypt(text2, 1729);
    std::string decrypted2 = ciphers::caesar::decrypt(encrypted2, 1729);
    assert(text2 == decrypted2);
    std::cout << "Original text : " << text2;
    std::cout << " , Encrypted text (with shift = 1729) : " << encrypted2;
    std::cout << " , Decrypted text : "<< decrypted2 << std::endl;
}

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

示例二:

使用C++实现凯撒密码的示例:

#include <iostream>
#include <string>

using namespace std;

// 凯撒密码加密函数
string encryptCaesarCipher(string plaintext, int shift) {
    string ciphertext = "";
    
    for (int i = 0; i < plaintext.length(); i++) {
        if (isalpha(plaintext[i])) {
            // 获取字符的ASCII码
            int ascii = int(plaintext[i]);
            
            // 根据移动位数进行替代
            if (islower(plaintext[i])) {
                ascii = (ascii - 97 + shift) % 26 + 97;
            } else {
                ascii = (ascii - 65 + shift) % 26 + 65;
            }
            
            // 将替代后的字符添加到密文中
            ciphertext += char(ascii);
        } else {
            // 非字母字符直接添加到密文中
            ciphertext += plaintext[i];
        }
    }
    
    return ciphertext;
}

// 凯撒密码解密函数
string decryptCaesarCipher(string ciphertext, int shift) {
    string plaintext = "";
    
    // 将移位数转换为负数,以实现向前移动的效果
    shift = -shift;
    
    // 调用加密函数进行解密
    plaintext = encryptCaesarCipher(ciphertext, shift);
    
    return plaintext;
}

int main() {
    string plaintext = "Hello, World!";
    int shift = 3;
    
    // 加密并输出密文
    string ciphertext = encryptCaesarCipher(plaintext, shift);
    cout << "Ciphertext: " << ciphertext << endl;
    
    // 解密并输出明文
    string decryptedText = decryptCaesarCipher(ciphertext, shift);
    cout << "Decrypted Text: " << decryptedText << endl;
    
    return 0;
}
        在上述示例代码中,`encryptCaesarCipher`函数实现了凯撒密码的加密过程,`decryptCaesarCipher`函数实现了凯撒密码的解密过程。使用`main`函数来测试加密和解密功能,并输出结果。

        注意,该凯撒密码实现只对字母字符进行加密和解密,非字母字符将保持原样。同时,移位数应为正整数,表示向后移动的位数。解密过程是通过将移位数转换为负数,然后调用加密函数实现的。文章来源地址https://www.toymoban.com/news/detail-781898.html

到了这里,关于c++ 凯撒密码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 凯撒密码(Python)

    一、凯撒密码是什么? 凯撒密码是一种非常古老的加密方法,最初由古罗马的凯撒大帝使用。凯撒大帝将纸条卷起后,按照字母表顺序向后移动一定的位数,然后再写下加密后的字母,这样其他人就无法直接读取他的信息。 凯撒密码(移位密码):是一种替换加密,明文中的

    2024年02月05日
    浏览(20)
  • 凯撒密码-java

    凯撒密码-java

    在 密码学 中, 恺撒密码 (英语:Caesar cipher),或称 恺撒加密 、 恺撒变换 、 变换加密 ,是一种最简单且最广为人知的加密技术。它是一种替换加密的技术, 明文 中的所有字母都在 字母表 上向后(或向前)按照一个固定数目进行偏移后被替换成 密文 。例如,当偏移量

    2024年02月07日
    浏览(9)
  • C# 实现 凯撒密码

            凯撒密码(Caesar cipher)是一种简单的替换式密码,它通过将明文中的每个字母在字母表中向后(或向前)按照一个固定数目进行偏移后被替换成密文。这种加密方式是以罗马共和时期恺撒的名字命名的,据说恺撒曾用此方法对其重要的军事信息进行加密。     

    2024年01月22日
    浏览(19)
  • 加密与安全_ 凯撒密码

    加密与安全_ 凯撒密码

    PKI - 02 对称与非对称密钥算法 凯撒密码是一种简单的替换加密技术,也称为移位密码。它是古典密码学中最早的密码之一,得名于古罗马军队领袖凯撒·尤利乌斯(Julius Caesar),据说他曾经使用过这种加密方法。 恺撒密码,也称为恺撒加密或恺撒变换,是一种最古老且最简

    2024年03月15日
    浏览(11)
  • 浅析加密算法一【凯撒密码】

    浅析加密算法一【凯撒密码】

    在密码学中, 恺撒密码 (英语: Caesar cipher ),或称 恺撒加密、恺撒变换、变换加密 ,是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是 3 3

    2023年04月10日
    浏览(12)
  • Python学习38:凯撒密码——解密

    Python学习38:凯撒密码——解密

    类型: 字符串 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬ 描述

    2024年02月06日
    浏览(7)
  • C语言实现简单加密算法 凯撒密码 RSA算法 简介及实现

    C语言实现简单加密算法 凯撒密码 RSA算法 简介及实现

    凯撒密码的核心思想就是移位。 将明文的每一个字符 在 密码系统所支持字符序列中向右平移N,映射得到新的字符从而实现加密,而解密则相反向左平移N。加密的Key即为N。 加密  解密 在如今的万维网环境中,如果A要向B发送数据,需要先加密这个数据,因为在一些不安全

    2024年02月08日
    浏览(18)
  • 凯撒密码_c++_java_python(加密及解密)

    公元前100多年凯撒发明的一种密码,简单来说是平移密码,也就是将字母位置向后移动一定位数。 如原文是ABCXYZ,密钥为3,加密后就是DEFABC。 以密钥的数字向后平移了三位,如果密钥是5就是平移五位。 解密就是把加密的文字进行还原。 我们通过对题目的分析可以发现,加

    2024年02月06日
    浏览(8)
  • 字节凯撒爆破

    2024年02月06日
    浏览(8)
  • 凯撒加解密和破解

    凯撒加解密和破解

            古典密码学是最基础的密码学问题,在古典密码学中,最为经典的就是凯撒密码。我们在这里简单介绍一下凯撒密码。         凯撒密码又称为凯撒加密,凯撒变换,变换加密,是一种最简单且为广为人知的加密技术。他就是一种替换加密。在明文中使用字母

    2024年02月11日
    浏览(9)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包