JAVA - Math.random() 난수 처리 취약점 : SecureRandom()
올바르지 않은 난수 취약점
자바에서 랜덤 난수를 발생시킬때 보통 Math.random()을 많이 사용하여 작성하였는데, 해당 메소드의 사용은 예상가능한 난수를 사용하는것으로 시스템 보안에 약점을 유발한다고 합니다...😥
해당 메소드는 시드값을 설정 할 수 없고 사용하는 알고리즘이 밝혀지면 취약해질 수 있기때문이라고 합니다.
결론은 해당 메소드 대신 차용할 메소드가 필요한데, 제시된 방법으로 java.util.Random
또는 java.security.SecureRandom 클래스 사용을 추천합니다.
이런 문제로 기존에 작성된 부분을 SecureRandom으로 변경하였습니다.
https://docs.oracle.com/javase/8/docs/api/java/security/SecureRandom.html
SecureRandom (Java Platform SE 8 )
This class provides a cryptographically strong random number generator (RNG). A cryptographically strong random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Mo
docs.oracle.com
사용법👀
Math.random()과 SecureRandom 클래스의 각각 사용법 비교를 해보겠습니다.
간단한 int 데이터 변수 생성 방법
- Math.random()
int div = (int) Math.floor( Math.random() * 2 )
- SecureRandom
SecureRandom sr = new SecureRandom();
int div = sr.nextInt(2);
정수형 난수 데이터를 구할때 올림, 내림처리도 필요없고 타입이 지정된 난수를 호출하기에 캐스팅도 없습니다.
앞으로 랜덤값을 생성해주는 SecureRandom클래스 자주 사용해야겠습니다.