2016-06-07 28 views
0

我試圖將JAVA代碼轉換爲Objective - C語言。我有一個要求,我應該使用Android開發人員使用的相同代碼。代碼如下嗨,我想使用JAVA代碼做AES加密幫助我轉換?

import java.io.UnsupportedEncodingException; 
import java.security.InvalidAlgorithmParameterException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.Map; 

import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 

import org.apache.commons.codec.binary.Base64; 



public class EncDec { 

    public static void main(String args[]) 
    { 


     String reqMessage="{\"accountType\":\"ALL\",\"uId\":\"c8ff46be-a083-4009-8a33-fc2d22cc40e3|123456784\",\"deviceId\":\"qvxy1234\"}"; 
     Map requestMap=new HashMap(); 
     requestMap.put("body", reqMessage); 
     String bodyString=(String) requestMap.get("body"); 
     String authKey="M/98hZivBqJQftMHsPvMgg&&"; 

     String encString= encode(authKey,bodyString); 
     System.out.println("encString ::: "+ encString); 

     String decString= decode(authKey,encString); 
     System.out.println("decString ::: "+ decString); 



    } 
    public static String encode(String keyString, String stringToEncode) throws NullPointerException { 
     if (keyString.length() == 0 || keyString == null) { 
      throw new NullPointerException("Please give Password"); 
     } 

     if (stringToEncode.length() == 0 || stringToEncode == null) { 
      throw new NullPointerException("Please give text"); 
     } 

     try { 
      SecretKeySpec skeySpec = getKey(keyString); 
      byte[] clearText = stringToEncode.getBytes("UTF8"); 

      // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID 
      final byte[] iv = new byte[16]; 
      Arrays.fill(iv, (byte) 0x00); 
      IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); 

      // Cipher is not thread safe 
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); 
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); 
      byte[] encryptedByte=cipher.doFinal(clearText); 
      String encrypedValue = new String(Base64.encodeBase64(encryptedByte)); 
      System.out.println("Encrypted: " + stringToEncode + " -> " + encrypedValue); 
      return encrypedValue; 

     } catch (InvalidKeyException e) { 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (BadPaddingException e) { 
      e.printStackTrace(); 
     } catch (NoSuchPaddingException e) { 
      e.printStackTrace(); 
     } catch (IllegalBlockSizeException e) { 
      e.printStackTrace(); 
     } catch (InvalidAlgorithmParameterException e) { 
      e.printStackTrace(); 
     } 
     return ""; 
    } 

    /** 
    * Decodes a String using AES-128 and Base64 
    * 
    * @param context 
    * @param password 
    * @param text 
    * @return desoded String 
    */ 
    public static String decode(String password, String text) throws NullPointerException { 

     if (password.length() == 0 || password == null) { 
      throw new NullPointerException("Please give Password"); 
     } 

     if (text.length() == 0 || text == null) { 
      throw new NullPointerException("Please give text"); 
     } 

     try { 
      SecretKey key = getKey(password); 

      // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID 
      final byte[] iv = new byte[16]; 
      Arrays.fill(iv, (byte) 0x00); 
      IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); 

      byte[] encrypedPwdBytes = Base64.decodeBase64(text.getBytes()); 
      // cipher is not thread safe 
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); 
      cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec); 
      byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes)); 

      String decrypedValue = new String(decrypedValueBytes); 
      System.out.println("Decrypted: " + text + " -> " + decrypedValue); 
      return decrypedValue; 

     } catch (InvalidKeyException e) { 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (BadPaddingException e) { 
      e.printStackTrace(); 
     } catch (NoSuchPaddingException e) { 
      e.printStackTrace(); 
     } catch (IllegalBlockSizeException e) { 
      e.printStackTrace(); 
     } catch (InvalidAlgorithmParameterException e) { 
      e.printStackTrace(); 
     } 
     return ""; 
    } 

    /** 
    * Generates a SecretKeySpec for given password 
    * 
    * @param password 
    * @return SecretKeySpec 
    * @throws UnsupportedEncodingException 
    */ 
    private static SecretKeySpec getKey(String password) throws UnsupportedEncodingException { 

     // You can change it to 256 if you wish 
     int keyLength = 128; 
     byte[] keyBytes = new byte[keyLength/8]; 
     // explicitly fill with zeros 
     Arrays.fill(keyBytes, (byte) 0x0); 

     // if password is shorter then key length, it will be zero-padded 
     // to key length 
     byte[] passwordBytes = password.getBytes("UTF-8"); 
     int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length; 
     System.arraycopy(passwordBytes, 0, keyBytes, 0, length); 
     SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); 
     return key; 
    } 


} 

所以我想這將被轉換爲客觀C.我不知道如何做到這一點。幫助我在這!

我在JAVA中搜索了一些代碼而且我嘗試過這樣做。但問題是它會給出一些其他解密的數據,但不是給出使用此代碼的確切數據。所以如果我轉換同樣的東西,我可能會得到我想要的確切代碼。 人們可能會在這裏瞭解JAVA以及Objective c。這些人可以幫助我,我猜。

+0

這段Java代碼是使用Javan.crypto包,這樣你就可以」因爲在iOS上沒有Java包,所以將它精確地轉換爲objective-c。您必須針對iOS上的安全框架進行編碼。 – Paulw11

回答

0

我不確定,但你可以試試:)。

對於編碼階段:

IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); 
// Cipher is not thread safe 
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); 
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); 
byte[] encryptedByte=cipher.doFinal(clearText); 
String encrypedValue = new String(Base64.encodeBase64(encryptedByte)); 

中的OBJ-C使用CCCrypt:

CCCryptorStatus CCCrypt(
CCOperation op,   //is kCCEncrypt in your case 
CCAlgorithm alg,  //is kCCAlgorithmAES128 
CCOptions options,  //is kCCModeCBC 
const void *key,  //may be skeySpec 
size_t keyLength, 
const void *iv,   // is your iv key : ivParameterSpec 
const void *dataIn,  
size_t dataInLength, 
void *dataOut,   /* data RETURNED here */ 
size_t dataOutAvailable, 
size_t *dataOutMoved) 
__OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0); 

請參考:CCCrypt decrypting in AES CBC works even without IV 瞭解更多信息,希望這有助於你。