aestst.cpp

来自「这个是有关加密解密的源程序」· C++ 代码 · 共 510 行 · 第 1/2 页

CPP
510
字号
/*
 -------------------------------------------------------------------------
 Copyright (c) 2001, Dr Brian Gladman <                 >, Worcester, UK.
 All rights reserved.

 LICENSE TERMS

 The free distribution and use of this software in both source and binary 
 form is allowed (with or without changes) provided that:

   1. distributions of this source code include the above copyright 
      notice, this list of conditions and the following disclaimer;

   2. distributions in binary form include the above copyright
      notice, this list of conditions and the following disclaimer
      in the documentation and/or other associated materials;

   3. the copyright holder's name is not used to endorse products 
      built using this software without specific written permission. 

 DISCLAIMER

 This software is provided 'as is' with no explicit or implied warranties
 in respect of its properties, including, but not limited to, correctness 
 and fitness for purpose.
 -------------------------------------------------------------------------
 Issue Date: 15/01/2002
*/

// Correct Output (for variable block size - BLOCK_SIZE undefined):

// lengths:  block = 16 bytes, key = 16 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c
// input   = 3243f6a8885a308d313198a2e0370734
// encrypt = 3925841d02dc09fbdc118597196a0b32
// decrypt = 3243f6a8885a308d313198a2e0370734

// lengths:  block = 16 bytes, key = 20 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input   = 3243f6a8885a308d313198a2e0370734
// encrypt = 231d844639b31b412211cfe93712b880
// decrypt = 3243f6a8885a308d313198a2e0370734

// lengths:  block = 16 bytes, key = 24 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input   = 3243f6a8885a308d313198a2e0370734
// encrypt = f9fb29aefc384a250340d833b87ebc00
// decrypt = 3243f6a8885a308d313198a2e0370734

// lengths:  block = 16 bytes, key = 28 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input   = 3243f6a8885a308d313198a2e0370734
// encrypt = 8faa8fe4dee9eb17caa4797502fc9d3f
// decrypt = 3243f6a8885a308d313198a2e0370734

// lengths:  block = 16 bytes, key = 32 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input   = 3243f6a8885a308d313198a2e0370734
// encrypt = 1a6e6c2c662e7da6501ffb62bc9e93f3
// decrypt = 3243f6a8885a308d313198a2e0370734

// lengths:  block = 20 bytes, key = 16 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c
// input   = 3243f6a8885a308d313198a2e03707344a409382
// encrypt = 16e73aec921314c29df905432bc8968ab64b1f51
// decrypt = 3243f6a8885a308d313198a2e03707344a409382

// lengths:  block = 20 bytes, key = 20 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input   = 3243f6a8885a308d313198a2e03707344a409382
// encrypt = 0553eb691670dd8a5a5b5addf1aa7450f7a0e587
// decrypt = 3243f6a8885a308d313198a2e03707344a409382

// lengths:  block = 20 bytes, key = 24 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input   = 3243f6a8885a308d313198a2e03707344a409382
// encrypt = 73cd6f3423036790463aa9e19cfcde894ea16623
// decrypt = 3243f6a8885a308d313198a2e03707344a409382

// lengths:  block = 20 bytes, key = 28 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input   = 3243f6a8885a308d313198a2e03707344a409382
// encrypt = 601b5dcd1cf4ece954c740445340bf0afdc048df
// decrypt = 3243f6a8885a308d313198a2e03707344a409382

// lengths:  block = 20 bytes, key = 32 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input   = 3243f6a8885a308d313198a2e03707344a409382
// encrypt = 579e930b36c1529aa3e86628bacfe146942882cf
// decrypt = 3243f6a8885a308d313198a2e03707344a409382

// lengths:  block = 24 bytes, key = 16 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// encrypt = b24d275489e82bb8f7375e0d5fcdb1f481757c538b65148a
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d

// lengths:  block = 24 bytes, key = 20 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// encrypt = 738dae25620d3d3beff4a037a04290d73eb33521a63ea568
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d

// lengths:  block = 24 bytes, key = 24 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// encrypt = 725ae43b5f3161de806a7c93e0bca93c967ec1ae1b71e1cf
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d

// lengths:  block = 24 bytes, key = 28 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// encrypt = bbfc14180afbf6a36382a061843f0b63e769acdc98769130
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d

// lengths:  block = 24 bytes, key = 32 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// encrypt = 0ebacf199e3315c2e34b24fcc7c46ef4388aa475d66c194c
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d

// lengths:  block = 28 bytes, key = 16 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// encrypt = b0a8f78f6b3c66213f792ffd2a61631f79331407a5e5c8d3793aceb1
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9

// lengths:  block = 28 bytes, key = 20 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// encrypt = 08b99944edfce33a2acb131183ab0168446b2d15e958480010f545e3
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9

// lengths:  block = 28 bytes, key = 24 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// encrypt = be4c597d8f7efe22a2f7e5b1938e2564d452a5bfe72399c7af1101e2
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9

// lengths:  block = 28 bytes, key = 28 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// encrypt = ef529598ecbce297811b49bbed2c33bbe1241d6e1a833dbe119569e8
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9

// lengths:  block = 28 bytes, key = 32 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// encrypt = 02fafc200176ed05deb8edb82a3555b0b10d47a388dfd59cab2f6c11
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9

// lengths:  block = 32 bytes, key = 16 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// encrypt = 7d15479076b69a46ffb3b3beae97ad8313f622f67fedb487de9f06b9ed9c8f19
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8

// lengths:  block = 32 bytes, key = 20 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// encrypt = 514f93fb296b5ad16aa7df8b577abcbd484decacccc7fb1f18dc567309ceeffd
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8

// lengths:  block = 32 bytes, key = 24 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// encrypt = 5d7101727bb25781bf6715b0e6955282b9610e23a43c2eb062699f0ebf5887b2
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8

// lengths:  block = 32 bytes, key = 28 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// encrypt = d56c5a63627432579e1dd308b2c8f157b40a4bfb56fea1377b25d3ed3d6dbf80
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8

// lengths:  block = 32 bytes, key = 32 bytes
// key     = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input   = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// encrypt = a49406115dfb30a40418aafa4869b7c6a886ff31602a7dd19c889dc64f7e4e7a
// decrypt = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8

#include <iostream>
#include <iomanip>
#include <memory.h>
#include <windows.h>

#ifdef AES_IN_CPP
#include "aescpp.h"
#else
#include "aes.h"
#endif
#include "aestst.h"

// Use this define if testing aespp.c

//#define AESX

typedef unsigned char   byte;
typedef unsigned long   word;

#if defined(AES_DLL)
fn_ptrs fn;
#endif

extern "C" void out_state(long s0, long s1, long s2, long s3)
{
    std::cout << '\n' << std::hex << std::setfill('0');
    std::cout << std::setw(8) << s0;
    std::cout << std::setw(8) << s1;
    std::cout << std::setw(8) << s2;
    std::cout << std::setw(8) << s3;
}

void oblk(char m[], byte v[], word n = 16)
{
    std::cout << std::hex << std::setfill('0') << '\n' << m;

    for(word i = 0; i < n; ++i)
    
        std::cout << std::setw(2) << static_cast<word>(v[i]);
}

void message(const char *s)   { std::cout << s; }

#ifdef  AES_DLL

#include "windows.h"

HINSTANCE init_dll(fn_ptrs& fn)
{   HINSTANCE   h_dll;

    if(!(h_dll = LoadLibrary(dll_path)))
    {
        std::cout << "\n\nDynamic link Library AES_DLL not found\n\n"; return 0;
    }

    fn.fn_blk_len = (g_blk_len*)GetProcAddress(h_dll, "_aes_blk_len@8");
    fn.fn_enc_key = (g_enc_key*)GetProcAddress(h_dll, "_aes_enc_key@12");
    fn.fn_dec_key = (g_dec_key*)GetProcAddress(h_dll, "_aes_dec_key@12");
    fn.fn_enc_blk = (g_enc_blk*)GetProcAddress(h_dll, "_aes_enc_blk@12");
    fn.fn_dec_blk = (g_dec_blk*)GetProcAddress(h_dll, "_aes_dec_blk@12");

#if !defined(BLOCK_SIZE)
    if(!fn.fn_enc_key || !fn.fn_dec_key || !fn.fn_enc_blk  || !fn.fn_dec_blk || !fn.fn_blk_len)
#else
    if(!fn.fn_enc_key || !fn.fn_dec_key || !fn.fn_enc_blk  || !fn.fn_dec_blk)
#endif
    {
        std::cout << "\n\nRequired DLL Entry Point(s) not found\n\n"; 
        FreeLibrary(h_dll); 
        return 0;
    }

    return h_dll;
}

⌨️ 快捷键说明

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