【问题标题】:Xamarin encrypting and decrypting in C # code Based on Android基于Android的C#代码中Xamarin加解密
【发布时间】:2015-09-07 21:43:36
【问题描述】:

您好,我一直在探索为我们提供 Visual Studio 2015 的新工具,即 Xamarin,我已经在 Android 上实现了一个应用程序,但我目前正在将它翻译成 C#,因为我的应用程序是平台,我不知道如何执行加密,我发现下面的例子你会坚持,但只是加密和解密,还附上已经用Java实现的代码,希望有人能支持我,因为我遇到了很多错误。

    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

【问题讨论】:

    标签: c# android encryption xamarin


    【解决方案1】:

    您可以使用像PCL Crypto 这样的第三方库。它也可以作为NuGet package.

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 1970-01-01
      • 2015-04-21
      • 2012-11-22
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 2014-04-20
      相关资源
      最近更新 更多