当前位置:   article > 正文

C#手动实现的AES加密算法示例_c# aes加密

c# aes加密
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. class AESEncryption {
  5. static byte[] secretKey = Encoding.ASCII.GetBytes("YourSecretKey");
  6. static byte[] iv = { 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54 };
  7. static int blockSize = 16;
  8. static byte[] Encrypt(byte[] plainBytes) {
  9. // Pad the input plaintext with zeroes to make it a multiple of the block size
  10. int paddingSize = blockSize - (plainBytes.Length % blockSize);
  11. if (paddingSize > 0 && paddingSize < blockSize) {
  12. byte[] paddedBytes = new byte[plainBytes.Length + paddingSize];
  13. Buffer.BlockCopy(plainBytes, 0, paddedBytes, 0, plainBytes.Length);
  14. plainBytes = paddedBytes;
  15. }
  16. // Create the AES cipher in CBC mode with zero padding
  17. AESCipher aes = new AESCipher(secretKey, iv);
  18. // Encrypt the padded plaintext using CBC mode with zero padding
  19. byte[] encryptedBytes = aes.EncryptCBC(plainBytes);
  20. return encryptedBytes;
  21. }
  22. static byte[] Decrypt(byte[] cipherBytes) {
  23. // Create the AES cipher in CBC mode with zero padding
  24. AESCipher aes = new AESCipher(secretKey, iv);
  25. // Decrypt the ciphertext using CBC mode with zero padding
  26. byte[] decryptedBytes = aes.DecryptCBC(cipherBytes);
  27. // Remove any trailing zero-padding from the decrypted data
  28. int nullCount = 0;
  29. for (int i = decryptedBytes.Length - 1; i >= 0; i--) {
  30. if (decryptedBytes[i] == 0) {
  31. nullCount++;
  32. } else {
  33. break;
  34. }
  35. }
  36. if (nullCount > 0) {
  37. byte[] unpaddedBytes = new byte[decryptedBytes.Length - nullCount];
  38. Buffer.BlockCopy(decryptedBytes, 0, unpaddedBytes, 0, unpaddedBytes.Length);
  39. decryptedBytes = unpaddedBytes;
  40. }
  41. return decryptedBytes;
  42. }
  43. static void Main(string[] args) {
  44. Console.Write("Enter plaintext: ");
  45. string plaintext = Console.ReadLine();
  46. byte[] plainBytes = Encoding.ASCII.GetBytes(plaintext);
  47. byte[] encryptedBytes = Encrypt(plainBytes);
  48. string encryptedText = Convert.ToBase64String(encryptedBytes);
  49. Console.WriteLine("Encrypted Text: " + encryptedText);
  50. byte[] cipherBytes = Convert.FromBase64String(encryptedText);
  51. byte[] decryptedBytes = Decrypt(cipherBytes);
  52. string decryptedText = Encoding.ASCII.GetString(decryptedBytes);
  53. Console.WriteLine("Decrypted Text: " + decryptedText);
  54. }
  55. }
  56. class AESCipher {
  57. byte[] key;
  58. byte[] iv;
  59. public AESCipher(byte[] key, byte[] iv) {
  60. this.key = key;
  61. this.iv = iv;
  62. }
  63. public byte[] EncryptCBC(byte[] plainBytes) {
  64. byte[] prevCipherBlock = iv;
  65. byte[] outputBytes = new byte[plainBytes.Length];
  66. for (int i = 0; i < plainBytes.Length; i += blockSize) {
  67. byte[] plainBlock = new byte[blockSize];
  68. byte[] cipherBlock = new byte[blockSize];
  69. Buffer.BlockCopy(plainBytes, i, plainBlock, 0, blockSize);
  70. // XOR the previous ciphertext block with the current plaintext block
  71. for (int j = 0; j < blockSize; j++) {
  72. plainBlock[j] ^= prevCipherBlock[j];
  73. }
  74. // AES encrypt the XORed block with the secret key
  75. cipherBlock = EncryptAES(plainBlock);
  76. // Copy the ciphertext block to the output buffer and update the previous ciphertext block
  77. Buffer.BlockCopy(cipherBlock, 0, outputBytes, i, blockSize);
  78. prevCipherBlock = cipherBlock;
  79. }
  80. return outputBytes;
  81. }
  82. public byte[] DecryptCBC(byte[] cipherBytes) {
  83. byte[] prevCipherBlock = iv;
  84. byte[] outputBytes = new byte[cipherBytes.Length];
  85. for (int i = 0; i < cipherBytes.Length; i += blockSize) {
  86. byte[] cipherBlock = new byte[blockSize];
  87. byte[] plainBlock = new byte[blockSize];
  88. Buffer.BlockCopy(cipherBytes, i, cipherBlock, 0, blockSize);
  89. // AES decrypt the ciphertext block with the secret key
  90. plainBlock = DecryptAES(cipherBlock);
  91. // XOR the plaintext block with the previous ciphertext block
  92. for (int j = 0; j < blockSize; j++) {
  93. plainBlock[j] ^= prevCipherBlock[j];
  94. }
  95. // Copy the plaintext block to the output buffer and update the previous ciphertext block
  96. Buffer.BlockCopy(plainBlock, 0, outputBytes, i, blockSize);
  97. prevCipherBlock = cipherBlock;
  98. }
  99. return outputBytes;
  100. }
  101. private byte[] EncryptAES(byte[] plainBytes) {
  102. byte[] cipherBytes = null;
  103. using (AesManaged aes = new AesManaged()) {
  104. aes.Key = key;
  105. aes.Mode = CipherMode.ECB;
  106. aes.Padding = PaddingMode.None;
  107. ICryptoTransform encryptor = aes.CreateEncryptor();
  108. cipherBytes = encryptor.TransformFinalBlock(plainBytes, 0, blockSize);
  109. }
  110. return cipherBytes;
  111. }
  112. private byte[] DecryptAES(byte[] cipherBytes) {
  113. byte[] plainBytes = null;
  114. using (AesManaged aes = new AesManaged()) {
  115. aes.Key = key;
  116. aes.Mode = CipherMode.ECB;
  117. aes.Padding = PaddingMode.None;
  118. ICryptoTransform decryptor = aes.CreateDecryptor();
  119. plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, blockSize);
  120. }
  121. return plainBytes;
  122. }
  123. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号