对称加密算法的使用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 工具库
大家好,我是 V 哥,今天给大家分享10款好用的 HarmonyOS的工具库,在开发鸿蒙应用时可以用下,好用的工具可以简化代码,让你写出优雅的应用来。废话不多说,马上开整。 1. efTool efTool是一个功能丰富且易用…...

ubuntu22.04中备份Iptables的设置
在 Ubuntu 22.04 中备份 iptables 的设置,您可以采用以下几种方法: 使用 iptables-save 命令: 您可以使用 iptables-save 命令将当前的 iptables 规则保存到文件中。例如,要将规则保存到 /etc/iptables/rules.v4 文件中࿰…...

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

若依从redis中获取用户列表
因为若依放入用户的时候,会在减值中添加随机串,所以用户的key会在redis中变成: 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截断, 第13关0x00截断 差不多了,今天先学文件上传白名单,在网上看了资料,差不多看懂了,但是还有几个地方需要实验一下&#…...

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

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

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

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

Jenkins从入门到精通,构建高效自动化流程
目录 一、Jenkins简介1、Jenkins的历史与发展(1)Jenkins的起源(2)Jenkins的发展(3)Jenkins的社区与生态系统(4)Jenkins在我国的发展 2、Jenkins的核心功能3、Jenkins的应用场景 二、…...

【Android 13源码分析】Activity生命周期之onCreate,onStart,onResume-2
忽然有一天,我想要做一件事:去代码中去验证那些曾经被“灌输”的理论。 – 服装…...

如何在电脑上浏览手机界面
联想浏览器中,点击右键-》检查,进入开发者工具: 点击如上,红色框框选中的手机浏览模式即可。...

国产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对象就是表示一个路径,可以是文件路径,也可以是文件夹的路径 这个路径可以是存在的,也允许是不存在的 常见的方法 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 标签进行关联 例如:点姓名就可以到text中去填写内容 input的使用 text 文本框radio 单选框select与option 选择框checkbox 复选框 textareaul与li 无序列表文中图片是本地的 链接: 图片下载地址 代码 <!DOCTYPE html> <…...

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

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

Codeforces Beta Round 14 (Div. 2) E. Camels (DP)
题目 Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he’s drawing camels with t humps, representing them as polylines in the plane. Each polyline consists of n ve…...

CSID-GAN:基于生成对抗网络的定制风格室内平面设计框架论文阅读
CSID-GAN: A Customized Style Interior Floor Plan Design Framework Based on Generative Adversarial Network 摘要前言II. CSID-GAN METHODA. Overall FrameworkB. Algorithm and Loss Function III. DATASETS AND EVALUATION METRICSA. DatasetsB. Evaluation Metrics IV.…...

02SQLite
文章目录 索引创建索引删除索引索引优点及缺点?避免使用索引 视图创建视图删除视图 事务事务控制命令通过事务方式对数据库进行访问优势: 索引 创建索引 索引(Index)是一种特殊查找表,数据库搜索引擎用来加速数据检索…...

学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
在线学籍管理平台系统 目录 基于SpringbootVUE的在线学籍管理平台系统设计与实现 一、前言 二、系统功能设计 三、系统实现 四、数据库设计 1、实体ER图 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取: 博主介绍:✌️大…...

JDBC编程
前言: 你是否见过用Java连接数据库的操作,对没错,今天我们要讲的就是这个“高级”操作,做好准备全程高速。 API: 什么是API?我喜欢先把它的全称说一下:Application Programming Interface。简…...

Python : 类变量、静态方法、类方法
文章目录 前言1 类变量(Java静态变量)2 Python中的静态方法(使用 @staticmethod 装饰器声明)3 类方法(使用 @classmethod 装饰器声明)4 静态方法和类方法的区别前言 学完Java过后,对python中有了一些疑惑。Java中有static修饰的静态变量和静态方法这两个很用用的知识点…...

大厂笔试现已经禁用本地IDE怎么看
如果我说本来面试做题这种事情就是反人类你相信吗? 这个罪恶的源头就是 Google,说是为了选择高素质的计算机编程水平的人才,然后把面试就变成了考试,最大的受益者当然是印度人了。 当把一个考察过程变成标准化的考试过程&#x…...

【PostgreSQL】入门篇——如何创建、删除和管理数据库及其用户,包括权限设置和角色管理
PostgreSQL 数据库及用户管理 1. 创建数据库 1.1 使用 SQL 命令创建数据库 在 PostgreSQL 中,可以使用 CREATE DATABASE 命令来创建数据库。以下是基本语法: CREATE DATABASE database_name;示例: CREATE DATABASE my_database;1.2 使用…...

网络安全:保护数字时代的堡垒
网络安全:保护数字时代的堡垒 引言: 在数字化时代,网络安全的重要性日益凸显。它不仅关系到个人隐私保护,还涉及国家安全和经济发展。随着技术的发展,网络安全的威胁也在不断进化,从个人设备到企业网络&am…...

【rCore OS 开源操作系统】Rust 字符串(可变字符串String与字符串切片str)
【rCore OS 开源操作系统】Rust 语法详解: Strings 前言 这次涉及到的题目相对来说比较有深度,涉及到 Rust 新手们容易困惑的点。 这一次在直接开始做题之前,先来学习下字符串相关的知识。 Rust 的字符串 Rust中“字符串”这个概念涉及多种类型&…...