ZHI.ZSystem开发组件是ZHI框架下的一款.NET开发组件。其针对.NET System库内部对象实现了大量的扩展方法,同时还集成了超级多的帮助类,以便于我们日常编程开发。最重要的是它基于.NET Standard 2.0目标框架编写,.NET Core 与.NET Framework编码工程师们都可以使用,不用根据版本下载,是不是超赞!
今天要介绍的帮助类就是用于AES加密解密用的EncryptHelper。接下来我们用一段代码来展示它的用法:
//把如下代码贴进单元测试方法后,“运行测试”就可查看加密解密结果啦! //AES KEY 128 BIT var aes_key = "afbbc5713904a815"; //AES IV 16 BYTE var aes_iv = "9420f687309c817c"; // plaintext var plaintext = "我们来进行单元测试咯!.}|/=@#¥%……&*()——+~··"; //BASE64 输出 var ciphertext_base_64_output = EncryptHelper.AESEncryptToBase64(plaintext, aes_key, aes_iv, AesCipherMode.CBC, AesPaddingMode.PKCS7Padding); //Hex 输出(十六进制输出) var ciphertext_hex_output = EncryptHelper.AESEncryptToHex(plaintext, aes_key, aes_iv, AesCipherMode.CBC, AesPaddingMode.PKCS7Padding); //AES 解密 BASE64 var base_64_decrypt = EncryptHelper.AESDecryptFromBase64(ciphertext_base_64_output, aes_key, aes_iv, AesCipherMode.CBC, AesPaddingMode.PKCS7Padding); //AES 解密 Hex var hex_decrypt = EncryptHelper.AESDecryptFromHex(ciphertext_hex_output, aes_key, aes_iv, AesCipherMode.CBC, AesPaddingMode.PKCS7Padding); Console.WriteLine("CBC密码模式"); Console.WriteLine(" 加密结果base64输出:{0}", ciphertext_base_64_output); Console.WriteLine(" 加密结果hex输出:{0}", ciphertext_hex_output); Console.WriteLine(" 解密base64:{0}", base_64_decrypt); Console.WriteLine(" 解密hex:{0}", hex_decrypt); Console.WriteLine(); //BASE64 输出 ciphertext_base_64_output = EncryptHelper.AESEncryptToBase64(plaintext, aes_key, aes_iv, AesCipherMode.CFB, AesPaddingMode.PKCS7Padding); //Hex 输出(十六进制输出) ciphertext_hex_output = EncryptHelper.AESEncryptToHex(plaintext, aes_key, aes_iv, AesCipherMode.CFB, AesPaddingMode.PKCS7Padding); //AES 解密 BASE64 base_64_decrypt = EncryptHelper.AESDecryptFromBase64(ciphertext_base_64_output, aes_key, aes_iv, AesCipherMode.CFB, AesPaddingMode.PKCS7Padding); //AES 解密 Hex hex_decrypt = EncryptHelper.AESDecryptFromHex(ciphertext_hex_output, aes_key, aes_iv, AesCipherMode.CFB, AesPaddingMode.PKCS7Padding); Console.WriteLine("CFB密码模式"); Console.WriteLine(" 加密结果base64输出:{0}", ciphertext_base_64_output); Console.WriteLine(" 加密结果hex输出:{0}", ciphertext_hex_output); Console.WriteLine(" 解密base64:{0}", base_64_decrypt); Console.WriteLine(" 解密hex:{0}", hex_decrypt); Console.WriteLine();
贴出单元测试的输出结果:
CBC密码模式 加密结果base64输出:/4Yd0+cc37pCuUPe0x8tFM5BVCirh4BMtfMecFyyxAsOGm3E+F8IPBmUwaw4n2/MJa1Kmc1ZzcSBUcOo7MdUcN0UEd/Quz9Xzi6wl2rppZU= 加密结果hex输出:ff861dd3e71cdfba42b943ded31f2d14ce415428ab87804cb5f31e705cb2c40b0e1a6dc4f85f083c1994c1ac389f6fcc25ad4a99cd59cdc48151c3a8ecc75470dd1411dfd0bb3f57ce2eb0976ae9a595 解密base64:我们来进行单元测试咯!.}|/=@#¥%……&*()——+~·· 解密hex:我们来进行单元测试咯!.}|/=@#¥%……&*()——+~·· CFB密码模式 加密结果base64输出:zoSHX1GgvlFp03pLMXgzHw+f6SDV4c7z3XJyEtD8EdnlEkmem1zCd+7Yt06kVBQAVEOEhyLHlDFbHInDOCZjf+lWRiph7aS3oo63oTvpvMQ= 加密结果hex输出:ce84875f51a0be5169d37a4b3178331f0f9fe920d5e1cef3dd727212d0fc11d9e512499e9b5cc277eed8b74ea45414005443848722c794315b1c89c33826637fe956462a61eda4b7a28eb7a13be9bcc4 解密base64:我们来进行单元测试咯!.}|/=@#¥%……&*()——+~·· 解密hex:我们来进行单元测试咯!.}|/=@#¥%……&*()——+~··
刚才的案例代码中,向大家展示了AES的CFB密码模式加密。相比实例化.NET内置的AES加密对象,这种静态方法调用的方式,是不是方便许多啦!
今天的介绍就到此为止啦,如有不懂的话还可以加QQ 技术群:735837718(500人上限),之后会向大家介绍更多关于ZHI.ZSystem组件库的用法,谢谢浏览~