2015-09-07 67 views
1

你好,我一直在探索新的工具,爲我們提供了Visual Studio 2015是Xamarin,我已經在Android上實現了一個應用程序,但我目前正在將它翻譯成C#應用程序是平臺,我不知道如何執行加密,我發現下面的例子將堅持,但只有加密和解密,也包括已經在Java中實現的代碼,我希望有人可以支持我,因爲我得到很多錯誤。Xamarin在C#代碼中的加密和解密基於Android

loggedUser = null; 
    sharedPreferences = getSharedPreferences(AppConstants.PREFERENCES_FILE_NAME, MODE_PRIVATE); 
    if (sharedPreferences.getAll().isEmpty()) { 
     Intent loginActivityIntent = new Intent(MainActivity.this, LoginActivity.class); 
     startActivity(loginActivityIntent); 
     finishAffinity(); 
    } 

    String loggedUserUsername = sharedPreferences.getString(AppConstants.LOGIN_CREDENTIAL_USERNAME_KEY, ""); 
    if (loggedUserUsername.isEmpty()) { 
     Intent loginActivityIntent = new Intent(MainActivity.this, LoginActivity.class); 
     loginActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(loginActivityIntent); 
     finishAffinity(); 
    } else { 
     File filesDirectory = new File(getFilesDir().getPath()); 
     for (File file: filesDirectory.listFiles()) { 
      if (file.getName().contains(loggedUserUsername) && file.getName().contains(AppConstants.USER_INFO_FILE_SUFFIX)) { 
       String deviceId; 
       String deviceKey; 
       byte[] secretBytes; 
       byte[] ivBytes; 
       FileInputStream fileInputStream = null; 
       CipherInputStream cipherInputStream = null; 
       ObjectInputStream objectInputStream = null; 

       try { 
        deviceId = sharedPreferences.getString(AppConstants.LOGIN_CREDENTIAL_DEVICE_ID_KEY, "").replace("-", ""); 
        deviceKey = sharedPreferences.getString(AppConstants.LOGIN_CREDENTIAL_DEVICE_KEY_KEY, "").replace("-", ""); 
        secretBytes = deviceKey.substring(0, 16).getBytes(); 
        ivBytes = deviceId.substring(deviceId.length() - 16, deviceId.length()).getBytes(); 

        fileInputStream = openFileInput(file.getName()); 
        final SecretKey secretKey = new SecretKeySpec(secretBytes, "AES"); 
        final IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); 
        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); 
        cipherInputStream = new CipherInputStream(fileInputStream, cipher); 
        objectInputStream = new ObjectInputStream(cipherInputStream); 
        loggedUser = new User((String)objectInputStream.readObject()); 
       } catch (ClassNotFoundException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IOException | JSONException e) { 
        if (AppConstants.DEBUG) Log.e(TAG, AppConstants.EXCEPTION_CAUGHT_MESSAGE + e.getMessage(), e); 
        loggedUser = null; 
       } finally { 
        if (objectInputStream != null) { 
         try { 
          objectInputStream.close(); 
         } catch (IOException e) { 
          if (AppConstants.DEBUG) Log.e(TAG, AppConstants.EXCEPTION_CAUGHT_MESSAGE + e.getMessage(), e); 
         } 
        } else { 
         if (AppConstants.DEBUG) Log.e(TAG, "objectOutputStream:null"); 
        } 

        if (cipherInputStream != null) { 
         try { 
          cipherInputStream.close(); 
         } catch (IOException e) { 
          if (AppConstants.DEBUG) Log.e(TAG, AppConstants.EXCEPTION_CAUGHT_MESSAGE + e.getMessage(), e); 
         } 
        } else { 
         if (AppConstants.DEBUG) Log.e(TAG, "cipherOutputStream:null"); 
        } 

        if (fileInputStream != null) { 
         try { 
          fileInputStream.close(); 
         } catch (IOException e) { 
          if (AppConstants.DEBUG) Log.e(TAG, AppConstants.EXCEPTION_CAUGHT_MESSAGE + e.getMessage(), e); 
         } 
        } else { 
         if (AppConstants.DEBUG) Log.e(TAG, "fileOutputStream:null"); 
        } 
       } 
       break; 
      } 
     } 
    } 

下面的代碼C#

 base.OnCreate(savedInstanceState); 
     loggedUser = null; 
     sharedPreferences = GetSharedPreferences(AppConstants.PREFERENCES_FILE_NAME, MODE_PRIVATE); 
     if (sharedPreferences.All.Count == 0) 
     { 
      Intent loginActivityIntent = new Intent(MainActivity.this, LoginActivity.Class); 
      loginActivityIntent.SetFlags(ActivityFlags.ClearTop); 
      StartActivity(loginActivityIntent); 
      FinishAffinity(); 
     } 
     else 
     { 
      Java.IO.File filesDirectory = new Java.IO.File(FilesDir.Path); 
      foreach (Java.IO.File file in filesDirectory.ListFiles()) 
      { 
       if (file.Name.Contains(loggedUser) && file.Name.Contains(AppConstants.USER_INFO_FILE_SUFFIX)) 
       { 
        string deviceId; 
        string deviceKey; 
        byte[] secretBytes; 
        byte[] ivBytes; 
        FileInputStream fileInputStream = null; 
        CipherInputStream cipherInputStream = null; 
        ObjectInputStream objectInputStream = null; 

        try 
        { 
         deviceId = sharedPreferences.GetString(AppConstants.LOGIN_CREDENTIAL_DEVICE_ID_KEY, "").Replace("-",""); 
         deviceId = sharedPreferences.GetString(AppConstants.LOGIN_CREDENTIAL_DEVICE_KEY_KEY, "").Replace("-", ""); 
         secretBytes = System.Text.Encoding.UTF8.GetBytes(deviceId.Substring(0, 16)); 
         ivBytes = System.Text.Encoding.UTF8.GetBytes(deviceId.Substring(deviceId.Length - 16, deviceId.Length)); 
         Stream fileStream = OpenFileInput(file.Name); 
         SecretKeySpec secretKeySpec = new SecretKeySpec(secretBytes, "AES"); 
         IvParameterSpec ivSpec = new IvParameterSpec(secretBytes); 
         Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding"); 
         cipher.Init(CipherMode.DecryptMode, secretKeySpec, ivSpec); 
         cipherInputStream = new CipherInputStream(fileStream,cipher); 
         objectInputStream = new ObjectInputStream(cipherInputStream); <- In this parte i have a error because not is possible to convert CipherInputStream to Stream 

這是與例子

Encryption and Decryption Support in .NET and Android

回答