当前位置: 首页 > 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!

相关文章:

对称加密算法的使用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…...

10款好用的开源 HarmonyOS 工具库

大家好&#xff0c;我是 V 哥&#xff0c;今天给大家分享10款好用的 HarmonyOS的工具库&#xff0c;在开发鸿蒙应用时可以用下&#xff0c;好用的工具可以简化代码&#xff0c;让你写出优雅的应用来。废话不多说&#xff0c;马上开整。 1. efTool efTool是一个功能丰富且易用…...

ubuntu22.04中备份Iptables的设置

在 Ubuntu 22.04 中备份 iptables 的设置&#xff0c;您可以采用以下几种方法&#xff1a; 使用 iptables-save 命令&#xff1a; 您可以使用 iptables-save 命令将当前的 iptables 规则保存到文件中。例如&#xff0c;要将规则保存到 /etc/iptables/rules.v4 文件中&#xff0…...

(PyTorch) 深度学习框架-介绍篇

前言 在当今科技飞速发展的时代&#xff0c;人工智能尤其是深度学习领域正以惊人的速度改变着我们的世界。从图像识别、语音处理到自然语言处理&#xff0c;深度学习技术在各个领域都取得了显著的成就&#xff0c;为解决复杂的现实问题提供了强大的工具和方法。 PyTorch 是一个…...

若依从redis中获取用户列表

因为若依放入用户的时候&#xff0c;会在减值中添加随机串&#xff0c;所以用户的key会在redis中变成&#xff1a; login_tokens:6af07052-b76d-44dd-a296-1335af03b2a6 这样的样子。 如果用 Set<Object> items redisService.redisTemplate.keys("login_tokens&…...

文件上传之%00截断(00截断)以及pikachu靶场

pikachu的文件上传和upload-lab的文件上传 目录 mime type类型 getimagesize 第12关%00截断&#xff0c; 第13关0x00截断 差不多了&#xff0c;今天先学文件上传白名单&#xff0c;在网上看了资料&#xff0c;差不多看懂了&#xff0c;但是还有几个地方需要实验一下&#…...

Chainlit集成LlamaIndex并使用通义千问实现和数据库交互的网页对话应用(text2sql)

前言 我在之前的几篇文章中写了如何使用Chainlit集成Langchain并使用通义千问实现和数据库交互的网页对话应用&#xff0c;但是发现Langchain的几种和数据库交互的组件都不够让我满意&#xff0c;虽然已经满足了大部分场景的需求&#xff0c;但是问题还是很多&#xff0c;比如…...

计组复习笔记

计组笔记 汇编部分 通用寄存器&#xff08;General Registers&#xff09;: AX (Accumulator): 用于累加运算&#xff0c;也是乘法和除法的默认寄存器。BX (Base Register): 可以用作一个基址寄存器&#xff0c;通常用于存放数据的基地址。CX (Counter Register): 通常用于循环…...

62. 环境贴图2

环境贴图作用测试 实际生活中光源照射到一个物体上&#xff0c;这个物体反射出去的光线也会影响其他的物体&#xff0c;环境贴图就是用一种简单方式&#xff0c;近似模拟一个物体周边环境对物体表面的影响。 测试&#xff1a;对于PBR材质&#xff0c;如果threejs三维场景不添…...

MATLAB中数据导入与导出的全面指南

在MATLAB中&#xff0c;数据的导入与导出是数据处理工作流中的两个基本步骤。导入是将外部数据加载到MATLAB工作区的过程&#xff0c;而导出则是将工作区中的数据保存到外部文件中。这两个步骤对于数据分析、可视化和结果共享至关重要。本文将详细介绍如何在MATLAB中进行数据的…...

Jenkins从入门到精通,构建高效自动化流程

目录 一、Jenkins简介1、Jenkins的历史与发展&#xff08;1&#xff09;Jenkins的起源&#xff08;2&#xff09;Jenkins的发展&#xff08;3&#xff09;Jenkins的社区与生态系统&#xff08;4&#xff09;Jenkins在我国的发展 2、Jenkins的核心功能3、Jenkins的应用场景 二、…...

【Android 13源码分析】Activity生命周期之onCreate,onStart,onResume-2

忽然有一天&#xff0c;我想要做一件事&#xff1a;去代码中去验证那些曾经被“灌输”的理论。                                                                                  – 服装…...

如何在电脑上浏览手机界面

联想浏览器中&#xff0c;点击右键-》检查&#xff0c;进入开发者工具&#xff1a; 点击如上&#xff0c;红色框框选中的手机浏览模式即可。...

国产RISC-V案例分享,基于全志T113-i异构多核平台!

RISC-V核心优势 全志T113-i是一款双核Cortex-A7@1.2GHz国产工业级处理器平台,并内置玄铁C906 RISC-V和HiFi4 DSP双副核心,可流畅运行Linux系统与Qt界面,并已适配OpenWRT系统、Docker容器技术。 而其中的RISC-V属于超高能效副核心,主频高达1008MHz,标配内存管理单元,可运…...

Day(16)--File

File File对象就是表示一个路径&#xff0c;可以是文件路径&#xff0c;也可以是文件夹的路径 这个路径可以是存在的&#xff0c;也允许是不存在的 常见的方法 public File(String pathname);//根据文件路径创建文件对象 public File(String parent,String child);//根据父路…...

Axios入门使用

文章目录 Axios入门使用一、引言二、Axios的安装与配置1、安装Axios2、创建Axios实例 三、发送HTTP请求1、GET请求2、POST请求3、并发请求 四、配置和拦截器1、配置默认值2、拦截器 五、错误处理和取消请求1、错误处理2、取消请求 四、总结 Axios入门使用 一、引言 随着前端技…...

大数据实时数仓Hologres(四):基于Flink+Hologres搭建实时数仓

文章目录 基于Flink+Hologres搭建实时数仓 一、使用示例 二、方案架构 1、架构优势 2、Hologres核心优势 三、实践场景 四、项目准备 1、创建阿里云账号AccessKey 2、准备MySQL数据源 五、构建实时数仓​编辑 1、管理元数据 2、构建ODS层 2.1、创建CDAS同步作业O…...

关于HTML 案例_个人简历展示02

展示效果 用table进行布局label 标签进行关联 例如&#xff1a;点姓名就可以到text中去填写内容 input的使用 text 文本框radio 单选框select与option 选择框checkbox 复选框 textareaul与li 无序列表文中图片是本地的 链接: 图片下载地址 代码 <!DOCTYPE html> <…...

Windows 11 24H2 v26100.1742 官方简体中文版

‌Windows 11 24H2是微软最新推出的操作系统更新&#xff0c;其在人工智能&#xff08;AI&#xff09;领域的创新为用户带来了显著的体验提升。该版本的一大亮点是AI Copilot&#xff0c;它能够智能地根据剪贴板内容调整操作上下文菜单&#xff0c;实现更智能化的交互。 此外&…...

【AIGC半月报】AIGC大模型启元:2024.10(上)

【AIGC半月报】AIGC大模型启元&#xff1a;2024.10&#xff08;上&#xff09; (1) YOLO11&#xff08;Ultralytics新作&#xff09; (1) YOLO11&#xff08;Ultralytics新作&#xff09; 2024.10.01 Ultralytics在 YOLO Vision 2024 活动上宣布发布其新的计算机视觉模型 YOLO…...

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计&#xff1a;let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性&#xff0c;这种设计体现了语言的核心哲学。以下是深度解析&#xff1a; 1.1 设计理念剖析 安全优先原则&#xff1a;默认不可变强制开发者明确声明意图 let x 5; …...

利用ngx_stream_return_module构建简易 TCP/UDP 响应网关

一、模块概述 ngx_stream_return_module 提供了一个极简的指令&#xff1a; return <value>;在收到客户端连接后&#xff0c;立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量&#xff08;如 $time_iso8601、$remote_addr 等&#xff09;&a…...

golang循环变量捕获问题​​

在 Go 语言中&#xff0c;当在循环中启动协程&#xff08;goroutine&#xff09;时&#xff0c;如果在协程闭包中直接引用循环变量&#xff0c;可能会遇到一个常见的陷阱 - ​​循环变量捕获问题​​。让我详细解释一下&#xff1a; 问题背景 看这个代码片段&#xff1a; fo…...

边缘计算医疗风险自查APP开发方案

核心目标:在便携设备(智能手表/家用检测仪)部署轻量化疾病预测模型,实现低延迟、隐私安全的实时健康风险评估。 一、技术架构设计 #mermaid-svg-iuNaeeLK2YoFKfao {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg…...

线程同步:确保多线程程序的安全与高效!

全文目录&#xff1a; 开篇语前序前言第一部分&#xff1a;线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分&#xff1a;synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分&#xff…...

【大模型RAG】Docker 一键部署 Milvus 完整攻略

本文概要 Milvus 2.5 Stand-alone 版可通过 Docker 在几分钟内完成安装&#xff1b;只需暴露 19530&#xff08;gRPC&#xff09;与 9091&#xff08;HTTP/WebUI&#xff09;两个端口&#xff0c;即可让本地电脑通过 PyMilvus 或浏览器访问远程 Linux 服务器上的 Milvus。下面…...

AI编程--插件对比分析:CodeRider、GitHub Copilot及其他

AI编程插件对比分析&#xff1a;CodeRider、GitHub Copilot及其他 随着人工智能技术的快速发展&#xff0c;AI编程插件已成为提升开发者生产力的重要工具。CodeRider和GitHub Copilot作为市场上的领先者&#xff0c;分别以其独特的特性和生态系统吸引了大量开发者。本文将从功…...

Java 二维码

Java 二维码 **技术&#xff1a;**谷歌 ZXing 实现 首先添加依赖 <!-- 二维码依赖 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.5.1</version></dependency><de…...

学习一下用鸿蒙​​DevEco Studio HarmonyOS5实现百度地图

在鸿蒙&#xff08;HarmonyOS5&#xff09;中集成百度地图&#xff0c;可以通过以下步骤和技术方案实现。结合鸿蒙的分布式能力和百度地图的API&#xff0c;可以构建跨设备的定位、导航和地图展示功能。 ​​1. 鸿蒙环境准备​​ ​​开发工具​​&#xff1a;下载安装 ​​De…...

uniapp 实现腾讯云IM群文件上传下载功能

UniApp 集成腾讯云IM实现群文件上传下载功能全攻略 一、功能背景与技术选型 在团队协作场景中&#xff0c;群文件共享是核心需求之一。本文将介绍如何基于腾讯云IMCOS&#xff0c;在uniapp中实现&#xff1a; 群内文件上传/下载文件元数据管理下载进度追踪跨平台文件预览 二…...