📄 encryption.mht
字号:
does not use it.</P>
<P>After the key is imported or generated, it is now time to =
perform=20
the encryption or decryption. The encryption and decryption =
depends=20
on a session key, which is based on the key we just imported =
or=20
generated previously.</P><PRE><SPAN =
class=3Dcpp-keyword>void</SPAN> Encrypt()
{
<SPAN class=3Dcpp-keyword>unsigned</SPAN> <SPAN =
class=3Dcpp-keyword>long</SPAN> length =3D m_clear.GetLength() +<SPAN =
class=3Dcpp-literal>1</SPAN>;
<SPAN class=3Dcpp-keyword>unsigned</SPAN> <SPAN =
class=3Dcpp-keyword>char</SPAN> * cipherBlock =3D (<SPAN =
class=3Dcpp-keyword>unsigned</SPAN> <SPAN =
class=3Dcpp-keyword>char</SPAN>*)malloc(length);
memset(cipherBlock, <SPAN class=3Dcpp-literal>0</SPAN>, length);
memcpy(cipherBlock, m_clear, length -<SPAN =
class=3Dcpp-literal>1</SPAN>);
<SPAN class=3Dcpp-keyword>if</SPAN> (!CryptEncrypt(hSessionKey, =
<SPAN class=3Dcpp-literal>0</SPAN>, TRUE, <SPAN =
class=3Dcpp-literal>0</SPAN>, cipherBlock, &length, length))
{
dwResult =3D GetLastError();
MessageBox(<SPAN class=3Dcpp-string>"Error CryptEncrypt() =
failed."</SPAN>, <SPAN class=3Dcpp-string>"Information"</SPAN>, MB_OK);
<SPAN class=3Dcpp-keyword>return</SPAN>;
}
m_cipher =3D cipherBlock;
m_clear =3D <SPAN class=3Dcpp-string>""</SPAN>;
free(cipherBlock);
}</PRE>
<P>The decryption does not vary much from the encryption. =
Like with=20
the encryption it is done with just one invocation and the =
cipher=20
text is then decrypted.</P><PRE><SPAN =
class=3Dcpp-keyword>void</SPAN> Decrypt()
{
<SPAN class=3Dcpp-keyword>unsigned</SPAN> <SPAN =
class=3Dcpp-keyword>long</SPAN> length =3D m_cipher.GetLength() +<SPAN =
class=3Dcpp-literal>1</SPAN>;
<SPAN class=3Dcpp-keyword>unsigned</SPAN> <SPAN =
class=3Dcpp-keyword>char</SPAN> * cipherBlock =3D (<SPAN =
class=3Dcpp-keyword>unsigned</SPAN> <SPAN =
class=3Dcpp-keyword>char</SPAN>*)malloc(length);
memset(cipherBlock, <SPAN class=3Dcpp-literal>0</SPAN>, =
length);
memcpy(cipherBlock, m_cipher, length -<SPAN =
class=3Dcpp-literal>1</SPAN>);
<SPAN class=3Dcpp-keyword>if</SPAN> (!CryptDecrypt(hSessionKey, =
<SPAN class=3Dcpp-literal>0</SPAN>, TRUE, <SPAN =
class=3Dcpp-literal>0</SPAN>, cipherBlock, &length))
{
dwResult =3D GetLastError();
MessageBox(<SPAN class=3Dcpp-string>"Error CryptDecrypt() =
failed."</SPAN>,=20
<SPAN =
class=3Dcpp-string>"Information"</SPAN>, MB_OK);
<SPAN class=3Dcpp-keyword>return</SPAN>;
}
m_clear =3D cipherBlock;
m_cipher =3D <SPAN class=3Dcpp-string>""</SPAN>;
free(cipherBlock);
}</PRE>
<H2>Points of interest</H2>
<P>The clear text / data is typically a string containing =
any text.=20
Before performing the encryption it is a good idea to copy =
the data=20
into an unsigned char pointer to avoid future problems with =
casts=20
when the encryption is performed.</P>
<P>Finally, when using the Win32 Crypto API, please notice =
that it=20
might not necessary compile as is. This is because a =
pre-processor=20
definition is missing in the project.</P><PRE><SPAN =
class=3Dcpp-preprocessor>#define _WIN32_WINNT 0x0400</SPAN></PRE>
<P>When defining the above globally to the project, it will =
be=20
possible to compile code that is using the Crypto API. My =
suggestion=20
is to place the definition in the <I>StdAfx.h</I> =
file.</P><!-- Article Ends --></DIV></SPAN>
<SCRIPT =
src=3D"http://www.codeproject.com/script/togglePre.js"=20
type=3Dtext/javascript></SCRIPT>
<H2>About Jessn</H2>
<DIV style=3D"OVERFLOW: hidden">
<TABLE border=3D0>
<TBODY>
<TR vAlign=3Dtop>
<TD class=3DsmallText noWrap><IMG=20
=
src=3D"http://www.codeproject.com/script/profile/images/{4629F873-7252-41=
E6-A5CF-907C61604F7B}.jpg"><BR></TD>
<TD class=3DsmallText>I'm a senior systems engineer, who =
holds a=20
b.sc. degree from the university of Aarhus. I have =
more than 5=20
yrs of commercial experience, who is specialized in =
security=20
(crypto systems) and evaluation of software =
architectures. I'm=20
working with C++, COM, DCOM, ATL and MFC applications =
based on=20
either the Oracle database or the SQL Server database, =
but I=20
have altso achieved a good knowledge of the C# =
language and=20
the .NET platform.<BR><BR>My primary function is to =
develop=20
commerce systems where security is an important issue =
such as=20
systems for the ministry of foreign affairs and the =
danish=20
national guard among others.
<P class=3DsmallText>Click <A=20
=
href=3D"http://www.codeproject.com/script/profile/whos_who.asp?vt=3Darts&=
amp;id=3D1546914">here</A>=20
to view Jessn's online=20
profile.</P></TD></TR></TBODY></TABLE></DIV><BR>
<TABLE cellPadding=3D4 width=3D"100%" border=3D0>
<TBODY>
<TR vAlign=3Dtop>
<TD width=3D"100%">
<H2>Other popular C++ / MFC articles:</H2>
<UL>
<LI><A=20
=
href=3D"http://www.codeproject.com/cpp/FastDelegate.asp">Member=20
Function Pointers and the Fastest Possible C++ =
Delegates</A>
<DIV class=3DsmallText>A comprehensive tutorial on =
member=20
function pointers, and an implementation of =
delegates that=20
generates only two ASM opcodes!</DIV>
<LI><A=20
=
href=3D"http://www.codeproject.com/cpp/exceptionhandler.asp">How=20
a C++ compiler implements exception handling</A>
<DIV class=3DsmallText>An indepth discussion of how =
VC++=20
implements exception handling. Source code includes=20
exception handling library for VC++.</DIV>
<LI><A=20
=
href=3D"http://www.codeproject.com/cpp/Differentiation.asp">Symbolic=20
Differentiation</A>
<DIV class=3DsmallText>This article demonstrates=20
differentiating expressions using a stack and =
displaying the=20
input expression and its derivative.</DIV>
<LI><A =
href=3D"http://www.codeproject.com/cpp/pointers.asp">A=20
Beginner's Guide to Pointers</A>
<DIV class=3DsmallText>An article showing the use of =
pointers=20
in C and C++</DIV></LI></UL></TD>
<TD width=3D360>
<SCRIPT language=3Djavascript>document.write("<a =
href=3D\"/script/admentor/admentorredir.asp?id=3D4402&way=3Dban\" =
target=3D_blank><img =
src=3D\"/script/ann/ServeImg.aspx?File=3D%2Fscript%2Fadmentor%2Fimages%2F=
Quest%5Fwebcast%5F300x250%2Egif&C=3DFalse&id=3D4402&cb=3D504823\" =
alt=3D\"\" border=3D0 width=3D300 height=3D250></a>");</SCRIPT>
</TD></TR></TBODY></TABLE><BR>
<SCRIPT language=3DJavaScript=20
src=3D"http://www.codeproject.com/script/addto.js"=20
type=3Dtext/javascript></SCRIPT>
<SCRIPT language=3DJavaScript type=3Dtext/javascript>
var addtoMethod=3D1;
var AddURL =3D document.location.href;
var AddTitle =3D escape(document.title);
DrawLinks(100, 0, "smallText", "addto")=20
</SCRIPT>
<BR><BR>
<FORM action=3D/script/rating/code/app/insert_vote.asp=20
method=3Dpost><INPUT type=3Dhidden =
value=3DEncryptionCryptoAPI/cpp9/8/2005=20
name=3Dvote_name> <INPUT type=3Dhidden=20
value=3D/cpp/EncryptionCryptoAPI.asp name=3Dgoal>=20
<TABLE cellSpacing=3D0 cellPadding=3D1 width=3D"100%" =
bgColor=3D#ff9900=20
border=3D0>
<TBODY>
<TR>
<TD width=3D"100%">
<TABLE cellSpacing=3D0 cellPadding=3D4 width=3D"100%"=20
bgColor=3D#fbedbb border=3D0>
<TBODY>
<TR>
<TD class=3Dsmalltext vAlign=3Dcenter>[<A=20
=
href=3D"http://www.codeproject.com/cpp/EncryptionCryptoAPI.asp#__top">Top=
</A>]</TD>
<TD vAlign=3Dcenter noWrap =
align=3Dright><I><B>Rate this=20
Article for us!</B></I> =20
<I>Poor</I><INPUT type=3Dradio value=3D1 =
name=3Drate><INPUT=20
type=3Dradio value=3D2 name=3Drate><INPUT =
type=3Dradio value=3D3=20
name=3Drate><INPUT type=3Dradio value=3D4 =
name=3Drate><INPUT=20
type=3Dradio value=3D5=20
name=3Drate><I>Excellent</I> <INPUT class=3DFormButton =
type=3Dsubmit value=3DVote>=20
=
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM>
<CENTER>
<DIV style=3D"MARGIN: 10px"><SPAN id=3DAdBanner5>
<SCRIPT language=3Djavascript>document.write("<a =
href=3D\"/script/admentor/admentorredir.asp?id=3D4388&way=3Dban\" =
target=3D_blank><img =
src=3D\"/script/ann/ServeImg.aspx?File=3D%2Fscript%2Fadmentor%2Fimages%2F=
WholeTomato%5FAutoSuggest%5F468x60%2Egif&C=3DFalse&id=3D4388&cb=3D504787\=
" alt=3D\"The intellisense upgrade for Visual Studio .NET - make your =
IDE as smart as you.\" border=3D0 width=3D468 =
height=3D60></a>");</SCRIPT>
</SPAN></DIV></CENTER><A name=3D__comments></A>
<SCRIPT language=3DJavaScript>
function MsgVoteForm(MemberID, MsgID)
{
document.write("<span id=3D\"MVF" + MsgID + "\">");
document.write("Rate this message: ");
for (var i=3D1; i<=3D5;i++)
{
document.write("<a href=3D'#xx" + MsgID.toString() + "xx' =
title=3D'give this message a vote of " + i.toString());
document.write("' onclick=3D'return RateMsg(" + MemberID.toString() + =
", " + MsgID.toString() + ", ");
document.write(i.toString() + ")'><b>" + i.toString() + "</b></a> ");
}
document.write(" (out of 5)");
document.writeln("</span>");
=09
=09
}
function RetypeForm(MemberID, MsgID)
{
var types =3D [
{pic:'news_general.gif', id: 1},
{pic:'news_info.gif', id: 2},
{pic:'news_question.gif', id: 4},
{pic:'news_answer.gif', id: 8},
{pic:'news_game.gif', id: 16},
{pic:'news_spam.gif', id: 32}
];
=09
document.write("<span id=3D\"RTF" + MsgID + "\">");
for (var i=3D0;i<types.length;i++)
{
document.write("<a href=3D'/script/comments/retype?msg=3D" + =
MsgID.toString());
document.write("&mid=3D" + MemberID.toString() + "&type=3D" + =
types[i].id + "'>");
document.write("<img border=3D0 src=3D'/script/images/" + types[i].pic =
+ "'></a><br />");
}
document.writeln("</span>");
}
function ReportMsg(userid, msgid, score)
{
if (confirm("Are you sure you want to report this message?") =3D=3D =
true)
return RateMsg(userid, msgid, score);
else return false;
}
function RateMsg(userid, msgid, score)
{
var req =3D new ActiveXObject("Msxml2.XMLHTTP");=20
req.onreadystatechange =3D function()
{
if ( req.readyState =3D=3D 4 )
{
if ( req.status =3D=3D 200 )
{
var respText =3D req.responseText;
var re =3D new RegExp("\<div\>([^\<]*)\</div\>", "g");
var match =3D re.exec(respText);
status.innerHTML =3D "<b>" + (match)?match[1]:"An error =
occured" + "</b>";
}
else
{
status.innerHTML =3D "<b style=3D'color:red'>Failed!" + =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -