❶ 一個matlab程序加密後只能通過密碼或者生成密鑰來運行程序如何實現
n=4;%%%設定允許輸入密碼的次數即可。
❷ 各位大俠,急求AES圖像加密的MATLAB代碼!!!
首先說定義的函數可能在調用時出現問題,原因是函數的輸入部分plot在其中並沒有使用,再者輸出參數應包括m和n,因此函數定義部分應該改為function
[hist,
rgbt,m,n]
=
getimagehists(imagename)。
在執行完hist
=
hist
/
(m*n),執行如下語句完成數據保存
save
hists.mat
hist
接著可以執行
clear
all
並執行
load
hists.mat
hist
及whos
以驗證數據存儲是否成功
❸ 求AES演算法加密C語言完整程序
恰好我有。能運行的,C語言的。
#include <string.h>
#include "aes.h"
#include "commonage.h"
#define byte unsigned char
#define BPOLY 0x1b //!< Lower 8 bits of (x^8+x^4+x^3+x+1), ie. (x^4+x^3+x+1).
#define BLOCKSIZE 16 //!< Block size in number of bytes.
#define KEYBITS 128 //!< Use AES128.
#define ROUNDS 10 //!< Number of rounds.
#define KEYLENGTH 16 //!< Key length in number of bytes.
byte xdata block1[ 256 ]; //!< Workspace 1.
byte xdata block2[ 256 ]; //!< Worksapce 2.
byte xdata * powTbl; //!< Final location of exponentiation lookup table.
byte xdata * logTbl; //!< Final location of logarithm lookup table.
byte xdata * sBox; //!< Final location of s-box.
byte xdata * sBoxInv; //!< Final location of inverse s-box.
byte xdata * expandedKey; //!< Final location of expanded key.
void CalcPowLog( byte * powTbl, byte * logTbl )
{
byte xdata i = 0;
byte xdata t = 1;
do {
// Use 0x03 as root for exponentiation and logarithms.
powTbl[i] = t;
logTbl[t] = i;
i++;
// Muliply t by 3 in GF(2^8).
t ^= (t << 1) ^ (t & 0x80 ? BPOLY : 0);
} while( t != 1 ); // Cyclic properties ensure that i < 255.
powTbl[255] = powTbl[0]; // 255 = '-0', 254 = -1, etc.
}
void CalcSBox( byte * sBox )
{
byte xdata i, rot;
byte xdata temp;
byte xdata result;
// Fill all entries of sBox[].
i = 0;
do {
// Inverse in GF(2^8).
if( i > 0 ) {
temp = powTbl[ 255 - logTbl[i] ];
} else {
temp = 0;
}
// Affine transformation in GF(2).
result = temp ^ 0x63; // Start with adding a vector in GF(2).
for( rot = 0; rot < 4; rot++ ) {
// Rotate left.
temp = (temp<<1) | (temp>>7);
// Add rotated byte in GF(2).
result ^= temp;
}
// Put result in table.
sBox[i] = result;
} while( ++i != 0 );
}
void CalcSBoxInv( byte * sBox, byte * sBoxInv )
{
byte xdata i = 0;
byte xdata j = 0;
// Iterate through all elements in sBoxInv using i.
do {
// Search through sBox using j.
cleardog();
do {
// Check if current j is the inverse of current i.
if( sBox[ j ] == i ) {
// If so, set sBoxInc and indicate search finished.
sBoxInv[ i ] = j;
j = 255;
}
} while( ++j != 0 );
} while( ++i != 0 );
}
void CycleLeft( byte * row )
{
// Cycle 4 bytes in an array left once.
byte xdata temp = row[0];
row[0] = row[1];
row[1] = row[2];
row[2] = row[3];
row[3] = temp;
}
void InvMixColumn( byte * column )
{
byte xdata r0, r1, r2, r3;
r0 = column[1] ^ column[2] ^ column[3];
r1 = column[0] ^ column[2] ^ column[3];
r2 = column[0] ^ column[1] ^ column[3];
r3 = column[0] ^ column[1] ^ column[2];
column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);
r0 ^= column[0] ^ column[1];
r1 ^= column[1] ^ column[2];
r2 ^= column[2] ^ column[3];
r3 ^= column[0] ^ column[3];
column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);
r0 ^= column[0] ^ column[2];
r1 ^= column[1] ^ column[3];
r2 ^= column[0] ^ column[2];
r3 ^= column[1] ^ column[3];
column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);
column[0] ^= column[1] ^ column[2] ^ column[3];
r0 ^= column[0];
r1 ^= column[0];
r2 ^= column[0];
r3 ^= column[0];
column[0] = r0;
column[1] = r1;
column[2] = r2;
column[3] = r3;
}
byte Multiply( unsigned char num, unsigned char factor )
{
byte mask = 1;
byte result = 0;
while( mask != 0 ) {
// Check bit of factor given by mask.
if( mask & factor ) {
// Add current multiple of num in GF(2).
result ^= num;
}
// Shift mask to indicate next bit.
mask <<= 1;
// Double num.
num = (num << 1) ^ (num & 0x80 ? BPOLY : 0);
}
return result;
}
byte DotProct( unsigned char * vector1, unsigned char * vector2 )
{
byte result = 0;
result ^= Multiply( *vector1++, *vector2++ );
result ^= Multiply( *vector1++, *vector2++ );
result ^= Multiply( *vector1++, *vector2++ );
result ^= Multiply( *vector1 , *vector2 );
return result;
}
void MixColumn( byte * column )
{
byte xdata row[8] = {
0x02, 0x03, 0x01, 0x01,
0x02, 0x03, 0x01, 0x01
}; // Prepare first row of matrix twice, to eliminate need for cycling.
byte xdata result[4];
// Take dot procts of each matrix row and the column vector.
result[0] = DotProct( row+0, column );
result[1] = DotProct( row+3, column );
result[2] = DotProct( row+2, column );
result[3] = DotProct( row+1, column );
// Copy temporary result to original column.
column[0] = result[0];
column[1] = result[1];
column[2] = result[2];
column[3] = result[3];
}
void SubBytes( byte * bytes, byte count )
{
do {
*bytes = sBox[ *bytes ]; // Substitute every byte in state.
bytes++;
} while( --count );
}
void InvSubBytesAndXOR( byte * bytes, byte * key, byte count )
{
do {
// *bytes = sBoxInv[ *bytes ] ^ *key; // Inverse substitute every byte in state and add key.
*bytes = block2[ *bytes ] ^ *key; // Use block2 directly. Increases speed.
bytes++;
key++;
} while( --count );
}
void InvShiftRows( byte * state )
{
byte temp;
// Note: State is arranged column by column.
// Cycle second row right one time.
temp = state[ 1 + 3*4 ];
state[ 1 + 3*4 ] = state[ 1 + 2*4 ];
state[ 1 + 2*4 ] = state[ 1 + 1*4 ];
state[ 1 + 1*4 ] = state[ 1 + 0*4 ];
state[ 1 + 0*4 ] = temp;
// Cycle third row right two times.
temp = state[ 2 + 0*4 ];
state[ 2 + 0*4 ] = state[ 2 + 2*4 ];
state[ 2 + 2*4 ] = temp;
temp = state[ 2 + 1*4 ];
state[ 2 + 1*4 ] = state[ 2 + 3*4 ];
state[ 2 + 3*4 ] = temp;
// Cycle fourth row right three times, ie. left once.
temp = state[ 3 + 0*4 ];
state[ 3 + 0*4 ] = state[ 3 + 1*4 ];
state[ 3 + 1*4 ] = state[ 3 + 2*4 ];
state[ 3 + 2*4 ] = state[ 3 + 3*4 ];
state[ 3 + 3*4 ] = temp;
}
void ShiftRows( byte * state )
{
byte temp;
// Note: State is arranged column by column.
// Cycle second row left one time.
temp = state[ 1 + 0*4 ];
state[ 1 + 0*4 ] = state[ 1 + 1*4 ];
state[ 1 + 1*4 ] = state[ 1 + 2*4 ];
state[ 1 + 2*4 ] = state[ 1 + 3*4 ];
state[ 1 + 3*4 ] = temp;
// Cycle third row left two times.
temp = state[ 2 + 0*4 ];
state[ 2 + 0*4 ] = state[ 2 + 2*4 ];
state[ 2 + 2*4 ] = temp;
temp = state[ 2 + 1*4 ];
state[ 2 + 1*4 ] = state[ 2 + 3*4 ];
state[ 2 + 3*4 ] = temp;
// Cycle fourth row left three times, ie. right once.
temp = state[ 3 + 3*4 ];
state[ 3 + 3*4 ] = state[ 3 + 2*4 ];
state[ 3 + 2*4 ] = state[ 3 + 1*4 ];
state[ 3 + 1*4 ] = state[ 3 + 0*4 ];
state[ 3 + 0*4 ] = temp;
}
void InvMixColumns( byte * state )
{
InvMixColumn( state + 0*4 );
InvMixColumn( state + 1*4 );
InvMixColumn( state + 2*4 );
InvMixColumn( state + 3*4 );
}
void MixColumns( byte * state )
{
MixColumn( state + 0*4 );
MixColumn( state + 1*4 );
MixColumn( state + 2*4 );
MixColumn( state + 3*4 );
}
void XORBytes( byte * bytes1, byte * bytes2, byte count )
{
do {
*bytes1 ^= *bytes2; // Add in GF(2), ie. XOR.
bytes1++;
bytes2++;
} while( --count );
}
void CopyBytes( byte * to, byte * from, byte count )
{
do {
*to = *from;
to++;
from++;
} while( --count );
}
void KeyExpansion( byte * expandedKey )
{
byte xdata temp[4];
byte i;
byte xdata Rcon[4] = { 0x01, 0x00, 0x00, 0x00 }; // Round constant.
unsigned char xdata *key;
unsigned char xdata a[16];
key=a;
//以下為加解密密碼,共16位元組。可以選擇任意值
key[0]=0x30;
key[1]=0x30;
key[2]=0x30;
key[3]=0x30;
key[4]=0x30;
key[5]=0x30;
key[6]=0x30;
key[7]=0x30;
key[8]=0x30;
key[9]=0x30;
key[10]=0x30;
key[11]=0x30;
key[12]=0x30;
key[13]=0x30;
key[14]=0x30;
key[15]=0x30;
////////////////////////////////////////////
// Copy key to start of expanded key.
i = KEYLENGTH;
do {
*expandedKey = *key;
expandedKey++;
key++;
} while( --i );
// Prepare last 4 bytes of key in temp.
expandedKey -= 4;
temp[0] = *(expandedKey++);
temp[1] = *(expandedKey++);
temp[2] = *(expandedKey++);
temp[3] = *(expandedKey++);
// Expand key.
i = KEYLENGTH;
while( i < BLOCKSIZE*(ROUNDS+1) ) {
// Are we at the start of a multiple of the key size?
if( (i % KEYLENGTH) == 0 ) {
CycleLeft( temp ); // Cycle left once.
SubBytes( temp, 4 ); // Substitute each byte.
XORBytes( temp, Rcon, 4 ); // Add constant in GF(2).
*Rcon = (*Rcon << 1) ^ (*Rcon & 0x80 ? BPOLY : 0);
}
// Keysize larger than 24 bytes, ie. larger that 192 bits?
#if KEYLENGTH > 24
// Are we right past a block size?
else if( (i % KEYLENGTH) == BLOCKSIZE ) {
SubBytes( temp, 4 ); // Substitute each byte.
}
#endif
// Add bytes in GF(2) one KEYLENGTH away.
XORBytes( temp, expandedKey - KEYLENGTH, 4 );
// Copy result to current 4 bytes.
*(expandedKey++) = temp[ 0 ];
*(expandedKey++) = temp[ 1 ];
*(expandedKey++) = temp[ 2 ];
*(expandedKey++) = temp[ 3 ];
i += 4; // Next 4 bytes.
}
}
void InvCipher( byte * block, byte * expandedKey )
{
byte round = ROUNDS-1;
expandedKey += BLOCKSIZE * ROUNDS;
XORBytes( block, expandedKey, 16 );
expandedKey -= BLOCKSIZE;
do {
InvShiftRows( block );
InvSubBytesAndXOR( block, expandedKey, 16 );
expandedKey -= BLOCKSIZE;
InvMixColumns( block );
} while( --round );
InvShiftRows( block );
InvSubBytesAndXOR( block, expandedKey, 16 );
}
void Cipher( byte * block, byte * expandedKey ) //完成一個塊(16位元組,128bit)的加密
{
byte round = ROUNDS-1;
XORBytes( block, expandedKey, 16 );
expandedKey += BLOCKSIZE;
do {
SubBytes( block, 16 );
ShiftRows( block );
MixColumns( block );
XORBytes( block, expandedKey, 16 );
expandedKey += BLOCKSIZE;
} while( --round );
SubBytes( block, 16 );
ShiftRows( block );
XORBytes( block, expandedKey, 16 );
}
void aesInit( unsigned char * tempbuf )
{
powTbl = block1;
logTbl = block2;
CalcPowLog( powTbl, logTbl );
sBox = tempbuf;
CalcSBox( sBox );
expandedKey = block1; //至此block1用來存貯密碼表
KeyExpansion( expandedKey );
sBoxInv = block2; // Must be block2. block2至此開始只用來存貯SBOXINV
CalcSBoxInv( sBox, sBoxInv );
}
//對一個16位元組塊解密,參數buffer是解密密緩存,chainBlock是要解密的塊
void aesDecrypt( unsigned char * buffer, unsigned char * chainBlock )
{
//byte xdata temp[ BLOCKSIZE ];
//CopyBytes( temp, buffer, BLOCKSIZE );
CopyBytes(buffer,chainBlock,BLOCKSIZE);
InvCipher( buffer, expandedKey );
//XORBytes( buffer, chainBlock, BLOCKSIZE );
CopyBytes( chainBlock, buffer, BLOCKSIZE );
}
//對一個16位元組塊完成加密,參數buffer是加密緩存,chainBlock是要加密的塊
void aesEncrypt( unsigned char * buffer, unsigned char * chainBlock )
{
CopyBytes( buffer, chainBlock, BLOCKSIZE );
//XORBytes( buffer, chainBlock, BLOCKSIZE );
Cipher( buffer, expandedKey );
CopyBytes( chainBlock, buffer, BLOCKSIZE );
}
//加解密函數,參數為加解密標志,要加解密的數據緩存起始指針,要加解密的數據長度(如果解密運算,必須是16的整數倍。)
unsigned char aesBlockDecrypt(bit Direct,unsigned char *ChiperDataBuf,unsigned char DataLen)
{
unsigned char xdata i;
unsigned char xdata Blocks;
unsigned char xdata sBoxbuf[256];
unsigned char xdata tempbuf[16];
unsigned long int xdata OrignLen=0; //未加密數據的原始長度
if(Direct==0)
{
*((unsigned char *)&OrignLen+3)=ChiperDataBuf[0];
*((unsigned char *)&OrignLen+2)=ChiperDataBuf[1];
*((unsigned char *)&OrignLen+1)=ChiperDataBuf[2];
*((unsigned char *)&OrignLen)=ChiperDataBuf[3];
DataLen=DataLen-4;
}
else
{
memmove(ChiperDataBuf+4,ChiperDataBuf,DataLen);
OrignLen=DataLen;
ChiperDataBuf[0]=OrignLen;
ChiperDataBuf[1]=OrignLen>>8;
ChiperDataBuf[2]=OrignLen>>16;
ChiperDataBuf[3]=OrignLen>>24;
}
cleardog();
aesInit(sBoxbuf); //初始化
if(Direct==0) //解密
{
Blocks=DataLen/16;
for(i=0;i<Blocks;i++)
{
cleardog();
aesDecrypt(tempbuf,ChiperDataBuf+4+16*i);
}
memmove(ChiperDataBuf,ChiperDataBuf+4,OrignLen);
cleardog();
return(OrignLen);
}
else //加密
{
if(DataLen%16!=0)
{
Blocks=DataLen/16+1;
//memset(ChiperDataBuf+4+Blocks*16-(DataLen%16),0x00,DataLen%16); //不足16位元組的塊補零處理
}
else
{
Blocks=DataLen/16;
}
for(i=0;i<Blocks;i++)
{
cleardog();
aesEncrypt(tempbuf,ChiperDataBuf+4+16*i);
}
cleardog();
return(Blocks*16+4);
}
}
//#endif
以上是C文件。以下是頭文件
#ifndef AES_H
#define AES_H
extern void aesInit( unsigned char * tempbuf );
extern void aesDecrypt(unsigned char *buffer, unsigned char *chainBlock);
extern void aesEncrypt( unsigned char * buffer, unsigned char * chainBlock );
extern void aesInit( unsigned char * tempbuf );
extern void aesDecrypt( unsigned char * buffer, unsigned char * chainBlock );
extern void aesEncrypt( unsigned char * buffer, unsigned char * chainBlock );
extern unsigned char aesBlockDecrypt(bit Direct,unsigned char *ChiperDataBuf,unsigned char DataLen);
#endif // AES_H
這是我根據網上程序改寫的。只支持128位加解密。沒有使用占內存很多的查表法。故運算速度會稍慢。
❹ aes加密演算法有多少種模式
一般的加密通常都是塊加密,如果要加密超過塊大小的數據,就需要涉及填充和鏈加密模式,文中提到的ECB和CBC等就是指鏈加密模式。這篇文檔比較形象地介紹了AES加密演算法中的一些模式轉載過來。注意,還缺一種CTR的模式。
同時在文章的最後,貼出幾對利用ECB and CBC模式得標准演算法得到的碼流串。
對稱加密和分組加密中的四種模式(ECB、CBC、CFB、OFB)
❺ matlab 提高函數速度
temp = bin2dec(reshape(data, 16, 8));
這樣試試看
不是所有for循環都可以去掉
難道我的回答第一句代碼是隱形的嘛?。。。就是讓你用這句替換掉for循環啊 當然這個結果是列向量,可能需要轉置一下什麼的
❻ 懂MATLAB和AES加密演算法的高手來幫我!怎麼用MATLAB語言編程AES加密演算法
加密過程為:C=Ek3(Dk2(Ek1(P)))
3DES解密過程為:P=Dk1((EK2(Dk3(C)))
具體的加/解密過程如圖2所示。K1、K2、K3決定了演算法的安全性,若三個密鑰互不相同,本質上就相當於用一個長為168位的密鑰進行加密。多年來,它在對付強力攻擊時是比較安全的。若數據對安全性要求不那麼高,K1可以等於K3。在這種情況下,密鑰的有效長度為112位。
❼ 簡述aes演算法的加密過程
AES加密過程涉及到 4 種操作,分別是位元組替代、行移位、列混淆和輪密鑰加。
1.位元組替換:位元組代替的主要功能是通過S盒完成一個位元組到另外一個位元組的映射。
2.行移位:行移位的功能是實現一個4x4矩陣內部位元組之間的置換。
4.輪密鑰加:加密過程中,每輪的輸入與輪密鑰異或一次(當前分組和擴展密鑰的一部分進行按位異或);因為二進制數連續異或一個數結果是不變的,所以在解密時再異或上該輪的密鑰即可恢復輸入。
5.密鑰擴展:其復雜性是確保演算法安全性的重要部分。當分組長度和密鑰長度都是128位時,AES的加密演算法共迭代10輪,需要10個子密鑰。AES的密鑰擴展的目的是將輸入的128位密鑰擴展成11個128位的子密鑰。AES的密鑰擴展演算法是以字為一個基本單位(一個字為4個位元組),剛好是密鑰矩陣的一列。因此4個字(128位)密鑰需要擴展成11個子密鑰,共44個字。
❽ 什麼是AESAES加密函數和AES解密函數
它被預期能成為人們公認的加密包括金融、電信和政府數字信息的方法。 AES 是一個新的可以用於保護電子數據的加密演算法。明確地說,AES 是一個迭代的、對稱密鑰分組的密碼,它可以使用128、192 和 256 位密鑰,並且用 128 位(16位元組)分組加密和解密數據。與公共密鑰密碼使用密鑰對不同,對稱密鑰密碼使用相同的密鑰加密和解密數據。通過分組密碼返回的加密數據 的位數與輸入數據相同。迭代加密使用一個循環結構,在該循環中重復置換(permutations )和替換(substitutions)輸入數據。Figure 1 顯示了 AES 用192位密鑰對一個16位位元組數據塊進行加密和解密的情形。
❾ 誰知道哪裡有AES演算法加密,解密c++/C語言代碼
我有寫好的,腫么給你?貼上來吧。
#ifndef aes_h_
#define aes_h_
#include <iostream>
#include <string>
using namespace std;
typedef unsigned char uint8;
class aes
{
public:
/// 構造函數
aes();
/// 析構函數
~aes();
/// 加密,默認256位密碼
///
/// @param input 要加密的字元串
/// @param output 加密後字元串
/// @return 無
/// @see
/// @note (note描述需要注意的問題)
void encrypt(const string& input, string& output);
/// 解密 默認密碼
///
/// @param input 要解密字元串
/// @param output 解密後字元串
/// @return 無
/// @see
/// @note (note描述需要注意的問題)
void decrypt(const string& input, string& output);
/// 加密 256位
///
/// @param key 密碼
/// @param input 要加密的字元串
/// @param output 加密後字元串
/// @return 無
/// @see
/// @note (note描述需要注意的問題)
void encrypt(uint8 key[32], const string& input, string& output);
/// 解密 256位
///
/// @param key 密碼
/// @param input 要解密字元串
/// @param output 解密後字元串
/// @return 無
/// @see
/// @note (note描述需要注意的問題)
void decrypt(uint8 key[32],const string& input, string& output);
private:
typedef struct
{
uint32 erk[64]; /* encryption round keys */
uint32 drk[64]; /* decryption round keys */
int nr; /* number of rounds */
}aes_context;
int aes_set_key( aes_context* ctx, uint8* key, int nbits );
void aes_encrypt( aes_context* ctx, uint8 input[16], uint8 output[16] );
void aes_decrypt( aes_context* ctx, uint8 input[16], uint8 output[16] );
};
#endif // aes_h_
我暈,太長貼不上來啊?
❿ AES什麼加密方式,起到什麼作用!
AES是高級加密標准,但是對普通的地電腦用戶是不需知道的,我們能用到的都是基於此標准開發的各種加密方法,EFS,MD5,無線網路的WEP,WAP等