Created
June 8, 2017 01:32
-
-
Save devetude/ebe9a17af92b74754306047a88013e5c to your computer and use it in GitHub Desktop.
RSA 알고리즘
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* RSA 알고리즘 | |
* | |
* @author devetude | |
*/ | |
public class Main { | |
// 암호화 할 데이터 상수 | |
private static final int RAW_DATA = 11109316; | |
// RSA 상수 | |
private static final int P = 11801; | |
private static final int Q = 11821; | |
private static final int N = P * Q; | |
private static final int GN = (P - 1) * (Q - 1); | |
private static final int E = 17; | |
// D 변수 | |
private static int D; | |
public static void main(String args[]) throws Exception { | |
// 버퍼를 통해 결과 값을 만듬 | |
StringBuilder sb = new StringBuilder(); | |
sb.append("P : ").append(P).append("\n"); | |
sb.append("Q : ").append(Q).append("\n"); | |
sb.append("N : ").append(N).append("\n"); | |
sb.append("GN : ").append(GN).append("\n"); | |
sb.append("E : ").append(E).append("\n"); | |
// D 값을 찾음 | |
for (int i = 1; i <= Integer.MAX_VALUE; i++) { | |
if ((i * E) % GN == 1) { | |
D = i; | |
break; | |
} | |
} | |
sb.append("D : ").append(D).append("\n"); | |
// D 값으로 데이터 암호화 | |
long encData = RAW_DATA; | |
sb.append("Raw Data : ").append(RAW_DATA).append("\n"); | |
for (int i = 2; i <= D; i++) { | |
encData = encData * RAW_DATA % N; | |
} | |
sb.append("Enc Data : ").append(encData).append("\n"); | |
// E 값으로 데이터 복호화 | |
long rawData = encData; | |
for (int i = 2; i <= E; i++) { | |
rawData = rawData * encData % N; | |
} | |
sb.append("Raw Data : ").append(rawData); | |
// 결과 값 한꺼번에 출력 | |
System.out.print(sb.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment