spring practice_0614

Choi Suyeon·2024년 6월 14일

암호화

평문 -> 암호문으로 변환
spring security에서는 암호화에 관련된 클래스와 인터페이스 제공.

  • 사용
- spring security core 의존성주입 ( 단방향 hash사용 )
   <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>6.2.4</version>
</dependency>

- spring security crypto  ( 복호화가 가능한 암호문 사용 )
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-crypto -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-crypto</artifactId>
    <version>6.2.1</version>
</dependency>

일방향 해시

비밀번호, 주민번호 암호화할 때 사용.
BCrypt암호화 알고리즘 사용.(강력한 비밀번호 암호화를 위해 고안된 알고리즘)
(같은 비밀번호일지라도 다른 결과가 만들어진다. => equals로 비교안됨)
salt : 암호화를 수행할 때 입력하는 값으로 입력되는 salt에 의해 다른 결과 생성된다.

  • 사용법
    org.springframework.security.crypto.password.PasswordEncoder 인터페이스
    org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder 클래스를 사용
1. 생성
PasswordEncoder pe = new BCryptPasswordEncoder();

2. 암호문 생성
String pass="비밀번호";
String encodePass = pe.encode(pass);

3. 비교(equals로 비교할 수 없다.)
boolean flag = pe.matches(입력된 비밀번호, 인코드된 비번);
ex) 암호화된비번 $2a$10$EVa7h3KEdxosqlfi3NL.w.RKKbd8gn55ZUTkPSCpYANRihpertEMu

복호화

  1. 암호화 클래스 생성(암호화 비번, salt)
    AES - 대칭키 암호화 알고리즘으로 암호문을 생성.
    String key="암호화키";
    String salt="";//salt에 따라 다른 형태의 암호문 생성(암호화 강도가 달라진다.)
    TextEncryptor te = Encryptors.text(key, salt);
    // 암호가 틀리면 : IllegalStateException
    // salt 틀리면 : IllegalArgumentException 발생

  2. 평문 -> 암호문(암호화)
    String plainText = "test@test.com";
    String cipherText = te.encrypt(plainText);

  3. 암호문 -> 평문(복호화)
    String email = te.decrypt(cipher Text);


Spring DI

약 결합( Loosely coupled )를 손쉽게 사용
강 결합( Strong coupled )

0개의 댓글