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