1. 程式人生 > 其它 >乙級PAT真題1004(Java)

乙級PAT真題1004(Java)

ZHI.ZSystem是github上一款開源的幫助函式庫。。其針對.NET System庫內部物件實現了大量的擴充套件方法,同時還集成了超級多的幫助類,以便於我們日常程式設計開發。最重要的是它基於.NET Standard 2.0目標框架編寫,.NET Core 與.NET Framework編碼工程師們都可以使用,不用根據版本下載,是不是超讚!

GitHub地址:https://github.com/peashooters/zhi

Gitee地址:https://gitee.com/peashooters/zhi

官方文件:https://peashooters.gitee.io/zhi-doc

今天要介紹的幫助類就是用於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元件庫的用法,謝謝瀏覽~