Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try{
- KeyPairGenerator clsKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
- clsKeyPairGenerator.initialize(2048);
- KeyPair clsKeyPair = clsKeyPairGenerator.genKeyPair();
- Key clsPublicKey = clsKeyPair.getPublic();
- Key clsPrivateKey = clsKeyPair.getPrivate();
- KeyFactory fact = KeyFactory.getInstance("RSA");
- RSAPublicKeySpec clsPublicKeySpec = fact.getKeySpec( clsPublicKey, RSAPublicKeySpec.class);
- RSAPrivateKeySpec clsPrivateKeySpec = fact.getKeySpec( clsPrivateKey, RSAPrivateKeySpec.class);
- System.out.println( "public key modulus(" + clsPublicKeySpec.getModulus( ) + ") exponent(" + clsPublicKeySpec.getPublicExponent( ) + ")" );
- System.out.println( "private key modulus(" + clsPrivateKeySpec.getModulus( ) + ") exponent(" + clsPrivateKeySpec.getPrivateExponent( ) + ")" );
- BufferedReader in = new BufferedReader ( new InputStreamReader(System.in));
- System.out.print("\n\n암호화할 문자열 입력 : ");
- // 암호화 한다.
- String strPinNumber = in.readLine();
- Cipher clsCipher = Cipher.getInstance("RSA");
- clsCipher.init( Cipher.ENCRYPT_MODE, clsPublicKey );
- byte[] arrCipherData = clsCipher.doFinal( strPinNumber.getBytes( ) );
- String strCipher = new String( arrCipherData );
- System.out.println( "cipher : " + strCipher );
- // 복호화 한다.
- clsCipher.init( Cipher.DECRYPT_MODE, clsPrivateKey );
- byte[] arrData = clsCipher.doFinal( arrCipherData );
- String strResult = new String( arrData );
- System.out.println( "result : " + strResult);
- }catch( Exception e )
- {
- System.out.println("!! EXCEPTION !!");
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement