Encryption and decryption of strings and Images

Source: Internet
Author: User

Class des
{
/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "ptoencrypt"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public static string md5encrypt (string ptoencrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = encoding. Default. getbytes (ptoencrypt );
Des. Key = asciiencoding. ASCII. getbytes (skey );
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Stringbuilder ret = new stringbuilder ();
Foreach (byte B in ms. toarray ())
{
Ret. appendformat ("{0: X2}", B );
}
Ret. tostring ();
Return ret. tostring ();
}

/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "ptoencrypt"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public static byte [] md5encrypt (byte [] inputbytearray, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Des. Key = asciiencoding. ASCII. getbytes (skey );
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Return Ms. toarray ();
}

/// <Summary>
/// Decrypt
/// </Summary>
/// <Param name = "ptodecrypt"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public static string md5decrypt (string ptodecrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = new byte [ptodecrypt. Length/2];
For (INT x = 0; x <ptodecrypt. Length/2; X ++)
{
Int I = (convert. toint32 (ptodecrypt. substring (x * 2, 2), 16 ));
Inputbytearray [x] = (byte) I;
}

Des. Key = asciiencoding. ASCII. getbytes (skey );
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Stringbuilder ret = new stringbuilder ();
Return System. Text. encoding. Default. getstring (Ms. toarray ());
}

/// <Summary>
/// Decrypt
/// </Summary>
/// <Param name = "ptodecrypt"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public static byte [] md5decrypt (byte [] inputbytearray, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Des. Key = asciiencoding. ASCII. getbytes (skey );
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Stringbuilder ret = new stringbuilder ();
Return Ms. toarray ();
}

/// <Summary>
/// Generate the key
/// </Summary>
/// <Returns> </returns>
Public static string generatekey ()
{
Descryptoserviceprovider descrypto = (descryptoserviceprovider) descryptoserviceprovider. Create ();
Return asciiencoding. ASCII. getstring (descrypto. Key );
}
}

Main ()

{

 

/* Image file encryption */
String filename = "xiongmao.jpg ";
// Read files or database image information
Filestream myfilestream = new filestream (filename, filemode. Open );
Binaryreader reader = new binaryreader (myfilestream );
Byte [] image = reader. readbytes (INT) myfilestream. Length );
// Encryption
String key = des. generatekey ();
Byte [] jiami = des. md5encrypt (image, key );
// Decrypt
Byte [] imagedata = des. md5decrypt (jiami, key );
// Display the image
Memorystream mstream = new memorystream ();
Mstream. Write (imagedata, 0, imagedata. Length );
Mstream. Flush ();
Bitmap IMG = new Bitmap (mstream );
Picturebox1.image = IMG;

/* String encryption/Decryption */
String S = des. md5encrypt ("fdsa hello, World Expo", key );
String SSS = des. md5decrypt (S, key );

}

==============================

Or

==============================

 

Class des2
{
/// <Summary>
/// DES encryption method
/// </Summary>
/// <Param name = "strplain"> plaintext </param>
/// <Param name = "strpolicey"> key </param>
/// <Param name = "strdesiv"> vector </param>
/// <Returns> ciphertext </returns>
Public static string desencrypt (string strplain, string strpolicey, string strdesiv)
{
Byte [] bytes0000ey = asciiencoding. ASCII. getbytes (strpolicey. substring (0, 8 ));

Byte [] bytesdesiv = asciiencoding. ASCII. getbytes (strdesiv );
Descryptoserviceprovider desencrypt = new descryptoserviceprovider ();
Memorystream msencrypt = new memorystream ();
Cryptostream csencrypt = new cryptostream (msencrypt, desencrypt. createencryptor (bytescipher ey, bytesdesiv), cryptostreammode. Write );
Streamwriter swencrypt = new streamwriter (csencrypt );

Swencrypt. writeline (strplain );
Swencrypt. Close ();
Csencrypt. Close ();
Byte [] bytescipher = msencrypt. toarray ();
Msencrypt. Close ();
Return convert. tobase64string (bytescipher );
}
/// <Summary>
/// Des Decryption Method
/// </Summary>
/// <Param name = "strcipher"> ciphertext </param>
/// <Param name = "strpolicey"> key </param>
/// <Param name = "strdesiv"> vector </param>
/// <Returns> plaintext </returns>
Public static string desdecrypt (string strcipher, string strpolicey, string strdesiv)
{
Byte [] bytes0000ey = asciiencoding. ASCII. getbytes (strpolicey. substring (0, 8 ));
Byte [] bytesdesiv = asciiencoding. ASCII. getbytes (strdesiv );
Byte [] bytescipher = convert. frombase64string (strcipher );
Descryptoserviceprovider desdecrypt = new descryptoserviceprovider ();
Memorystream msdecrypt = new memorystream (bytescipher );
Cryptostream csdecrypt = new cryptostream (msdecrypt, desdecrypt. createdecryptor (bytescipher ey, bytesdesiv), cryptostreammode. Read );
Streamreader srdecrypt = new streamreader (csdecrypt );
String strplaintext = srdecrypt. Readline ();
Srdecrypt. Close ();
Csdecrypt. Close ();
Msdecrypt. Close ();
Return strplaintext;
}

/// <Summary>
/// DES encryption method
/// </Summary>
/// <Param name = "plain"> plaintext </param>
/// <Param name = "strpolicey"> key </param>
/// <Param name = "strdesiv"> vector </param>
/// <Returns> ciphertext </returns>
Public static byte [] desencrypt (byte [] bytesplain, string strpolicey, string strdesiv)
{
Byte [] bytes0000ey = asciiencoding. ASCII. getbytes (strpolicey. substring (0, 8 ));

Byte [] bytesdesiv = asciiencoding. ASCII. getbytes (strdesiv );
Descryptoserviceprovider desencrypt = new descryptoserviceprovider ();
Memorystream msencrypt = new memorystream ();
Cryptostream csencrypt = new cryptostream (msencrypt, desencrypt. createencryptor (bytescipher ey, bytesdesiv), cryptostreammode. Write );
Csencrypt. Write (bytesplain, 0, bytesplain. Length );
Csencrypt. Close ();
Byte [] bytescipher = msencrypt. toarray ();
Msencrypt. Close ();
Return bytescipher;
}
/// <Summary>
/// Des Decryption Method
/// </Summary>
/// <Param name = "bytescipher"> ciphertext </param>
/// <Param name = "strpolicey"> key </param>
/// <Param name = "strdesiv"> vector </param>
/// <Returns> plaintext </returns>
Public static byte [] desdecrypt (byte [] bytescipher, string strpolicey, string strdesiv)
{
Byte [] bytes0000ey = asciiencoding. ASCII. getbytes (strpolicey. substring (0, 8 ));
Byte [] bytesdesiv = asciiencoding. ASCII. getbytes (strdesiv );
Descryptoserviceprovider desdecrypt = new descryptoserviceprovider ();
Memorystream msdecrypt = new memorystream (bytescipher );
Cryptostream csdecrypt = new cryptostream (msdecrypt, desdecrypt. createdecryptor (bytescipher ey, bytesdesiv), cryptostreammode. Read );
Byte [] fromencrypt = new byte [bytescipher. Length];
Csdecrypt. Read (fromencrypt, 0, fromencrypt. Length );
Csdecrypt. Close ();
Msdecrypt. Close ();
Return fromencrypt;
}

}

 

Main ()

{

 

/* Image file encryption */
String filename = "xiongmao.jpg ";
// Read files or database image information
Filestream myfilestream = new filestream (filename, filemode. Open );
Binaryreader reader = new binaryreader (myfilestream );
Byte [] image = reader. readbytes (INT) myfilestream. Length );
// Encryption
Byte [] jiami = des2.desencrypt (image, "abxiongabxiong", "xiongabxcddabxiongabxiongmaocddmaocdd ");
// Decrypt
Byte [] imagedata = des2.desdecrypt (jiami, "abxiongabxiong", "xiongabxcddabxiongabxiongmaocddmaocdd ");
// Display the image
Memorystream mstream = new memorystream ();
Mstream. Write (imagedata, 0, imagedata. Length );
Mstream. Flush ();
Bitmap IMG = new Bitmap (mstream );
Picturebox1.image = IMG;

/* String encryption/Decryption */
String S = des2.desencrypt ("fdsa hello, World Expo", "abxiongabxiong", "xiongabxcddabxiongabxiongmaocddmaocdd ");
String SSS = des2.desdecrypt (S, "abxiongabxiong", "xiongabxcddabxiongabxiongmaocddmaocdd ");

}

 

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.