基于Android的Xamarin C#码加解密

基于Android的Xamarin C#码加解密,c#,android,encryption,xamarin,C#,Android,Encryption,Xamarin,您好,我一直在探索为我们提供Visual Studio 2015的新工具,即Xamarin,我已经在Android上实现了一个应用程序,但我目前正在将其翻译为C#因为我的应用程序是平台,我不知道如何执行加密,我发现下面的示例您会坚持,但只会加密和解密,同时附上已经用Java实现的代码,我希望有人能支持我,因为我会遇到很多错误 loggedUser = null; sharedPreferences = getSharedPreferences(AppConstants.PREFE

您好,我一直在探索为我们提供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;
SharedReferences=GetSharedReferences(AppConstants.PREFERENCES\u文件名,模式\u私有);
if(sharedReferences.All.Count==0)
{
Intent loginActivityIntent=新的Intent(MainActivity.this,LoginActivity.Class);
LoginActivityContent.SetFlags(ActivityFlags.ClearTop);
StartActivity(物流活动内容);
有限性();
}
其他的
{
Java.IO.File filesDirectory=新的Java.IO.File(FilesDir.Path);
foreach(filesDirectory.ListFiles()中的Java.IO.File文件)
{
if(file.Name.Contains(loggedUser)和&file.Name.Contains(AppConstants.USER\u INFO\u file\u后缀))
{
字符串设备ID;
字符串设备键;
字节[]secretBytes;
字节[]ivBytes;
FileInputStream FileInputStream=null;
CipherInputStream CipherInputStream=null;
ObjectInputStream ObjectInputStream=null;
尝试
{
deviceId=SharedReferences.GetString(AppConstants.LOGIN\u CREDENTIAL\u DEVICE\u ID\u KEY,“”)。替换(“-”,“”);
deviceId=SharedReferences.GetString(AppConstants.LOGIN\u CREDENTIAL\u DEVICE\u KEY\u KEY,“”)。替换(“-”,“”);
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=新SecretKeySpec(secretBytes,“AES”);
IvParameterSpec ivSpec=新的IvParameterSpec(secretBytes);
Cipher Cipher=Cipher.GetInstance(“AES/CBC/PKCS5Padding”);
cipher.Init(CipherMode.DecryptMode,secretKeySpec,ivSpec);
cipherInputStream=新的cipherInputStream(文件流,密码);

objectInputStream=新的objectInputStream(cipherInputStream);您可以使用类似的第三方库。它也可以作为

提供。您可以使用类似的第三方库。它也可以作为

        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