2014-09-24 2 views
-2

누군가 도움이 필요합니다. 내 안드로이드 전화에서 공개 키 (파일 암호화 용)와 해독 할 개인 키를 만듭니다. 나는 그들을 2 파일에 저장한다. 안드로이드로 해독하면 괜찮습니다. 내가 해결 방법을 모르는개인 키를 사용하여 파일 암호 해독 (RSA)

at sun.security.rsa.RSAPadding.unpadV15(Unknown Source) 
    at sun.security.rsa.RSAPadding.unpad(Unknown Source) 
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356) 
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382) 
    at javax.crypto.Cipher.doFinal(Cipher.java:2087) 
    at com.molisy.decryptfile.Main.RSADecrypt(Main.java:193) 
    at com.molisy.decryptfile.Main$2.actionPerformed(Main.java:309) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$200(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

: 나는 개인 키 파일을 복사하고 내 PC (자바)에서 해독하는 데 사용할 때, 나는 예외를 얻을. 하지만 개인 키 파일의 값은 내 안드로이드와 내 PC에 인쇄 할 때 동일하지 않습니다. 내 안드로이드 애플 리케이션 : OpenSSLRSAPrivateKey{modulus=a9a141d6ef0050e27f00ec381f2fcfeb47781dcfb14f9b3eb378bd361b92b6da7feb8f2be7466314794d7543eb99c818d260d48b898c9995db3a3af76b23013ff935f77b8a89edcbd9d16a583b60591e55f7cb8271fa4cfae53fd759f3d8e1b522485e2cf29e89034223329322b4357c84fc848348b004136d6d360f8c9a70cb,privateExponent=7eaff2ee455da50b23f35a78a7c21bb50a9189223eb8c7a7527ed04182e2563265eb55e862384d73530d28916b7a54d944f610878e5935b39821ab3c720598be28d747de099ff8fac6558f235b983815efc61cbc574be39d97dc7ac57e6cf82161f4301dfe777c3c33c58d7c75f581de5cc0db83b079de7d79864a6189667171, 내 자바 애플 리케이션 : [email protected] 왜 그렇게 다릅니다. 및

+3

공개 키 모듈을 공공 장소에 게시하지 마십시오. – hexafraction

+1

@hexafraction보다 정확하게 말해, 공공 장소에 개인 지수를 게시하지 마십시오. 출력이 다른 이유는'OpenSSLRSAPrivateKey'가'Object.toString()'과'RSAPrivateKeyImpl'를 오버라이드 (override)하기 때문입니다. –

+0

키가 PC 및 Android에서 동일하지 않으면 이유를 알 수 있습니다. 왜 동일하지 않은지 알아보십시오. –

답변

0

여기 내 코드입니다 해결하는 방법 :

public void RSADecrypt(String inFileName, String outFileName) { 
     try { 
      /* Get the encrypted message from file. */ 
      FileInputStream cipherfile = new FileInputStream(inFileName); 

      byte[] ciphertext = new byte[cipherfile.available()]; 
      cipherfile.read(ciphertext); 
      cipherfile.close();   
      PrivateKey privatekey =readPrivateKeyFromFile("D:\\Private.key"); 

      /* Create cipher for decryption. */ 
      Cipher decrypt_cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
      decrypt_cipher.init(Cipher.DECRYPT_MODE, privatekey); 
      FileOutputStream plainfile = new FileOutputStream(outFileName); 
      int n = ciphertext.length/128; 
      System.out.println("len: " + n); 
      byte[] data1 = new byte[128]; 
      for (int i = 0; i < n; i++) { 
       for (int j = 0; j < 128; j++) { 
        data1[j] = ciphertext[128 * i + j]; 
       } 
       byte[] descryptedData = decrypt_cipher.doFinal(data1); 
       plainfile.write(descryptedData); 

      } 

      plainfile.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
public PrivateKey readPrivateKeyFromFile(String fileName) 
      throws IOException { 
     FileInputStream fis = null; 
     ObjectInputStream ois = null; 
     try { 
      fis = new FileInputStream(new File(fileName)); 
      ois = new ObjectInputStream(fis); 

      BigInteger modulus = (BigInteger) ois.readObject(); 
      BigInteger exponent = (BigInteger) ois.readObject(); 

      // Get Private Key 
      RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
        modulus, exponent); 
      KeyFactory fact = KeyFactory.getInstance("RSA"); 
      PrivateKey privateKey = fact.generatePrivate(rsaPrivateKeySpec); 
      System.out.println("get key ok: " + privateKey.toString()); 
      return privateKey; 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (ois != null) { 
       ois.close(); 
       if (fis != null) { 
        fis.close(); 
       } 
      } 
     } 
     return null; 
    } 

나는 128 바이트/시간의 암호를 해독. 데이터가 너무 길어서 해독 할 수 없기 때문입니다. 그것은 안드로이드에서 잘 작동합니다. readPrivateKeyFromFile 메소드는 개인 키의 다른 값을 반환합니다 (동일한 코드를 사용하더라도 안드로이드와 같지 않음).

-1

나는 많은 것을 시도하고 알아 낸다. moblie에서 암호화하는 경우 youn은 모바일에서만 해독 할 수 있고 PC에서도 동일하게 해독 할 수 있습니다. 다른 환경 때문에 그것이 사실입니까?

관련 문제