Implementation of symmetric encryption DES and asymmetric encryption RSA

Source: Internet
Author: User
Shared strkey as string = "12345678"
Shared striv as string = "12345678"

Private shared key () as byte = encoding. utf8.getbytes (strkey. substring (0, 8 ))
Private shared IV () as byte = encoding. utf8.getbytes (striv. substring (0, 8 ))
'Key = new byte () {11, 12, 13, 14, 15, 16, 17, 18}
'Iv = new byte () {11, 12, 13, 14, 15, 16, 17, 18}

Public shared function encrypt (byval strtext) as string
Try
Dim inputbytearray () as byte = encoding. utf8.getbytes (strtext)
Dim des as descryptoserviceprovider = new descryptoserviceprovider
Dim MS as new memorystream
Dim CS as new cryptostream (MS, Des. createencryptor (Key, IV), cryptostreammode. Write)
CS. Write (inputbytearray, 0, inputbytearray. length)
CS. flushfinalblock ()
Return convert. tobase64string (Ms. toarray ())
Catch ex as exception
Return ex. Message
End try
End Function

Public shared function decrypt (byval strtext) as string
Dim inputbytearray () as byte
'Inputbytearray = new byte (strtext. length)

Try
Inputbytearray = convert. frombase64string (strtext)
Dim des as new descryptoserviceprovider
Dim MS as new memorystream
Dim CS as new cryptostream (MS, Des. createdecryptor (Key, IV), cryptostreammode. Write)
CS. Write (inputbytearray, 0, inputbytearray. length)
CS. flushfinalblock ()
Dim Encoding As encoding = encoding. utf8
Return encoding. getstring (Ms. toarray ())
Catch ex as exception
Return ex. Message
End try
End Function

C # implement des

Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Security. cryptography;
Using System. IO;
Using System. configuration;

Public static class encrypthelper
{< br> static string strkey = system. configuration. configurationmanager. appsettings [ " encryptkey " ];
static string striv = strkey;

private static byte [] key = encoding. utf8.getbytes (strkey. substring ( 0 , 8 ));
private static byte [] IV = encoding. utf8.getbytes (striv. substring ( 0 , 8 ));

Public   Static   String Encrypt ( String Strtext)
{
Try
{
Byte [] inputbytearray = Encoding. utf8.getbytes (strtext );
Descryptoserviceprovider des =   New Descryptoserviceprovider ();
MS for system. Io. memorystream =   New Memorystream ();
Cryptostream CS =   New Cryptostream (MS, Des. createencryptor (Key, IV), cryptostreammode. Write );
CS. Write (inputbytearray, 0 , Inputbytearray. Length );
CS. flushfinalblock ();
Return Convert. tobase64string (Ms. toarray ());

}
Catch(Exception ex)
{

ReturnEx. message;
}
}

Public Static StringDecrypt (StringStrtext)
{

Byte [] inputbytearray;

Try
{
Inputbytearray=Convert. frombase64string (strtext );

descryptoserviceprovider des = New descryptoserviceprovider ();
system. io. memorystream MS = New memorystream ();
cryptostream CS = New cryptostream (MS, des. createdecryptor (Key, IV), cryptostreammode. write);

CS. write (inputbytearray, 0 , inputbytearray. length);
CS. flushfinalblock ();
return encoding. utf8.getstring (Ms. toarray ();
}< br> catch (exception ex)
{

ReturnEx. message;
}
}

}

Asymmetric RSA Using System;
Using System. Security. cryptography;
Using System. text;
 
Class Rsacspsample
{
Public   Void Maintest ()
{
Try
{
// Create a unicodeencoder
Unicodeencoding byteconverter =   New Unicodeencoding ();
 
// Create a byte array to retain the original, encrypted, and decrypted data.
// Byte [] datatoencrypt = byteconverter. getbytes ("data to be encrypted ");
 
Byte [] Datatoencrypt = Byteconverter. getbytes ( " Data to be encrypted " );
Byte [] Encrypteddata;
Byte [] Decrypteddata;
 
// Create an rsacryptoserviceprovider instance to generate public and private key data.
Rsacryptoserviceprovider RSA =   New Rsacryptoserviceprovider ();
 
// This data is used to encrypt the public key information.
// Use rsacryptoserviceprovider. exportparameters (false ),
// And a Boolean variable to indicate whether to use EP.
Encrypteddata = Rsaencrypt (datatoencrypt, RSA. exportparameters ( False ), False );
 
Console. writeline ( " Encrypted plaintext: {0} " , Convert. tobase64string (encrypteddata ));
 
// Use this data to decrypt and use the private key
// Rsacryptoserviceprovider. exportparameters (true ),
// And a Boolean variable to indicate whether to use EP.
Decrypteddata = Rsadecrypt (encrypteddata, RSA. exportparameters ( True ), False );
 
// The decrypted information is displayed on the console.
Console. writeline ( " Decrypted plaintext: {0} " , Byteconverter. getstring (decrypteddata ));
Console. Read ();
}
Catch (Argumentnullexception)
{
Console. writeline ( " Encryption failed. " );
}
}
 
Public   Byte [] Rsaencrypt ( Byte [] Datatoencrypt, rsaparameters rsakeyinfo, Bool Dooaeppadding)
{
Try
{
Rsacryptoserviceprovider RSA =   New Rsacryptoserviceprovider ();
 
// Enter RSA key information, including public key information.
RSA. importparameters (rsakeyinfo );
 
// Encrypt the byte array and whether to fill it with OAEP
// The oa ep is only filled in Microsoft Windows XP or
// Useful in later versions
Return RSA. Encrypt (datatoencrypt, dooaeppadding );
}
Catch (Cryptographicexception E)
{
Console. writeline (E. Message );
Return   Null ;
}
}
 
Public   Byte [] Rsadecrypt ( Byte [] Datatodecrypt, rsaparameters rsakeyinfo, Bool Dooaeppadding)
{
Try
{
Rsacryptoserviceprovider RSA =   New Rsacryptoserviceprovider ();
 
// Enter the RSA key information.
// Private Key Information
RSA. importparameters (rsakeyinfo );
 
// Decrypts this byte array and determines whether to fill the array with OAEP.
Return RSA. decrypt (datatodecrypt, dooaeppadding );
}
Catch (Cryptographicexception E)
{
Console. writeline (E. tostring ());
Return   Null ;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.