본문 바로가기
728x90

백엔드/Java80

AES-256 실제로 프로젝트에서 사용하기 위해 만들었던 코드로 org.apache.commons.codec 라이브러리에 의존성을 가지고 있다. AES256Util 객체를 생성할때에는 암호화/복호화에 사용될 키를 입력받는다. 스프링 빈컨테이너에 키를 넣어 생성해서 사용하기 위해서 만들었으나 쉽게 생성자를 수정하고 맴버변수를 등록하도록 수정하여 사용할 수도 있다, 단 키의 길이가 16자리 이하일 경우 오류가 발생한다. 개발전 준비단계에 언급한 local_policy.jar 파일과 US_export_policy.jar 추가 다운로드한 라이브러리가 없을 경우에는 16자리의 키를 입력하더라고 java.security.InvalidKeyException: Illegal key size이 발생한다. import java.io.U.. 2024. 3. 20.
SHA-256 복호화가 없으므로 별도의 키가 없습니다. 또 키가 없으므로 SHA256 알고리즘을 돌려서는 항상 같은 결과가 떨어집니다. 1과 같은 단순한 값을 sha256으로 암호화했을 경우 항상 같은 값이 나오기 때문에 비밀번호 입력을 적당한 길이와 복잡성을 가지도록 유도하여 패스워드를 유추하는데 어렵도록 해야 합니다. import java.security.MessageDigest; public class Sha256 { public static String encrypt(String planText) { try{ MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(planText.getBytes()); byte byteData[] = md.dige.. 2024. 3. 20.
알고리즘 // 알파벳으로 시작하고 _ 숫자 영문자만 허용하는 8-30 길이의 단어 String pattern = "^[a-zA-Z][a-zA-Z_0-9]{7,29}$"; String input = ""; Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(input); if (m.find()) { System.out.println("Valid"); } else { System.out.println("Invalid"); } ------ // 파일확장자 String pattern = "^\\S+.(?i)(txt|pdf|hwp|xls)$"; String input = "abc.txt"; boolean i = Pattern.matches(pattern, input).. 2024. 3. 20.
같은 단어 제거 정규식 String regex = "(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Scanner in = new Scanner(System.in); int numSentences = Integer.parseInt(in.nextLine()); while (numSentences-- > 0) { String input = in.nextLine(); Matcher m = p.matcher(input); while (m.find()) { input = input.replaceAll(m.group(0),m.group(1)); } System.out.println(input); } in.clos.. 2024. 3. 20.
자바 정수범위 // - 2^7 ~ 2^7-1; byte a = Byte.MAX_VALUE; byte b = Byte.MAX_VALUE; // - 2^15 ~ 2^15-1 short s1 = Short.MIN_VALUE; short s2 = Short.MAX_VALUE; // - 2^31 ~ 2^31-1 int i1 = Integer.MIN_VALUE; int i2 = Integer.MAX_VALUE; // - 2^63 ~ 2^63-1 long h1 = Long.MIN_VALUE; long h2 = Long.MAX_VALUE; byte aa = (byte) (new BigInteger("2").pow(7).longValue()); //-128 - as expected short bb = (short) (new BigIn.. 2024. 3. 20.
728x90