当前位置: 首页 > news >正文

对称加密算法的使用Java和C#

1. JAVA中的使用

1.1.原生使用

Main函数代码

import symmetric_encryption.AESExample;  
import symmetric_encryption.BlowfishExample;  
import symmetric_encryption.DESExample;  
import symmetric_encryption.TripleDESExample;  public class App {  public static void main(String[] args) throws Exception {  AESExample.AesDemo();  DESExample.DesDemo();  TripleDESExample.TripleDESDemo();  BlowfishExample.BlowfishDemo();  }  
}

输出信息如下:

=====================AES DEMO =====================
AES Encode Key: Nm68zP3mPU1AqZ4BGmJgQQ==
AES Hex Key: 366EBCCCFDE63D4D40A99E011A626041
Encrypted (AES): Nu+3MFg0288m8m/6TWLxTw==
Decrypted (AES): Hello, World!
=====================DES DEMO =====================
DES Key: 3IV2v5u5wiw=
DES Hex Key: DC8576BF9BB9C22C
Encrypted (DES): EJQkt0ixvle+as0XaQpOnA==
Decrypted (DES): Hello, World!
=====================Triple DES Demo =====================
3DES Key: 8liisLYq+El2bnOD2g43uUYcj54ahdUH
3DES Hex Key: F258A2B0B62AF849766E7383DA0E37B9461C8F9E1A85D507
Encrypted (3DES): FNuP13xIstMKDobta9MY/g==
Decrypted (3DES): Hello, World!
=====================Blowfish Demo=====================
Blowfish Key: kRP7GtxxsNtaXzKAl0Q+Eg==
Blowfish Hex Key: 9113FB1ADC71B0DB5A5F328097443E12
Encrypted (Blowfish): dxvqQu1DfcFOx47LkmK1AA==
Decrypted (Blowfish): Hello, World!

1.1.1.AES

package symmetric_encryption;  import utils.StringByteHelper;  import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.SecretKey;  
import javax.crypto.spec.SecretKeySpec;  
import java.util.Base64;  public class AESExample {  public static void AesDemo() throws Exception {  System.out.println("=====================AES DEMO =====================");  String originalText = "Hello, World!";  // 生成 AES 密钥  KeyGenerator keyGen = KeyGenerator.getInstance("AES");  keyGen.init(128); // 可以使用 128, 192, 256 位密钥  SecretKey secretKey_Random = keyGen.generateKey();  // 输出 AES 密钥  String aesKey = Base64.getEncoder().encodeToString(secretKey_Random.getEncoded());  System.out.println("AES Encode Key: " + aesKey);  System.out.println("AES Hex Key: " + StringByteHelper.bytesToHex(secretKey_Random.getEncoded()).toUpperCase());  // 使用 Base64 编码的密钥字符串  byte[] decodedKey = Base64.getDecoder().decode(aesKey);  SecretKeySpec secretKey = new SecretKeySpec(decodedKey, "AES");  // 加密  Cipher cipher = Cipher.getInstance("AES");  cipher.init(Cipher.ENCRYPT_MODE, secretKey);  byte[] encryptedBytes = cipher.doFinal(originalText.getBytes());  String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);  System.out.println("Encrypted (AES): " + encryptedText);  // 解密  cipher.init(Cipher.DECRYPT_MODE, secretKey);  byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));  String decryptedText = new String(decryptedBytes);  System.out.println("Decrypted (AES): " + decryptedText);  }  
}

1.1.2.DES

package symmetric_encryption;  import utils.StringByteHelper;  import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.SecretKey;  
import javax.crypto.spec.SecretKeySpec;  
import java.util.Base64;  public class DESExample {  public static void DesDemo() throws Exception {  System.out.println("=====================DES DEMO =====================");  String originalText = "Hello, World!";  // 生成 DES 密钥  KeyGenerator keyGen = KeyGenerator.getInstance("DES");  keyGen.init(56); // DES 使用 56 位密钥  SecretKey secretKey_Random = keyGen.generateKey();  // 输出 AES 密钥  String aesKey = Base64.getEncoder().encodeToString(secretKey_Random.getEncoded());  System.out.println("DES Key: " + aesKey);  System.out.println("DES Hex Key: " + StringByteHelper.bytesToHex(secretKey_Random.getEncoded()).toUpperCase());  // 使用 Base64 编码的密钥字符串  byte[] decodedKey = Base64.getDecoder().decode(aesKey);  SecretKeySpec secretKey = new SecretKeySpec(decodedKey, "DES");  // 加密  Cipher cipher = Cipher.getInstance("DES");  cipher.init(Cipher.ENCRYPT_MODE, secretKey);  byte[] encryptedBytes = cipher.doFinal(originalText.getBytes());  String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);  System.out.println("Encrypted (DES): " + encryptedText);  // 解密  cipher.init(Cipher.DECRYPT_MODE, secretKey);  byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));  String decryptedText = new String(decryptedBytes);  System.out.println("Decrypted (DES): " + decryptedText);  }  
}

1.1.3.Blowfish

package symmetric_encryption;  import utils.StringByteHelper;  import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.SecretKey;  
import javax.crypto.spec.SecretKeySpec;  
import java.util.Base64;  public class BlowfishExample {  public static void BlowfishDemo() throws Exception {  System.out.println("=====================Blowfish Demo=====================");  String originalText = "Hello, World!";  // 生成 Blowfish 密钥  KeyGenerator keyGen = KeyGenerator.getInstance("Blowfish");  keyGen.init(128); // Blowfish 支持 32 到 448 位密钥  SecretKey secretKey_Random = keyGen.generateKey();  // 输出密钥  String key = Base64.getEncoder().encodeToString(secretKey_Random.getEncoded());  System.out.println("Blowfish Key: " + key);  System.out.println("Blowfish Hex Key: " + StringByteHelper.bytesToHex(secretKey_Random.getEncoded()).toUpperCase());  // 使用 Base64 编码的密钥字符串  byte[] decodedKey = Base64.getDecoder().decode(key);  SecretKeySpec secretKey = new SecretKeySpec(decodedKey, "Blowfish");  // 加密  Cipher cipher = Cipher.getInstance("Blowfish");  cipher.init(Cipher.ENCRYPT_MODE, secretKey);  byte[] encryptedBytes = cipher.doFinal(originalText.getBytes());  String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);  System.out.println("Encrypted (Blowfish): " + encryptedText);  // 解密  cipher.init(Cipher.DECRYPT_MODE, secretKey);  byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));  String decryptedText = new String(decryptedBytes);  System.out.println("Decrypted (Blowfish): " + decryptedText);  }  
}

1.1.4.3DES-DESede

package symmetric_encryption;  import utils.StringByteHelper;  import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.SecretKey;  
import javax.crypto.spec.SecretKeySpec;  
import java.util.Base64;  public class TripleDESExample {  public static void TripleDESDemo() throws Exception {  System.out.println("=====================Triple DES Demo =====================");  String originalText = "Hello, World!";  // 生成 3DES 密钥  KeyGenerator keyGen = KeyGenerator.getInstance("DESede");  keyGen.init(168); // 3DES 使用 112 或 168 位密钥  SecretKey secretKey_Random = keyGen.generateKey();  // 输出密钥  String key = Base64.getEncoder().encodeToString(secretKey_Random.getEncoded());  System.out.println("3DES Key: " + key);  System.out.println("3DES Hex Key: " + StringByteHelper.bytesToHex(secretKey_Random.getEncoded()).toUpperCase());  // 使用 Base64 编码的密钥字符串  byte[] decodedKey = Base64.getDecoder().decode(key);  SecretKeySpec secretKey = new SecretKeySpec(decodedKey, "DESede");  // 加密  Cipher cipher = Cipher.getInstance("DESede");  cipher.init(Cipher.ENCRYPT_MODE, secretKey);  byte[] encryptedBytes = cipher.doFinal(originalText.getBytes());  String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);  System.out.println("Encrypted (3DES): " + encryptedText);  // 解密  cipher.init(Cipher.DECRYPT_MODE, secretKey);  byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));  String decryptedText = new String(decryptedBytes);  System.out.println("Decrypted (3DES): " + decryptedText);  }  
}

1.2.库Bouncy Castle实现

1.2.1.pom引用

<dependency>  <groupId>org.bouncycastle</groupId>  <artifactId>bcpkix-jdk15on</artifactId>  <version>1.70</version> <!-- 请检查最新版本 -->  
</dependency>

1.2.2.代码

package symmetric_encryption.bouncy_castle;  import org.bouncycastle.jce.provider.BouncyCastleProvider;  import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.SecretKey;  
import javax.crypto.spec.SecretKeySpec;  
import java.security.Security;  
import java.util.Base64;  public class SymmetricEncryptionExample {  public static void main(String[] args) throws Exception {  Security.addProvider(new BouncyCastleProvider());  String originalText = "Hello, World!";  // AES  SecretKey aesKey = generateKey("AES");  String aesEncrypted = encrypt(originalText, aesKey, "AES");  String aesDecrypted = decrypt(aesEncrypted, aesKey, "AES");  System.out.println("AES Encrypted: " + aesEncrypted);  System.out.println("AES Decrypted: " + aesDecrypted);  // 3DES  SecretKey desedeKey = generateKey("DESede");  String desedeEncrypted = encrypt(originalText, desedeKey, "DESede");  String desedeDecrypted = decrypt(desedeEncrypted, desedeKey, "DESede");  System.out.println("3DES Encrypted: " + desedeEncrypted);  System.out.println("3DES Decrypted: " + desedeDecrypted);  // Blowfish  SecretKey blowfishKey = generateKey("Blowfish");  String blowfishEncrypted = encrypt(originalText, blowfishKey, "Blowfish");  String blowfishDecrypted = decrypt(blowfishEncrypted, blowfishKey, "Blowfish");  System.out.println("Blowfish Encrypted: " + blowfishEncrypted);  System.out.println("Blowfish Decrypted: " + blowfishDecrypted);  // Twofish  SecretKey twofishKey = generateKey("Twofish");  String twofishEncrypted = encrypt(originalText, twofishKey, "Twofish");  String twofishDecrypted = decrypt(twofishEncrypted, twofishKey, "Twofish");  System.out.println("Twofish Encrypted: " + twofishEncrypted);  System.out.println("Twofish Decrypted: " + twofishDecrypted);  // DES  SecretKey desKey = generateKey("DES");  String desEncrypted = encrypt(originalText, desKey, "DES");  String desDecrypted = decrypt(desEncrypted, desKey, "DES");  System.out.println("DES Encrypted: " + desEncrypted);  System.out.println("DES Decrypted: " + desDecrypted);  }  public static SecretKey generateKey(String algorithm) throws Exception {  KeyGenerator keyGen = KeyGenerator.getInstance(algorithm);  keyGen.init(128); // 为适应不同算法,密钥长度可以调整  return keyGen.generateKey();  }  public static String encrypt(String data, SecretKey key, String algorithm) throws Exception {  Cipher cipher = Cipher.getInstance(algorithm);  cipher.init(Cipher.ENCRYPT_MODE, key);  byte[] encryptedBytes = cipher.doFinal(data.getBytes());  return Base64.getEncoder().encodeToString(encryptedBytes);  }  public static String decrypt(String encryptedData, SecretKey key, String algorithm) throws Exception {  Cipher cipher = Cipher.getInstance(algorithm);  cipher.init(Cipher.DECRYPT_MODE, key);  byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));  return new String(decryptedBytes);  }  
}
AES Encrypted: pZ4ndMOL7nqf3BUkKG9B1Q==
AES Decrypted: Hello, World!
3DES Encrypted: 2+bdSlKx+t/ApZQJaiEUxw==
3DES Decrypted: Hello, World!
Blowfish Encrypted: PqMEK3hIK3i2px1T+3eG3A==
Blowfish Decrypted: Hello, World!
Twofish Encrypted: Zd+K4yKaeUjFYUZmMdho2w==
Twofish Decrypted: Hello, World!
DES Encrypted: oaSKCkZmMHDdJP3tFVXnQQ==
DES Decrypted: Hello, World!

1.3. Hutool库实现

package symmetric_encryption.hutool;  import cn.hutool.crypto.symmetric.AES;  
import cn.hutool.crypto.symmetric.DES;  
import cn.hutool.crypto.symmetric.DESede;  import java.nio.charset.StandardCharsets;  
import java.util.Base64;  public class HutoolCustomKeyExample {  public static void main(String[] args) {  String originalText = "Hello, World!";  // 自定义密钥(示例)  byte[] aesKey = "1234567890123456".getBytes(StandardCharsets.UTF_8); // 16 字节的 AES 密钥  byte[] desedeKey = "123456789012345678901234".getBytes(StandardCharsets.UTF_8); // 24 字节的 3DES 密钥  byte[] desKey = "12345678".getBytes(StandardCharsets.UTF_8); // 8 字节的 DES 密钥  // AES 加密  AES aes = new AES(aesKey);  String aesEncrypted = Base64.getEncoder().encodeToString(aes.encrypt(originalText.getBytes()));  String aesDecrypted = new String(aes.decrypt(Base64.getDecoder().decode(aesEncrypted)));  System.out.println("AES Encrypted: " + aesEncrypted);  System.out.println("AES Decrypted: " + aesDecrypted);  // 3DES 加密  DESede desede = new DESede(desedeKey);  String desedeEncrypted = Base64.getEncoder().encodeToString(desede.encrypt(originalText.getBytes()));  String desedeDecrypted = new String(desede.decrypt(Base64.getDecoder().decode(desedeEncrypted)));  System.out.println("3DES Encrypted: " + desedeEncrypted);  System.out.println("3DES Decrypted: " + desedeDecrypted);  // DES 加密  DES des = new DES(desKey);  String desEncrypted = Base64.getEncoder().encodeToString(des.encrypt(originalText.getBytes()));  String desDecrypted = new String(des.decrypt(Base64.getDecoder().decode(desEncrypted)));  System.out.println("DES Encrypted: " + desEncrypted);  System.out.println("DES Decrypted: " + desDecrypted);  }  
}
AES Encrypted: s1aiR0qHAayxg11CyTDX1Q==
AES Decrypted: Hello, World!
3DES Encrypted: D15pY2FWB+GdK4k1cty80g==
3DES Decrypted: Hello, World!
DES Encrypted: mtC0+LarYAf+btyyuqKiyw==
DES Decrypted: Hello, World!

2.C#中的使用

2.1.原生使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;namespace ConsoleSymmetricEncryption.SymmetricEncryption
{internal class SymmetricEncryptionExample{public static void EncryptionDemo(){string originalText = "Hello, World!";// AES 加密var aesKey = GenerateKey(16); // 16 字节密钥(128 位)string aesEncrypted = Encrypt(originalText, aesKey, "AES");string aesDecrypted = Decrypt(aesEncrypted, aesKey, "AES");Console.WriteLine($"AES Encrypted: {aesEncrypted}");Console.WriteLine($"AES Decrypted: {aesDecrypted}");// 3DES 加密var desedeKey = GenerateKey(24); // 24 字节密钥(192 位)string desedeEncrypted = Encrypt_DESede(originalText, desedeKey, "DESede");string desedeDecrypted = Decrypt_DESede(desedeEncrypted, desedeKey, "DESede");Console.WriteLine($"3DES Encrypted: {desedeEncrypted}");Console.WriteLine($"3DES Decrypted: {desedeDecrypted}");// DES 加密var desKey = GenerateKey(8); // 8 字节密钥(64 位)string desEncrypted = Encrypt(originalText, desKey, "DES");string desDecrypted = Decrypt(desEncrypted, desKey, "DES");Console.WriteLine($"DES Encrypted: {desEncrypted}");Console.WriteLine($"DES Decrypted: {desDecrypted}");}private static byte[] GenerateKey(int keySize){using (var rng = new RNGCryptoServiceProvider()){byte[] key = new byte[keySize];rng.GetBytes(key);return key;}}private static string Encrypt_DESede(string data, byte[] key, string algorithm){using (var cipher = new TripleDESCryptoServiceProvider()){cipher.Key = key;cipher.GenerateIV();var iv = cipher.IV;using (var encryptor = cipher.CreateEncryptor())using (var ms = new MemoryStream()){ms.Write(iv, 0, iv.Length); // 写入 IVusing (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)){var dataBytes = Encoding.UTF8.GetBytes(data);cs.Write(dataBytes, 0, dataBytes.Length);}return Convert.ToBase64String(ms.ToArray());}}}private static string Decrypt_DESede(string encryptedData, byte[] key, string algorithm){var fullCipher = Convert.FromBase64String(encryptedData);using (var cipher = new TripleDESCryptoServiceProvider()){cipher.Key = key;var iv = new byte[cipher.BlockSize / 8];Array.Copy(fullCipher, 0, iv, 0, iv.Length); // 获取 IVcipher.IV = iv;using (var decryptor = cipher.CreateDecryptor())using (var ms = new MemoryStream(fullCipher, iv.Length, fullCipher.Length - iv.Length))using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))using (var sr = new StreamReader(cs)){return sr.ReadToEnd();}}}private static string Encrypt(string data, byte[] key, string algorithm){using (var cipher = SymmetricAlgorithm.Create(algorithm)){cipher.Key = key;cipher.GenerateIV();var iv = cipher.IV;using (var encryptor = cipher.CreateEncryptor())using (var ms = new MemoryStream()){ms.Write(iv, 0, iv.Length); // 写入 IVusing (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)){var dataBytes = Encoding.UTF8.GetBytes(data);cs.Write(dataBytes, 0, dataBytes.Length);}return Convert.ToBase64String(ms.ToArray());}}}private static string Decrypt(string encryptedData, byte[] key, string algorithm){var fullCipher = Convert.FromBase64String(encryptedData);using (var cipher = SymmetricAlgorithm.Create(algorithm)){cipher.Key = key;var iv = new byte[cipher.BlockSize / 8];Array.Copy(fullCipher, 0, iv, 0, iv.Length); // 获取 IVcipher.IV = iv;using (var decryptor = cipher.CreateDecryptor())using (var ms = new MemoryStream(fullCipher, iv.Length, fullCipher.Length - iv.Length))using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))using (var sr = new StreamReader(cs)){return sr.ReadToEnd();}}}}
}
AES Encrypted: R6N1mEsRcthLag9ayCfb9fU7CuN1jTOG/keXA8fcUTM=
AES Decrypted: Hello, World!
3DES Encrypted: XLXkXyHIL2G/FPT7T91wk8DxbiEVdwtF
3DES Decrypted: Hello, World!
DES Encrypted: dxPu5Z0nwy6GqJ26hVSm0oR5JhWxDQGF
DES Decrypted: Hello, World!

2.2. 库Bouncy Castle实现

using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;public class BouncyCastleSymmetricEncryptionExample
{public static void EncryptionDemo(){string originalText = "Hello, World!";byte[] desKey = GenerateKey(8); // DES 需要 8 字节密钥byte[] tripleDesKey = GenerateKey(24); // 3DES 需要 24 字节密钥// 示例密钥byte[] aesKey = GenerateKey(16); // 16字节密钥 (128位)string strKey = Convert.ToBase64String(aesKey);string strDesKey = Convert.ToBase64String(desKey);string strTripleDesKey = Convert.ToBase64String(tripleDesKey);// 示例密钥byte[] blowfishKey = GenerateKey(16); // Blowfish 需要 32 字节密钥byte[] twofishKey = GenerateKey(32); // Twofish 需要 16 字节密钥string strBlowfishKey = Convert.ToBase64String(blowfishKey);string strTwofishKey = Convert.ToBase64String(twofishKey);string strEncryptValue = AesEncrypt(strKey, originalText);Console.WriteLine($"Encrypted: {strEncryptValue}");string strDecryptValue = AesDecrypt(strKey, strEncryptValue);Console.WriteLine($"Decrypted: {strDecryptValue}");// DES 加解密string desEncrypted = DesEncrypt(strDesKey, originalText);Console.WriteLine($"DES Encrypted: {desEncrypted}");string desDecrypted = DesDecrypt(strDesKey, desEncrypted);Console.WriteLine($"DES Decrypted: {desDecrypted}");// 3DES 加解密string tripleDesEncrypted = TripleDesEncrypt(strTripleDesKey, originalText);Console.WriteLine($"3DES Encrypted: {tripleDesEncrypted}");string tripleDesDecrypted = TripleDesDecrypt(strTripleDesKey, tripleDesEncrypted);Console.WriteLine($"3DES Decrypted: {tripleDesDecrypted}");// Blowfish 加解密string blowfishEncrypted = BlowfishEncrypt(strBlowfishKey, originalText);Console.WriteLine($"Blowfish Encrypted: {blowfishEncrypted}");string blowfishDecrypted = BlowfishDecrypt(strBlowfishKey, blowfishEncrypted);Console.WriteLine($"Blowfish Decrypted: {blowfishDecrypted}");// Twofish 加解密string twofishEncrypted = TwofishEncrypt(strTwofishKey, originalText);Console.WriteLine($"Twofish Encrypted: {twofishEncrypted}");string twofishDecrypted = TwofishDecrypt(strTwofishKey, twofishEncrypted);Console.WriteLine($"Twofish Decrypted: {twofishDecrypted}");}private static byte[] GenerateKey(int length){byte[] key = new byte[length];new SecureRandom().NextBytes(key);return key;}public static string AesEncrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(data);IBufferedCipher cipher = CipherUtilities.GetCipher("AES/ECB/PKCS7Padding");cipher.Init(true, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return Convert.ToBase64String(result);}public static string AesDecrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = Convert.FromBase64String(data);IBufferedCipher cipher = CipherUtilities.GetCipher("AES/ECB/PKCS7Padding");cipher.Init(false, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return System.Text.Encoding.UTF8.GetString(result);}public static string DesEncrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(data);IBufferedCipher cipher = CipherUtilities.GetCipher("DES/ECB/PKCS7Padding");cipher.Init(true, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return Convert.ToBase64String(result);}public static string DesDecrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = Convert.FromBase64String(data);IBufferedCipher cipher = CipherUtilities.GetCipher("DES/ECB/PKCS7Padding");cipher.Init(false, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return System.Text.Encoding.UTF8.GetString(result);}public static string TripleDesEncrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(data);IBufferedCipher cipher = CipherUtilities.GetCipher("DESede/ECB/PKCS7Padding");cipher.Init(true, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return Convert.ToBase64String(result);}public static string TripleDesDecrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = Convert.FromBase64String(data);IBufferedCipher cipher = CipherUtilities.GetCipher("DESede/ECB/PKCS7Padding");cipher.Init(false, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return System.Text.Encoding.UTF8.GetString(result);}public static string BlowfishEncrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(data);IBufferedCipher cipher = CipherUtilities.GetCipher("Blowfish/ECB/PKCS7Padding");cipher.Init(true, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return Convert.ToBase64String(result);}public static string BlowfishDecrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = Convert.FromBase64String(data);IBufferedCipher cipher = CipherUtilities.GetCipher("Blowfish/ECB/PKCS7Padding");cipher.Init(false, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return System.Text.Encoding.UTF8.GetString(result);}public static string TwofishEncrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(data);IBufferedCipher cipher = CipherUtilities.GetCipher("Twofish/ECB/PKCS7Padding");cipher.Init(true, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return Convert.ToBase64String(result);}public static string TwofishDecrypt(string key, string data){byte[] keyBytes = Convert.FromBase64String(key);byte[] dataBytes = Convert.FromBase64String(data);IBufferedCipher cipher = CipherUtilities.GetCipher("Twofish/ECB/PKCS7Padding");cipher.Init(false, new KeyParameter(keyBytes));byte[] result = cipher.DoFinal(dataBytes);return System.Text.Encoding.UTF8.GetString(result);}
}
Encrypted: P3a/jU6DQo1t2u2/OHKY+A==
Decrypted: Hello, World!
DES Encrypted: N/AEce4Jfdo3mM2YAMM2OA==
DES Decrypted: Hello, World!
3DES Encrypted: tEHiBxWvexxm09rjzr9T6A==
3DES Decrypted: Hello, World!
Blowfish Encrypted: hlHHRAv/oeHTzE0YsT6fIA==
Blowfish Decrypted: Hello, World!
Twofish Encrypted: 9SXoH4tB8gX3Cm/UcUoD3g==
Twofish Decrypted: Hello, World!

http://www.mrgr.cn/news/43402.html

相关文章:

  • 【C++】vector
  • 洛谷刷题 P1042 [NOIP2003 普及组] 乒乓球
  • Linux dlsym符号查找疑惑分析
  • SAP MM学习笔记 - 豆知识10 - OMSY 初期化会计期间,ABAP调用MMPV/MMRV来批量更新会计期间(TODO)
  • 懒洋洋浅谈--机器学习框架
  • 网络通信——OSPF和RIP的区别(总结)
  • 【漏洞复现】锐捷 RG-EW1200G 无线路由器 登录绕过
  • STL07——手写一个简单版本的unordered_set
  • Error while loading conda entry point: conda-libmamba-solver
  • C++ 语言特性29 - 协程介绍
  • 【Python】Dejavu:Python 音频指纹识别库详解
  • 【运动控制】关于GPIO的NPN型输入与NPN漏型输入
  • 算法工程师重生之第二十天(组合总和 组合总和II 分割回文串 )
  • 2024/10/5 数据结构打卡
  • 【MySQL】数据库基础
  • 几个卷积神经网络(CNN)可视化的网站
  • 快仓智能斩获过亿美元D轮融资,加速全球智能仓储与物流布局
  • 优化理论及应用精解【21】
  • 直立行走机器人技术概述
  • CSS选择器 快速入门