求一个vc.net写的des例子程序。

whiteeyes 2002-12-31 03:09:09
msdn只有vb.net,c#.net下的例子。
...全文
70 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
poweruser 2003-01-09
如果不使用托管的话,跟6.0的没有什么区别呀
回复
des程序vc和vc.net的差不多,不过我好像只有tc写的
回复
ahu9870 2003-01-07
原始代码来自Codeproject,进行了少量改造。
先建立一个托管VC项目,再将代码复制进去。

#include "stdafx.h"
#using <system.dll>
#using <mscorlib.dll>
#include <tchar.h>

using namespace System;
using namespace System::Diagnostics ;
using namespace System::IO ;
using namespace System::Security::Cryptography ;
using namespace System::Text ;

// 这是此应用程序的入口点
int _tmain(void)
{
// TODO: 请用您自己的代码替换下面的示例代码。

DESCryptoServiceProvider * myDES = new DESCryptoServiceProvider();

MemoryStream * myMemoryStream = new MemoryStream;
UnicodeEncoding * myUnicodeEncoding = new UnicodeEncoding();
Encoding * myEncodingMethod = new UnicodeEncoding();

String * myPassphrase = L"我的密码";
String * myMessage = L"DES加密测试文本。";

Console::WriteLine("托管C++消息加密DES测试开始:");
Console::Write(" 当前密码:");
Console::WriteLine(myPassphrase);
Console::Write(" 当前明文:");
Console::WriteLine(myMessage);
Console::WriteLine("");

try
{
Byte myPassphraseBytes[] = myUnicodeEncoding->GetBytes( myPassphrase );
Byte myMessageBytes[] = myUnicodeEncoding->GetBytes( myMessage );

// Use the initialization vector and passphrase for both
// encrypting and decrypting functions
myDES->GenerateIV ();
myDES->Key = myPassphraseBytes;//myHash ;

ICryptoTransform * myDecryptor = myDES->CreateDecryptor ();
ICryptoTransform * myEncryptor = myDES->CreateEncryptor ();

// Create an encryption stream that will use the memory stream for
// a storage area.
CryptoStream * myEncryptStream = new CryptoStream(myMemoryStream, myEncryptor, CryptoStreamMode::Write );
myEncryptStream->Write (myMessageBytes,0,myMessageBytes->Length );
myEncryptStream->FlushFinalBlock ();

// The memory stream now holds an encrypted message.
Byte myEncryptedMessage[] = myMemoryStream->ToArray ();

Console::Write(" 密文:");
Console::WriteLine(Convert::ToBase64String ( myEncryptedMessage ));
myEncryptStream->Close ();
myMemoryStream->Close ();

/////////////////////////////////////////
// Decrypt an encrypted memory stream
/////////////////////////////////////////
MemoryStream * myEncryptedMemoryStream = new MemoryStream;
myEncryptedMemoryStream->Write (myEncryptedMessage,0,myEncryptedMessage->Length );

// Reset stream pointer to the beginning of the memory stream
myEncryptedMemoryStream->Seek (0,SeekOrigin::Begin );

// Create a decryption stream that will read from the
// memory stream to decrypt.
CryptoStream * myDecryptStream = new CryptoStream(myEncryptedMemoryStream, myDecryptor, CryptoStreamMode::Read );

// The stream reader will pull data through the myDecryptStream CryptoStream
// using the Unicoding character set.
StreamReader * sr = new StreamReader ( myDecryptStream, myEncodingMethod, false );

Console::Write(" 解密原文:");
Console::WriteLine( sr->ReadToEnd () );

myDecryptStream->Close ();
myEncryptedMemoryStream->Close ();}
catch (Exception * e)
{
Console::WriteLine("");
Console::WriteLine( e );
Console::WriteLine("测试出错!");
return 1;
}
Console::WriteLine("");
Console::WriteLine("测试成功结束。");
return 0;
}
回复
发帖
VC.NET
创建于2007-09-28

7540

社区成员

.NET技术 VC.NET
申请成为版主
帖子事件
创建了帖子
2002-12-31 03:09
社区公告
暂无公告