. NET AES encrypted video and other files

Source: Internet
Author: User

The video of the company learning platform downloaded from the app needs to be encrypted

Refer to some of the wording for finding data

Wrote the AES encryption method.

Record to prevent forgetting

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classClass1 {Private Const stringSKey ="_?73^?dvt3st5har3"; Private Const stringSaltkey ="[Email Protected]&kt3st5har3ey"; Private Const intiterations =1042;//recommendation is >=        Static byte[] GetBytes (stringstr) {            varbytes =New byte[Str. Length *sizeof(Char)]; Buffer.blockcopy (str. ToCharArray (),0, Bytes,0, Bytes.            Length); returnbytes; }        Static stringGenerateKey () {//Create An instance of Symetric algorithm. Key and IV is generated automatically.DESCryptoServiceProvider Descrypto =(DESCryptoServiceProvider) descryptoserviceprovider.create (); //Use the automatically generated key for encryption.            returnASCIIEncoding.ASCII.GetString (Descrypto.key); }         Public Static voidEncryptFile (stringSrcfilename,stringdestfilename) {            varAES =Newaesmanaged (); Aes. BlockSize= -; Aes. KeySize= the; varSalt =GetBytes (Saltkey); varKey =Newrfc2898derivebytes (SKey, salt, iterations); Aes. Key= key. GetBytes (AES. KeySize/8); Aes.iv= key. GetBytes (AES. BlockSize/8); Aes. Mode=CIPHERMODE.CBC; ICryptoTransform Transform=AES. CreateEncryptor (AES.            Key, AES.IV); using(varDest =NewFileStream (destFileName, FileMode.CreateNew, FileAccess.Write, Fileshare.none)) {                using(varCryptoStream =NewCryptoStream (dest, Transform, CryptoStreamMode.Write)) {                    using(varSource =NewFileStream (Srcfilename, FileMode.Open, FileAccess.Read, FileShare.Read)) {source.                    CopyTo (CryptoStream); }                }            }        }         Public Static voidDecryptFile (stringSrcfilename,stringdestfilename) {            varAES =Newaesmanaged (); Aes. BlockSize= -; Aes. KeySize= the; varSalt =GetBytes (Saltkey); varKey =Newrfc2898derivebytes (SKey, salt, iterations); Aes. Key= key. GetBytes (AES. KeySize/8); Aes.iv= key. GetBytes (AES. BlockSize/8); Aes. Mode=CIPHERMODE.CBC; ICryptoTransform Transform=AES. CreateDecryptor (AES.            Key, AES.IV); using(varDest =NewFileStream (destFileName, FileMode.CreateNew, FileAccess.Write, Fileshare.none)) {                using(varCryptoStream =NewCryptoStream (dest, Transform, CryptoStreamMode.Write)) {                    Try                    {                        using(varSource =NewFileStream (Srcfilename, FileMode.Open, FileAccess.Read, FileShare.Read)) {source.                        CopyTo (CryptoStream); }                    }                    Catch(Cryptographicexception exception) {Throw NewApplicationException ("decryption failed.", exception); }                }            }        }    }}

Run

    Static voidMain (string[] args) {           //class1.encryptfile (@ "C:\kc_Page_2.mp4", @ "C:\kc_Page_3.mp4"); //class1.decryptfile (@ "C:\kc_Page_3.mp4", @ "C:\kc_Page_4.mp4");Class1.encryptfile (@"C:\1.docx",@"C:\2.docx"); Class1.decryptfile (@"C:\2.docx",@"C:\3.docx"); }

. NET AES encrypted video and other files

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.