반응형
ByteUtil.java
해당 소스에 사용된 ByteUtil 클래스
TestRsa.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import java.security.InvalidKeyException; import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; public class TestRsa { public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { SecureRandom random = new SecureRandom(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048, random); KeyPair pair = generator.generateKeyPair(); Key publicKey = pair.getPublic(); System.out.println("publicKey = " + publicKey); Key privateKey = pair.getPrivate(); System.out.println("privateKey = " + privateKey); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey, random); String text = "RSA 암호화!!"; System.out.println("원본 : " + text); // String -> byte 변환 byte[] t0 = text.getBytes(); System.out.println("원본 바이트 : " + ByteUtil.toHexString(t0)); // 인코딩 byte[] b0 = cipher.doFinal(t0); System.out.println("인코딩 : " + ByteUtil.toHexString(b0)); // 디코딩 cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] b1 = cipher.doFinal(b0); System.out.println("디코딩 : " + ByteUtil.toHexString(b1)); System.out.println("디코딩된 원본:" + new String(b1)); } } | cs |
########### Console 결과 ###########
1 2 3 4 5 6 7 8 9 | publicKey = Sun RSA public key, 2048 bits modulus: 24375096591711126055210512185359957069059389332115666685656256009167572767173223522908531449043521360276825613201726594898353846423279278285747007266846094212573914607322409575412719234320181695009686453504284619499201124842375042929556510354483722116550582489225345515352818937465595047638810682064411880791025358297049816845633188104939146545275383717858564379062518328967892129218263037788613461088589973921632894302911326334363077605546489131720190553333874412384419644166222126318457219924457347665925613430947371080765753200018907246728766599325596504477911485731278280070273136297987513034643007577899945186837 public exponent: 65537 privateKey = sun.security.rsa.RSAPrivateCrtKeyImpl@ffc92d04 원본 : RSA 암호화!! 원본 바이트 : 52534120ec9594ed98b8ed99942121 인코딩 : 1556d68e09e782fd67d3d7413f535ca829526ba33ff7869d4c9a75d4ce5e0cf30333c390f01305041ad86fad071b6e5c935d47edbc2b4acd314f7adc54db6916dbe1bb48708d16adf0927aeb3f8b1a4461d15c34c74ce72dff5125ab66324db15630d9b59ca1bb2e205d656dff38461c282475035fed2095872971bb8f961cce2b2fa797b389d6a18704f2d8204510d819dcc2ccd05597fd5b04536d76698a15cf05f5800cbd86f5016916393b2fd5fbaa935b138f57ebff7825ae6812ebe41f1cc357fb33a62c55d23ad7d3ca3fb8ebeaa651f4842209b0e0128cda6520d1fe9e37af678e2a515eec9e79be6b480e2786c4fc42a23c4846a94d35ff105dd7c7 디코딩 : 52534120ec9594ed98b8ed99942121 디코딩된 원본:RSA 암호화!! | cs |
반응형
'IT > 언어' 카테고리의 다른 글
시큐어 코딩 가이드 (0) | 2014.05.29 |
---|---|
ByteUtil.java (0) | 2014.05.29 |
Servlet Encoding Filter (0) | 2014.05.21 |
정규 표현식 예제 (0) | 2014.05.21 |
Servlet 에러 페이지 처리 (0) | 2014.05.21 |