21xrx.com
2025-04-13 08:36:29 Sunday
文章检索 我的文章 写文章
C++实现常见加密算法
2023-07-08 14:34:51 深夜i     15     0
C++ encryption algorithms implementation common

C++是一门常用的程序设计语言,也是实现加密算法的一种途径。加密算法是通过对数据进行转换,使其难以被未经授权的用户读取或理解,以保护数据的安全性。下面介绍一些常见的加密算法及其在C++中的实现。

1. Caesar密码

Caesar密码是一种简单的加密算法,它通过对明文进行每个字符位移一定的距离来获得密文。在C++中,可以使用字符串的ASCII码实现Caesar密码加密过程。具体实现如下:

string Caesar_Encrypt(string plaintext, int offset){
  string ciphertext = plaintext;
  for (int i = 0; i < plaintext.length(); i++) {
    ciphertext[i] = 'A' + ((plaintext[i] - 'A' + offset) % 26);
  }
  return ciphertext;
}
string Caesar_Decrypt(string ciphertext, int offset){
  string plaintext = ciphertext;
  for (int i = 0; i < ciphertext.length(); i++) {
    plaintext[i] = 'A' + ((ciphertext[i] - 'A' - offset + 26) % 26);
  }
  return plaintext;
}

2. DES加密

DES(Data Encryption Standard)是一种广泛使用的对称加密算法,其中输入数据分成64位的明文块,使用56位的密钥进行加密,输出64位的密文块。C++中可以使用OpenSSL库来实现DES加密,如下所示:

#include <openssl/des.h>
string DES_Encrypt(string plaintext, string key){
  DES_cblock keyEncrypt;
  DES_cblock ivec;
  DES_key_schedule keySchedule;
  memset((void*)keyEncrypt, 0, 8);
  for (int i = 0; i < key.length() && i < 8; i++) {
    keyEncrypt[i] = key[i];
  }
  memset((void*)ivec, 0, 8);
  DES_set_key_checked(&keyEncrypt, &keySchedule);
  string ciphertext = "";
  const_DES_cblock inputText;
  DES_cblock outputText;
  int inputLen = 0, len;
  while (inputLen < plaintext.length()) {
    memset((void*)inputText, 0, 8);
    len = 8;
    if (inputLen + len > plaintext.length()) {
      len = plaintext.length() - inputLen;
    }
    memcpy(inputText, plaintext.c_str() + inputLen, len);
    DES_ncbc_encrypt(inputText, outputText, len, &keySchedule, &ivec, DES_ENCRYPT);
    inputLen += len;
    for (int i = 0; i < 8; i++) {
      ciphertext += outputText[i];
    }
  }
  return ciphertext;
}
string DES_Decrypt(string ciphertext, string key){
  DES_cblock keyEncrypt;
  DES_cblock ivec;
  DES_key_schedule keySchedule;
  memset(keyEncrypt, 0, 8);
  for (int i = 0; i < key.length() && i < 8; i++) {
    keyEncrypt[i] = key[i];
  }
  memset(ivec, 0, 8);
  DES_set_key_checked(&keyEncrypt, &keySchedule);
  string plaintext = "";
  const_DES_cblock inputText;
  DES_cblock outputText;
  int inputLen = 0, len;
  while (inputLen < ciphertext.length()){
    memset(inputText, 0, 8);
    len = 8;
    if (inputLen + len > ciphertext.length()) {
      len = ciphertext.length() - inputLen;
    }
    memcpy(inputText, ciphertext.c_str() + inputLen, len);
    DES_ncbc_encrypt(inputText, outputText, len, &keySchedule, &ivec, DES_DECRYPT);
    inputLen += len;
    plaintext += string(reinterpret_cast<const char*>(outputText));
  }
  return plaintext;
}

3. RSA加密

RSA(Rivest-Shamir-Adleman)加密算法是一种非对称加密算法,它使用一个公共密钥和一个私有密钥来加密和解密数据,常用于网络数据传输和数字签名等。在C++中,可以使用OpenSSL库来实现RSA加密,如下所示:

#include <openssl/rsa.h>
#include <openssl/pem.h>
string RSA_Encrypt(string plaintext, string publicKey){
  RSA *rsa = NULL;
  BIO *keyBio = BIO_new_mem_buf(publicKey.c_str(), -1);
  rsa = PEM_read_bio_RSA_PUBKEY(keyBio, &rsa, NULL, NULL);
  int rsa_len = RSA_size(rsa);
  char *encryptText = new char[rsa_len + 1];
  memset(encryptText, 0, rsa_len + 1);
  int len = RSA_public_encrypt(plaintext.length(), (const unsigned char*)plaintext.c_str(), (unsigned char*)encryptText, rsa, RSA_PKCS1_PADDING);
  if (len == -1)
    return "";
  
  string ciphertext = "";
  for (int i = 0; i < len; i++) {
    ciphertext += encryptText[i];
  }
  delete[] encryptText;
  RSA_free(rsa);
  BIO_free(keyBio);
  return ciphertext;
}
string RSA_Decrypt(string ciphertext, string privateKey){
  RSA *rsa = NULL;
  BIO *keyBio = BIO_new_mem_buf(privateKey.c_str(), -1);
  rsa = PEM_read_bio_RSAPrivateKey(keyBio, &rsa, NULL, NULL);
  int rsa_len = RSA_size(rsa);
  char *decryptText = new char[rsa_len + 1];
  memset(decryptText, 0, rsa_len + 1);
  int len = RSA_private_decrypt(ciphertext.length(), (const unsigned char*)ciphertext.c_str(), (unsigned char*)decryptText, rsa, RSA_PKCS1_PADDING);
  if (len == -1)
    return "";
  
  string plaintext = "";
  for (int i = 0; i < len; i++) {
    plaintext += decryptText[i];
  }
  delete[] decryptText;
  RSA_free(rsa);
  BIO_free(keyBio);
  return plaintext;
}

总之,C++是实现加密算法的一种选择,不同的加密算法有不同的实现方法。以上是一些常见的加密算法及其在C++中的实现。

  
  

评论区