Google Code Prettify

2022年4月19日 星期二

Spring Security: AES 加解密

AES (Advanced Encryption Standard) 加密是美國聯邦政府採用的對稱式加密標準,用來取代原先使用多年的 DES (Data Encryption Standard) 加密。Spring Security 提供一個命名為 Encryptors 的類別,可以進行 AES 加解密,說明如下。

  • KeyGenerators

AES 加密需要 key 也需要 salt,這個工廠類別是密鑰產生器,產生出來的字串要當 key 或 salt 都可以,一般來說,key 會是人類取名,在必要時要求使用者需入,salt 則是系統產生,這個類別通常用來產生 salt。

如上圖,可以知道這個類別可以產生 BytesKeyGenerator 和 StringKeyGenerator 兩種密鑰產生器,BytesKeyGenerator 會產生 byte[] 的密鑰,StringKeyGenerator 則是 byte[] 的 hex 字串。

secureRandom 不帶參數時,產生的密鑰長度是 8 bytes,要指定長度就帶參數。

程式寫法如下:
  1. String salt = KeyGenerators.string().generateKey();
  • Encryptors
產生加密類別的工廠類別,宣告如下:

這個工廠類別會產生兩種加密類別 -- BytesEncryptorTextEncryptor,這兩個類別都只有兩個 method - encrypt、descrypt,BytesEncryptor 的 method 傳入 byte[] 傳回 byte[],TextEncryptor 傳入 hex 值的字串,傳回 hex 值的字串。
  1. @ExtendWith(SpringExtension.class)
  2. @SpringBootTest
  3. @Slf4j
  4. class EncryptorsTest {
  5. private String salt = StringUtil.EMPTY;
  6. private String password = StringUtil.EMPTY;
  7.  
  8. @BeforeEach
  9. void setUp() throws Exception {
  10. salt = KeyGenerators.string().generateKey();
  11. log.info("salt = " + salt);
  12. password = "P@ssw0rd";
  13. }
  14.  
  15. @AfterEach
  16. void tearDown() throws Exception {
  17. }
  18.  
  19. @Test
  20. void testBytesEncryptor() {
  21. BytesEncryptor encryptor = Encryptors.standard(password, salt);
  22. byte[] text = "習近平快GG了!".getBytes();
  23. byte[] encrypted = encryptor.encrypt(text);
  24. log.info(NumberUtil.toHex(encrypted));
  25. byte[] decrypted = encryptor.decrypt(encrypted);
  26. log.info(new String(decrypted));
  27. }
  28. @Test
  29. void testTextEncryptor() {
  30. TextEncryptor encryptor = Encryptors.text(password, salt);
  31. String encrypted = encryptor.encrypt("習近平快GG了!");
  32. log.info(encrypted);
  33. String decrypted = encryptor.decrypt(encrypted);
  34. log.info(decrypted);
  35. }
  36. @Test
  37. void testTextEncryptorAndBytesEncryptor() throws Exception {
  38. TextEncryptor textEncryptor = Encryptors.text(password, salt);
  39. String encrypted = textEncryptor.encrypt("習近平快GG了!");
  40. log.info(encrypted);
  41. BytesEncryptor bytesEncryptor = Encryptors.standard(password, salt);
  42. byte[] b = NumberUtil.hexToByte(encrypted);
  43. byte[] decryptedBytes = bytesEncryptor.decrypt(b);
  44. log.info(new String(decryptedBytes));
  45. }
  46. }
如上,在 setUp()中產生 salt 及設定 password。testBytesEncryptor() 是測試 BytesEncryptor 的加解密,testTextEncryptor() 是測試 TextEncryptor 的加解密,testTextEncryptorAndBytesEncryptor() 則是驗證 TextEncryptor 產生的加密字串確實只是 hex 表示的字串。

沒有留言:

張貼留言