1. 程式人生 > >c++實現aes加密演算法,對字串進行加密

c++實現aes加密演算法,對字串進行加密

我的blog中,已經寫過一篇關於aes加密演算法的呼叫。不過使用的引數必須時unsigned char型別。我們在
程式設計中使用最多的char型別,我從網上下載了一個程式碼,追加了一部分程式碼。
    其主要功能進行實現unsigned char型別資料到char型別轉化,將unsigned char結果與16進位制字串串之間的轉化。程式碼如下:

點選(此處)摺疊或開啟

  1. #ifndef AES_H
  2. #define AES_H
  3. #include <string.h>
  4. class AES
  5. {
  6. public:
  7.   AES(
    unsigned char* key);
  8.   virtual ~AES();
  9.   unsigned char* Cipher(unsigned char* input);
  10.   unsigned char* InvCipher(unsigned char* input);
  11.   void* Cipher(void* input, int length=0);
  12.   void* InvCipher(void* input, int length);
  13.   void Cipher(char *input, char *output);
  14.   void InvCipher(
    char *inut, char *output);
  15. private:
  16.   unsigned char Sbox[256];
  17.   unsigned char InvSbox[256];
  18.   unsigned char w[11][4][4];
  19.   void KeyExpansion(unsigned char* key, unsigned char w[][4][4]);
  20.   unsigned char FFmul(unsigned char a, unsigned char b);
  21.   void SubBytes(unsigned char state[
    ][4]);
  22.   void ShiftRows(unsigned char state[][4]);
  23.   void MixColumns(unsigned char state[][4]);
  24.   void AddRoundKey(unsigned char state[][4], unsigned char k[][4]);
  25.   void InvSubBytes(unsigned char state[][4]);
  26.   void InvShiftRows(unsigned char state[][4]);
  27.   void InvMixColumns(unsigned char state[][4]);
  28.   int strToHex(const char *ch, char *hex);
  29.   int hexToStr(const char *hex, char *ch);
  30.   int ascillToValue(const char ch);
  31.   char valueToHexCh(const int value);
  32.   int getUCharLen(const unsigned char *uch);
  33.   int strToUChar(const char *ch, unsigned char *uch);
  34.   int ucharToStr(const unsigned char *uch, char *ch);
  35.   int ucharToHex(const unsigned char *uch, char *hex);
  36.   int hexToUChar(const char *hex, unsigned char *uch);
  37. };
  38. #endif // AES_H
對應的cpp檔案如下:

點選(此處)摺疊或開啟

  1. #include "aes.h"
  2. AES::AES(unsigned char* key)
  3. {
  4.   unsigned char sBox[] =
  5.   { /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  6.     0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, /*0*/
  7.     0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, /*1*/
  8.     0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, /*2*/
  9.     0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, /*3*/
  10.     0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, /*4*/
  11.     0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, /*5*/
  12.     0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, /*6*/
  13.     0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, /*7*/
  14.     0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, /*8*/
  15.     0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, /*9*/
  16.     0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, /*a*/
  17.     0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, /*b*/
  18.     0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, /*c*/
  19.     0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, /*d*/
  20.     0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, /*e*/
  21.     0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 /*f*/
  22.   };
  23.   unsigned char invsBox[256] =
  24.   { /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  25.     0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb, /*0*/
  26.     0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb, /*1*/
  27.     0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e, /*2*/
  28.     0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25, /*3*/
  29.     0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92, /*4*/
  30.     0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84, /*5*/
  31.     0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06, /*6*/
  32.     0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b, /*7*/
  33.     0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73, /*8*/
  34.     0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e, /*9*/
  35.     0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b

    相關推薦

    c++實現aes加密演算法字串進行加密

    我的blog中,已經寫過一篇關於aes加密演算法的呼叫。不過使用的引數必須時unsigned char型別。我們在 程式設計中使用最多的char型別,我從網上下載了一個程式碼,追加了一部分程式碼。     其主要功能進行實現unsigned char型別資料到char

    java實現編輯距離演算法計算字串相似度

     這是Levenshtein Distance演算法的java實現,另外oracle 10g r2當中好像自帶了這樣的函式,utl_match包當中public class LD {  /**       * 計算向量距離       * Levenshtein Distan

    java爬蟲之入門基礎 java讀取txt檔案字串進行操作後匯出txt檔案

    相比於C#,java爬蟲,python爬蟲更為方便簡要,首先呢,python的urllib2包提供了較為完整的訪問網頁文件的API,再者呢對於摘下來的文章,python的beautifulsoap提供了簡潔的文件處理功能,這就成就了他爬蟲的優勢。 作為一名滿腦子要成為一名大牛的程式設計師小白來講,倒不是非要

    使用MD5線上加解密工具漢字進行加密得到不同的結果

    1. 問題背景 使用相同的漢字字串進行MD5計算,有時候會遇到:使用不同的線上工具,得到不同的MD5計算結果。 2. 原因 出現這樣情況的原因,通常是因為不同的線上工具使用的對漢字的編碼方式不同。(如果還有其他原因,歡迎大家評論留言哈,本喵也很想知道~) 3. 驗證 用一個32位

    利用 BASE64Encoder 字串進行加密 BASE64Decoder進行解密

    import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class TestEncrypt { public s

    Java使用RSA加密算法內容進行加密

    hat trac ogg size gen cte false static doc 什麽是RSA加密算法 RSA是一種典型的非對稱性加密算法,具體介紹可參考阮一峰的日誌 RSA算法原理 下面是使用RSA算法對傳輸內容進行加密的一個簡要Java案例,主要用到了三個類,大體實

    php實現封裝aes加密演算法與前端互動

    class AesSecurity { /** * method 為AES-128-CBC時 * @var string傳入要加密的明文 * 傳入一個16位元組的key * 傳入一個16位元組的初始偏移向量IV */ priv

    C語言程式設計實現使用AES檔案進行加密

    #include <stdio.h>#include <stdlib.h>#include <string.h>#include <openssl/aes.h>int main(int argc,char* argv[]){ 

    C# 實現AES加密--解密

    bsp 密文 rem adding gets manage string sys cipher /// <summary> /// AES 加密 /// </summary> ///

    sort函式的用法(C++排序庫函式的呼叫)陣列進行排序c++中有庫函式幫我們實現這們就不需要我們自己來程式設計進行排序了。

    對陣列進行排序,在c++中有庫函式幫我們實現,這們就不需要我們自己來程式設計進行排序了。 (一)為什麼要用c++標準庫裡的排序函式 Sort()函式是c++一種排序方法之一,學會了這種方法也打消我學習c++以來使用的氣泡排序和選擇排序所帶來的執行效率不高的問題!因為它使用

    各種排序演算法的場景以及c++實現(插入排序希爾排序氣泡排序快速排序選擇排序歸併排序)

    對現有工作並不是很滿意,所以決定找下一個坑。由工作中遇到排序場景並不多,大都是用氣泡排序,太low,面試又經常問到一些排序演算法方面的東西。剛好讓小學妹郵的資料結構也到了。就把各種排序演算法重新總結一下,以作留存。 排序分為內部排序和外部排序,內部排序是在記憶體中排序。外

    C#實現AES加密和解密函式

    AES簡介 AES(The Advanced Encryption Standard)是美國國家標準與技術研究所用於加密電子資料的規範。它被預期能成為人們公認的加密包括金融、電信和政府數字資訊的方法。 AES 是一個新的可以用於保護電子資料的加密演算法。明確地說,AES 是

    C++實現WebBrowser控制元件中滑鼠點選事件的監聽並獲取所點選標籤的超連結

            主要步驟如下:         1、接收網頁事件(參考資料) 要響應網頁事件,需要實現IDispatch介面,並在其Invoke()方法中處理收到的訊息。對於MFC,因為CCmdTarget類已經實現了該介面,所以只需要繼承CCmdTarget並結合相關巨

    使用AES演算法檔案進行加密解密(JAVA+Eclipse)

    一、專案中引用第三方類庫的方法    Bouncy Castle類庫的用法(如何在自己的專案中使用第三方類庫)    1)手動配置    將.jar,src,javadoc拷到專案目錄下   專案名,右鍵選build path-configure build path   

    C語言】編寫函式實現庫函式atoi字串轉換成整形

    //編寫函式實現庫函式atoi,把字串轉換成整形 #include <stdio.h> #include <string.h> int my_atoi(const char *

    CryptoJS值JavaScript加密演算法sha, md5, rc4, base64, aes

    支援的演算法包括:MD5,SHA-1,SHA-256,AES,Rabbit,MARC4,HMAC,HMAC-MD5,HMAC-SHA1,HMAC-SHA256,PBKDF21.2.var CryptoJS = require("cryptojs");CryptoJS.require.Hmac(); //如果需

    C# SHA1加密演算法並轉為大寫

    SHA1 sha1 = new SHA1CryptoServiceProvider();這裡 需要引入名稱空間: using System.Security.Cryptography; cla

    java實現二分查詢演算法兩種方式實現非遞迴和遞迴

    java實現二分查詢演算法 1、概念 2、前提 3、思想 4、過程 4、複雜度 5、實現方式 1. 非遞迴方式 2. 遞迴方式

    利用切片操作實現一個trim()函式去除字串首尾的空格注意不要呼叫str的strip()方法:# 測試: if trim('hello ') != 'hello': print('測試失敗!') elif trim(' hello'

    def trim(s): k = 0 '''while迴圈判斷輸入字串是否為空值''' while k < len(s): if s[k] == ' ': #如果是空字元則記錄字元的個數 k = k + 1 #k自增來記錄數值

    使用SSM 或者 springboot +mybatis時資料庫的認證資訊(使用者名稱密碼)進行加密

    通常情況下,為了提高安全性,我們需要對資料庫的認證資訊進行加密操作,然後在啟動專案的時候,會自動解密來核對資訊是否正確。下面介紹在SSM和springboot專案中分別是怎樣實現的。 無論是使用SSM還是springboot,首先我們需要一個加密工具,這裡我採用的是AES 高階加