如何在C ++中加密结构 [英] how to encrypt a structure in C++
本文介绍了如何在C ++中加密结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
是否可以用C ++加密不同数据类型(struct)的结构,如何做到这一点?
祝你好运。
Rania
解决方案
对于加密,数据类型并不重要只要您知道要加密的数据的长度。
例如,在Windows中,您可以使用加密API函数 CryptEncrypt [ ^ ]加密数据。
因此,如果您希望加密具有不同数据类型的类型MyStruct
的名为myStruct
的变量,请传递& myStruct
作为pbData
参数和sizeof(MyStruct)
aspdwDataLen
参数。
< blockquote>
/ *
最简单的数据结构类型是线性数组
,最简单的用C ++加密结构类似这样的代码:
* /
#包括< iostream>
使用std :: cout;
使用std :: endl;
void encryptArray(char []);
void decryptArray(char * eStr);
int main()
{
//创建一个字符串到数组
char string [] =这是一个文本字符串!;
cout<< 原始数组是:<<字符串<< ENDL;
encryptArray(string);
//函数加密数组()
cout<< 加密数组字符串是:<<字符串<< ENDL;
decryptArray(string);
//函数解密数组()
cout<< 在数组字符串后:<<字符串<< ENDL;
std :: cin.get();
返回0;
} //主要结束
// ------------------------------ -----------------------
//加密数组数据
void encryptArray(char e [])
{
for(int i = 0; e [i]!='\ 0'; ++ i)++ e [i];
} //加密数组
// ----------------------------------- ------------------
//解密数组数据
void decryptArray(char * eStr){
for(; * eStr!= '\0'; ++ eStr) - (* eStr);
}
Hello
Is it possible to encrypt a structure of different data types (struct) in C++, and how can this be done?
Best regards.
Rania
解决方案
For encryption, the data types do not really matter as long as you know the length of the data that you wish to encrypt.
For example, in Windows, you can use the Crypto API function CryptEncrypt[^] to encrypt data.
So if you wish to encrypt a variable calledmyStruct
of typeMyStruct
having different data types, pass&myStruct
as thepbData
parameter andsizeof(MyStruct)
as thepdwDataLen
parameter.
/* The simplest type of data structure is a linear array and simplest encrypt a structure in C++ similar this code : */ #include <iostream > using std :: cout; using std :: endl; void encryptArray( char [ ] ); void decryptArray( char * eStr ); int main( ) { // create a string to Array char string[ ] = "this is a text string !"; cout << "Original Array is: " << string << endl; encryptArray( string ); // function encrypt Array( ) cout << "encrypt Array string is: " << string << endl; decryptArray( string ); //function decrypt Array( ) cout << "after Array string is: " << string << endl; std::cin.get(); return 0; }// end of main //----------------------------------------------------- //encrypt Array data void encryptArray (char e[] ) { for( int i=0; e[i] != '\0'; ++i ) ++e[i]; } // encrypt Array //----------------------------------------------------- //decrypt Array data void decryptArray( char * eStr ) { for( ; * eStr != '\0'; ++ eStr ) --(* eStr); }
这篇关于如何在C ++中加密结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文