赞
踩
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- class AESEncryption {
-
- static byte[] secretKey = Encoding.ASCII.GetBytes("YourSecretKey");
- static byte[] iv = { 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54 };
- static int blockSize = 16;
-
- static byte[] Encrypt(byte[] plainBytes) {
- // Pad the input plaintext with zeroes to make it a multiple of the block size
- int paddingSize = blockSize - (plainBytes.Length % blockSize);
- if (paddingSize > 0 && paddingSize < blockSize) {
- byte[] paddedBytes = new byte[plainBytes.Length + paddingSize];
- Buffer.BlockCopy(plainBytes, 0, paddedBytes, 0, plainBytes.Length);
- plainBytes = paddedBytes;
- }
-
- // Create the AES cipher in CBC mode with zero padding
- AESCipher aes = new AESCipher(secretKey, iv);
-
- // Encrypt the padded plaintext using CBC mode with zero padding
- byte[] encryptedBytes = aes.EncryptCBC(plainBytes);
-
- return encryptedBytes;
- }
-
- static byte[] Decrypt(byte[] cipherBytes) {
- // Create the AES cipher in CBC mode with zero padding
- AESCipher aes = new AESCipher(secretKey, iv);
-
- // Decrypt the ciphertext using CBC mode with zero padding
- byte[] decryptedBytes = aes.DecryptCBC(cipherBytes);
-
- // Remove any trailing zero-padding from the decrypted data
- int nullCount = 0;
- for (int i = decryptedBytes.Length - 1; i >= 0; i--) {
- if (decryptedBytes[i] == 0) {
- nullCount++;
- } else {
- break;
- }
- }
- if (nullCount > 0) {
- byte[] unpaddedBytes = new byte[decryptedBytes.Length - nullCount];
- Buffer.BlockCopy(decryptedBytes, 0, unpaddedBytes, 0, unpaddedBytes.Length);
- decryptedBytes = unpaddedBytes;
- }
-
- return decryptedBytes;
- }
-
- static void Main(string[] args) {
- Console.Write("Enter plaintext: ");
- string plaintext = Console.ReadLine();
- byte[] plainBytes = Encoding.ASCII.GetBytes(plaintext);
- byte[] encryptedBytes = Encrypt(plainBytes);
- string encryptedText = Convert.ToBase64String(encryptedBytes);
- Console.WriteLine("Encrypted Text: " + encryptedText);
- byte[] cipherBytes = Convert.FromBase64String(encryptedText);
- byte[] decryptedBytes = Decrypt(cipherBytes);
- string decryptedText = Encoding.ASCII.GetString(decryptedBytes);
- Console.WriteLine("Decrypted Text: " + decryptedText);
- }
- }
-
- class AESCipher {
-
- byte[] key;
- byte[] iv;
-
- public AESCipher(byte[] key, byte[] iv) {
- this.key = key;
- this.iv = iv;
- }
-
- public byte[] EncryptCBC(byte[] plainBytes) {
- byte[] prevCipherBlock = iv;
- byte[] outputBytes = new byte[plainBytes.Length];
- for (int i = 0; i < plainBytes.Length; i += blockSize) {
- byte[] plainBlock = new byte[blockSize];
- byte[] cipherBlock = new byte[blockSize];
- Buffer.BlockCopy(plainBytes, i, plainBlock, 0, blockSize);
-
- // XOR the previous ciphertext block with the current plaintext block
- for (int j = 0; j < blockSize; j++) {
- plainBlock[j] ^= prevCipherBlock[j];
- }
-
- // AES encrypt the XORed block with the secret key
- cipherBlock = EncryptAES(plainBlock);
-
- // Copy the ciphertext block to the output buffer and update the previous ciphertext block
- Buffer.BlockCopy(cipherBlock, 0, outputBytes, i, blockSize);
- prevCipherBlock = cipherBlock;
- }
- return outputBytes;
- }
-
- public byte[] DecryptCBC(byte[] cipherBytes) {
- byte[] prevCipherBlock = iv;
- byte[] outputBytes = new byte[cipherBytes.Length];
- for (int i = 0; i < cipherBytes.Length; i += blockSize) {
- byte[] cipherBlock = new byte[blockSize];
- byte[] plainBlock = new byte[blockSize];
- Buffer.BlockCopy(cipherBytes, i, cipherBlock, 0, blockSize);
-
- // AES decrypt the ciphertext block with the secret key
- plainBlock = DecryptAES(cipherBlock);
-
- // XOR the plaintext block with the previous ciphertext block
- for (int j = 0; j < blockSize; j++) {
- plainBlock[j] ^= prevCipherBlock[j];
- }
-
- // Copy the plaintext block to the output buffer and update the previous ciphertext block
- Buffer.BlockCopy(plainBlock, 0, outputBytes, i, blockSize);
- prevCipherBlock = cipherBlock;
- }
- return outputBytes;
- }
-
- private byte[] EncryptAES(byte[] plainBytes) {
- byte[] cipherBytes = null;
- using (AesManaged aes = new AesManaged()) {
- aes.Key = key;
- aes.Mode = CipherMode.ECB;
- aes.Padding = PaddingMode.None;
-
- ICryptoTransform encryptor = aes.CreateEncryptor();
- cipherBytes = encryptor.TransformFinalBlock(plainBytes, 0, blockSize);
- }
- return cipherBytes;
- }
-
- private byte[] DecryptAES(byte[] cipherBytes) {
- byte[] plainBytes = null;
- using (AesManaged aes = new AesManaged()) {
- aes.Key = key;
- aes.Mode = CipherMode.ECB;
- aes.Padding = PaddingMode.None;
-
- ICryptoTransform decryptor = aes.CreateDecryptor();
- plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, blockSize);
- }
- return plainBytes;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。