Advertisement
Guest User

Untitled

a guest
Nov 15th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3.  
  4.  
  5. try{
  6.  
  7. KeyPairGenerator clsKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
  8. clsKeyPairGenerator.initialize(2048);
  9.  
  10. KeyPair clsKeyPair = clsKeyPairGenerator.genKeyPair();
  11. Key clsPublicKey = clsKeyPair.getPublic();
  12. Key clsPrivateKey = clsKeyPair.getPrivate();
  13. KeyFactory fact = KeyFactory.getInstance("RSA");
  14. RSAPublicKeySpec clsPublicKeySpec = fact.getKeySpec( clsPublicKey, RSAPublicKeySpec.class);
  15. RSAPrivateKeySpec clsPrivateKeySpec = fact.getKeySpec( clsPrivateKey, RSAPrivateKeySpec.class);
  16. System.out.println( "public key modulus(" + clsPublicKeySpec.getModulus( ) + ") exponent(" + clsPublicKeySpec.getPublicExponent( ) + ")" );
  17. System.out.println( "private key modulus(" + clsPrivateKeySpec.getModulus( ) + ") exponent(" + clsPrivateKeySpec.getPrivateExponent( ) + ")" );
  18.  
  19.  
  20.  
  21.  
  22.  
  23. BufferedReader in = new BufferedReader ( new InputStreamReader(System.in));
  24.  
  25. System.out.print("\n\n암호화할 문자열 입력 : ");
  26.  
  27.  
  28. // 암호화 한다.
  29. String strPinNumber = in.readLine();
  30.  
  31. Cipher clsCipher = Cipher.getInstance("RSA");
  32. clsCipher.init( Cipher.ENCRYPT_MODE, clsPublicKey );
  33. byte[] arrCipherData = clsCipher.doFinal( strPinNumber.getBytes( ) );
  34. String strCipher = new String( arrCipherData );
  35. System.out.println( "cipher : " + strCipher );
  36. // 복호화 한다.
  37. clsCipher.init( Cipher.DECRYPT_MODE, clsPrivateKey );
  38. byte[] arrData = clsCipher.doFinal( arrCipherData );
  39.  
  40. String strResult = new String( arrData );
  41. System.out.println( "result : " + strResult);
  42.  
  43.  
  44.  
  45.  
  46. }catch( Exception e )
  47. {
  48. System.out.println("!! EXCEPTION !!");
  49. e.printStackTrace();
  50. }
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement