2016-09-28 21 views
1

私は自分のアプリケーションのUIをMeteorに組み込み、REST API(Spring CXF)からデータを取得して送信します。 Meteorでデータを暗号化し、REST APIコードで復号化したいと思います。私は暗号化と解読にAESを使用しています。流星で私は暗号化のためにhttps://atmospherejs.com/jparker/crypto-aesパッケージを使用しています。私は暗号化キーが流星によって送信する解読のためにjavaの下のコードを書いた。私は、コードを実行するとMeteorとJavaによるデータの暗号化と復号化

public class AESTest { 
      private static String AESStr = "<Encrypted KEY>"; 
      public static void main(String[] args) throws Exception { 
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 
       System.out.println(decrypt(AESStr, "Test")); 
      } 
     public static String decrypt(String responseStr, String passPhrase) throws  GeneralSecurityException { 
      String decryptedStr = ""; 
      try { 
         Cipher cipher = getCipher(Cipher.DECRYPT_MODE, passPhrase); 
         byte[] decoded = Base64.decodeBase64(responseStr.getBytes()); 
         byte[] decryptedWithKey = cipher.doFinal(decoded); 
         byte[] decrypted = Arrays.copyOfRange(decryptedWithKey, 16, decryptedWithKey.length); 
         decryptedStr = new String(decrypted, "UTF-8"); 
       } catch (Exception e) { 
         e.printStackTrace(); 
       } 
       return decryptedStr; 
     } 

     private static Cipher getCipher(int mode, String passPhrase) throws Exception { 
       SecretKeySpec secretKeySpec = new SecretKeySpec(passPhrase.getBytes(), "AES"); 
       byte[] IV = new byte[16]; 
       new Random().nextBytes(IV); 
       AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV); 
       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); 
       cipher.init(mode, secretKeySpec, paramSpec); 
       return cipher; 
     } 
} 

私は誰が問題で私を導くことができる例外

javax.crypto.BadPaddingException: pad block corrupted 
    at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(Unknown Source) 
    at javax.crypto.Cipher.doFinal(Cipher.java:2165) 
    at com.tph.r3.EncodeTest.decrypt(EncodeTest.java:37) 
    at com.tph.r3.EncodeTest.main(EncodeTest.java:26) 

の下に取得していますか?

+0

は暗号AES packgeとして解読作業をしていますか?私はCryptoJS.AES.decrypt()を使用することを意味します。 – Roshith

+0

はい、それは完璧に動作します –

答えて

0

解読ロジックw.r.t IVに問題があります。間違っている復号化暗号を初期化するためにIVをランダムに選択しています。通常、最初の16バイトを形成するresponseStrを暗号化するために使用されたのと同じIVを使用する必要があります。

現在のフォームでは、IVがランダムに選択されていて暗号化には使用されない暗号化にのみgetCipher()を使用できます。別の方法を書いたほうがいい。暗号解読のための

擬似コード:

decCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
SecretKeySpec keySpec = new SecretKeySpec(securityKey , "AES"); 

//IV + Cipher 
byte [] cipherWithIV = Base64.decodeBase64(responseStr.getBytes())); 

//Extract IV 
byte [] iv = new byte [16]; 
byte [] cipherWithoutIV = new byte [cipherWithIV.length - 16 ]; 

//First 16 bytes 
for(i < 16; i++) { 
    iv [i] = cipherWithIV [i]; 
} 

//Rest of the cipher ie 16 -> cipherWithIV.length 
for(i < cipherWithIV.length; i++) { 
    cipherWithoutIV [j] = cipherWithIV[i]; 
    j++; 
} 

// 
IvParameterSpec ivParamSpec = new IvParameterSpec(iv); 

// 
decCipher.init(Cipher.DECRYPT_MODE, keySpec, ivParamSpec); 

//Decrypt cipher without IV 
decText = decCipher.doFinal(cipherWithoutIV); 

//Convert to string 
decString = new String(decText,"UTF8");