====== Java RSA ====== * [[http://arstechnica.com/security/2010/01/768-bit-rsa-cracked-1024-bit-safe-for-now/|2010년 현재 RSA는 1024이상이 완전하다]] ===== 기본 암호화 복호화 예제 ===== * [[http://kwon37xi.egloos.com/4427199|RSA 기반 웹페이지 암호화 로그인]] TODO http://blog.kangwoo.kr/47 http://www.java2s.com/Tutorial/Java/0490__Security/BasicRSAexample.htm ===== 암호화 복호화 데이터 길이 제한 ===== * [[http://stackoverflow.com/questions/10007147/getting-a-illegalblocksizeexception-data-must-not-be-longer-than-256-bytes-when|java - getting a IllegalBlockSizeException: Data must not be longer than 256 bytes when using rsa]] * RSA는 기본적으로 **(Key bit수 / 8) - 11** 바이트만을 암호화 복호화 할 수 있다. * 즉, 키가 1024bit라면 ''(1024 / 8) - 11''의 결과인 117 바이트만 암호화 복호화 가능하다. ===== RSA 키를 저장했다가 복구하기 ===== RSA 키(public/private)을 hex 문자열로 만들어 저장했다가, hex 문자열에서 다시 키 객체를 생성할 수 있다. 아래는 ''PublicKey'' 예이며, ''PrivateKey''는 ''RSAPrivateKeySpec''를 사용하면 된다. ==== Key를 hex 문자열로 ==== KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(1024); KeyPair keyPair = generator.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec publicSpec = keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class); String publicKeyModulus = publicSpec.getModulus().toString(16); String publicKeyExponent = publicSpec.getPublicExponent().toString(16); ==== hex 문자열에서 Key로 ==== BigInteger modulus = new BigInteger(publicKeyModuls, 16); BigInteger exponent = new BigInteger(publicKeyExponent, 16); RSAPublicKeySpec pubks = new RSAPublicKeySpec(modulus, exponent); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(pubks);