⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shortcrypter.cpp

📁 password encrypt & decrypt
💻 CPP
字号:
#include <iostream>
#include <string>
using namespace std;

string encrypt(string m, unsigned short k);

int main(int argc, char* argv[]) {

  string message = "This is a sample message";
  string cipher = encrypt(message, 54321);

  cout << cipher << endl;

  exit(EXIT_SUCCESS);
}


string encrypt(string m, unsigned short k) {

  int cur, mid, max, len, pad, blks;
  unsigned short k1, k2;
  char tmp;

  srand(k);
  k1 = (unsigned short)rand();
  k2 = (unsigned short)rand();

  len = m.length();

  if ((len % 8) > 0)              // Compute how many pad characters will be
    pad = 8 - (len % 8);          //   required to make the message a multiple
  else                            //   8 characers long
    pad = 0;

  if (pad > 0)                    // Add spaces to pad message if required
    for (cur = 0; cur < pad; cur++)
      m += string(" ");

  max = m.length();               // Compute how many 8 characters blocks
  blks = max / 8;                 //   there are in the message

  for (int i = 0; i < max; i++)   // Mangle the bits of the message using k
    m[i] = m[i] xor k;

  for (int i = 0; i < (blks*8); i += 8) {
    tmp = m[0+i];
    m[0+i] = m[2+i];              // Mix up each block
    m[2+i] = m[5+i];
    m[5+i] = m[6+i];
    m[6+i] = m[1+i];
    m[1+i] = m[7+i];
    m[7+i] = m[3+i];
    m[3+i] = m[4+i];
    m[4+i] = tmp;
  }

  for (int i = 0; i < max; i++)   // Mangle the bits of the message using k1
    m[i] = m[i] xor k1;

  mid = max / 2;                  // Compute a "halfway" mark in the message
  len = mid / 2;

  for (int i = 0; i < len; i++) { // Shuffle the message some more by
    tmp = m[i];                   //   moving the block positions
    m[i] = m[i+mid+len];
    m[i+mid+len] = tmp;
  }

  for (int i = len; i < mid; i++) {
    tmp = m[i];                   // Shuffle the message some more by
    m[i] = m[max-1-i];            //   swapping and reversing characters in
    m[max-1-i] = tmp;             //   different blocks
  }

  for (int i = 0; i < max; i++)   // Mangle the bits of the message using k2
    m[i] = m[i] xor k2;

  return m;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -