C # AES Encryption algorithm

Source: Internet
Author: User

1 /// <summary>2         ///AES Encryption algorithm for aes/cbc/pkcs5padding pattern in Java3         /// </summary>4         /// <param name= "S" >string to encrypt</param>5         /// <param name= "SKey" >secret key</param>6         /// <returns></returns>7          Public Static stringAesencrypt (stringSstringSKey)8         {9Aescryptoserviceprovider Provider =NewAescryptoserviceprovider ();Ten             if(SKey = =NULL) One             { A                 Throw NewException ("key is empty null"); -             } -             //determine if key is 16-bit the             if(Skey.length! = -) -             { -                 Throw NewException ("key length is not 16 bits"); -             } +             byte[] bytes =Encoding.UTF8.GetBytes (s); -Provider. Key =Encoding.UTF8.GetBytes (SKey); +PROVIDER.IV = Encoding.UTF8.GetBytes ("0102030405060708"); AMemoryStream stream =NewMemoryStream (); atCryptoStream stream2 =NewCryptoStream (stream, provider. CreateEncryptor (), cryptostreammode.write); -Stream2. Write (Bytes,0, Bytes. Length); - stream2. FlushFinalBlock (); -StringBuilder Builder =NewStringBuilder (); -             foreach(byteNuminchStream. ToArray ()) -             { inBuilder. AppendFormat ("{0:x2}", num); -             } to Builder. ToString (); + stream2. Close (); - Stream. Close (); the             returnBuilder. ToString (); *         } $ Panax Notoginseng         /// <summary> -         ///AES decryption algorithm for aes/cbc/pkcs5padding pattern in Java the         /// </summary> +         /// <param name= "S" >string to decrypt</param> A         /// <param name= "SKey" >secret key</param> the         /// <returns></returns> +          Public Static stringAesdecrypt (stringSstringSKey) -         { $Aescryptoserviceprovider Provider =NewAescryptoserviceprovider (); $             byte[] buffer =New byte[S.length/2]; -              for(inti =0; I < (s.length/2); i++) -             { the                 intnum2 = Convert.ToInt32 (s.substring (i *2,2),0x10); -Buffer[i] = (byte) num2;Wuyi             } theProvider. Key =Encoding.UTF8.GetBytes (SKey); -PROVIDER.IV = Encoding.UTF8.GetBytes ("0102030405060708"); WuMemoryStream stream =NewMemoryStream (); -CryptoStream stream2 =NewCryptoStream (stream, provider. CreateDecryptor (), cryptostreammode.write); AboutStream2. Write (Buffer,0, buffer. Length); $ stream2. FlushFinalBlock (); -StringBuilder Builder =NewStringBuilder (); - stream2. Close (); - Stream. Close (); A             returnEncoding.Default.GetString (stream. ToArray ()); +}

C # AES Encryption algorithm

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.