MD5(128bit) : 해시 암호화 코드(default/가장 많이 쓰는 형태) ex) 쇼핑몰 SHA-1(160bit) : MD5에서 보다 높은 bit를 활용하여 사용하는 방식 -내부시스템 SHA-2(128,224,256,384,512bit) : SHA-1에서 개발자가 직접 bit를 선정하도록 update -> AI 에 뚫림 SHA-3(256,384,512bit) : SHA2에서 특수문자 형태의 코드를 추가 RSA (2048bit) : 전자서명을 사용하는 형태로 암호화 (Linux에서 최초 개발) 금융관련 = md5 + RSA
● MD5
암호화 라이브러리 => try~catch 사용
try {
//암호화 라이브러리
MessageDigest md5 = MessageDigest.getInstance("MD5");
//암호화는 bit단위로 이루어지기 때문에 getBytes()로 변환
md5.update(mpass.getBytes());
byte[] m = md5.digest();
//System.out.println(m);
StringBuilder sb = new StringBuilder();
for(byte w : m) {
// % :문자 포맷 - %x(소문자+숫자) %X(대문자+숫자) : 16진수 형태의 구성
//문자 포맷(2진수) : %02x
//문자 포맷(8진수) : %08x
String word = String.format("%08x", w);
sb.append(word);
}
System.out.println(sb);
} catch (Exception e) {
e.printStackTrace();
//무조건 string으로 받아야함 (int,long등으로 못받음)
System.out.println("올바른 문자가 아니어서 암호화가 정상 작동되지 않습니다.");
}
}
}
① 의 출력 결과
① 받아온 데이터인 mpass 를 md5로 암호화 하기 위해 get.byte로 bit로 변환 후 md5형태로 암호화 함
② 변환된 'm' 값을 string.format을 이용해 문자포맷 %x 로 돌림
%X 로 돌림 ③ 진행 시 주르륵 나오므로 문자열로 받기 위해 StringBuilder 로 문자열로 변환 (2진수, 8진수 등으로도 변환 가능) 그냥 %x(16진수)로 돌려 문자열로 출력한 결과
e1adc3949ba59abbe56e057f2f883e(16진수) e10adc3949ba59abbe56e057f20f883e(2진수) 000000e10000000a000000dc0000003900000049000000ba00000059000000ab000000be00000056000000e000000057 000000f20000000f000000880000003e(8진수)
1. java로 ' dbcon ' 이라는 class 생성 후 라이브러리로 classes에 있는 properties 파일을 사용
코드 예시) database 연결 + properties 활용
public class dbcon {
//classes 에 있는 properties 파일을 사용하는 라이브러리
Properties pp = new Properties();
String resource = "db.properties"; //로드 할 properties 파일 명
public Connection db() throws Exception{
//classes 안에 있는 properties 를 로드하여 모든 문자 내용을 저장시킴
this.pp.load(this.getClass().getClassLoader().getResourceAsStream(resource));
Class.forName(this.pp.getProperty("driver"));
Connection con = DriverManager.getConnection(this.pp.getProperty("url"),this.pp.getProperty("user"),this.pp.getProperty("password"));
return con;
}
}
- properties 파일 안의 내용도 찍어볼 수 있음
System.out.println(this.pp.getProperty("user")); 이용 getProperty("properties파일 안의 내용")을 이용해서 다 로드 가능함 properties 파일에 sql(ddl) 구문 넣고 사용가능함 - ddl은 module 에 들어가야함 (controll에 넣으면 안됨)
코드) properties 파일의 user 값을 가져옴
public class dbcon {
//classes 에 있는 properties 파일을 사용하는 라이브러리
Properties pp = new Properties();
String resource = "db.properties"; //로드 할 properties 파일 명
public Connection db() throws Exception{
//classes 안에 있는 properties 를 로드하여 모든 문자 내용을 저장시킴
this.pp.load(this.getClass().getClassLoader().getResourceAsStream(resource));
System.out.println(this.pp.getProperty("user"));
Class.forName(null);
Connection con = DriverManager.getConnection(null);
return con;
}
}
하고 loginok2.do 생성 후 db connection 만 해주고 실행하면
@WebServlet("/loginok2.do")
public class login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
dbcon dc = new dbcon();
try {
Connection con = dc.db();
} catch (Exception e) {
System.out.println("DB 연결 실패!!");
}
}
}
properties 파일의 user 인 hana가 찍히는 것을 확인
● MVC형태로 문자를 md5 로 변환하는 Class 생성
사용자가 입력한 패스워드를 md5로 변환 후 return 하는 메소드를 생성
public class passwd_md {
private StringBuilder repass = new StringBuilder();
private String ori_pass = null;
private MessageDigest md5 = null;
public passwd_md(String userpw) { //pw를 받아오는 파트
this.ori_pass = userpw;
}
//사용자가 입력한 패스워드를 md5로 변환 후 return 하는 메소드
public String md5_passwd() {
try {
this.md5= MessageDigest.getInstance("md5");
this.md5.update(this.ori_pass.getBytes());
byte[] md = md5.digest();
for(byte ck : md) {
String m = String.format("%x", ck);
this.repass.append(m);
}
} catch (Exception e) {
System.out.println("앙호화 변환 오류");
}
//리턴할 메소드 자료형이 string으로 toString을 이용함
return this.repass.toString();
}
}
● properties 에 ddl 구문 넣고 사용시 주의 - properties 를 쓰면 preparedstatement만 사용가능함 다른건 사용하지 못함 (ddl구문안에 다른 변수 활용x)
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/cms
user = hana
password = hana1234
select_db = select * from login where mid=? order by midx desc