C#之字符串的加密与解密——在xamarin上实现

先上图:

 

先创建一个Encrypt类:

 public class Encrypt
    {
        internal string ToEncrypt(string encryptKey,string str)
        {
            try
            {
                byte[] P_byte_key = Encoding.Unicode.GetBytes(encryptKey);//将秘钥字符串转换为字节序列
                byte[] P_byte_date = Encoding.Unicode.GetBytes(str);
                MemoryStream ms = new MemoryStream();//创建内存字节流对象;
                CryptoStream cs = new CryptoStream(ms,new DESCryptoServiceProvider().CreateEncryptor(P_byte_key,P_byte_key),CryptoStreamMode.Write);//创建加密流对象;
                cs.Write(P_byte_date, 0, P_byte_date.Length);//将加密流写入字节序列
                cs.FlushFinalBlock();//将数据压入基础流;
                byte[] P_bt_stemp = ms.ToArray();//从基础流中获取字节序列
                ms.Close();//关闭内存流
                cs.Close();//关闭加密流
                return Convert.ToBase64String(P_bt_stemp);//返回加密后的字符串
            }
            catch(CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }

        internal string ToDecrypt(string encryptKey, string str)
        {
            try
            {
                byte[] P_byte_key = //将密钥字符串转换为字节序列
                    Encoding.Unicode.GetBytes(encryptKey);
                byte[] P_byte_data = //将加密后的字符串转换为字节序列
                    Convert.FromBase64String(str);
                MemoryStream P_Stream_MS =//创建内存流对象并写入数据
                    new MemoryStream(P_byte_data);
                CryptoStream P_CryptStream_Stream = //创建加密流对象
                    new CryptoStream(P_Stream_MS, new DESCryptoServiceProvider().
                    CreateDecryptor(P_byte_key, P_byte_key), CryptoStreamMode.Read);
                byte[] P_bt_temp = new byte[200];//创建字节序列对象
                MemoryStream P_MemoryStream_temp =//创建内存流对象
                    new MemoryStream();
                int i = 0;//创建记数器
                while ((i = P_CryptStream_Stream.Read(//使用while循环得到解密数据
                    P_bt_temp, 0, P_bt_temp.Length)) > 0)
                {
                    P_MemoryStream_temp.Write(//将解密后的数据放入内存流
                        P_bt_temp, 0, i);
                }
                return //方法返回解密后的字符串
                    Encoding.Unicode.GetString(P_MemoryStream_temp.ToArray());
            }
            catch (CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }
    }

然后在写主类:MainActivity.cs

[Activity(Label = "StringEncrypt", MainLauncher = true)]
    public class MainActivity : Activity
    {
        EditText inputwordAgo, EncryptAfter, DecodeAgo, DecodeAfter,password1,password2;
        Button btnEncrypt, btnDecode;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            inputwordAgo = FindViewById<EditText>(Resource.Id.inputwordAgo);
            EncryptAfter = FindViewById<EditText>(Resource.Id.EncryptAfter);
            DecodeAgo = FindViewById<EditText>(Resource.Id.DecodeAgo);
            DecodeAfter = FindViewById<EditText>(Resource.Id.DecodeAfter);
            password1 = FindViewById<EditText>(Resource.Id.password1);
            password2 = FindViewById<EditText>(Resource.Id.password2);

            btnEncrypt = FindViewById<Button>(Resource.Id.btnEnp);
            btnDecode = FindViewById<Button>(Resource.Id.btnDecode);

            btnEncrypt.Click += btn_Encrypt_Click;
            btnDecode.Click += btn_UnEncrypt_Click;

        }

        private void btn_Encrypt_Click(object sender,EventArgs args)
        {
            if (password1.Text.Length == 4)
            {
                try
                {
                    EncryptAfter.Text = new Encrypt().ToEncrypt(password1.Text, inputwordAgo.Text);
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
                }
            }
            else
            {
                Toast.MakeText(this, "密码长度不符", ToastLength.Short).Show();
            }
        }
        private void btn_UnEncrypt_Click(object sender, EventArgs e)
        {
            if (password2.Text.Length == 4)
            {
                try
                {
                    DecodeAfter.Text = new Encrypt().ToDecrypt(password2.Text, DecodeAgo.Text);
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
                }
            }
            else
            {
                Toast.MakeText(this, "密码长度不符", ToastLength.Short).Show();
            }
        }
    }

源码下载地址:

https://download.csdn.net/download/z1607273131/10683192 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Xamarin 中,要在 `MainViewModel` 中实现页面跳转,您可以使用步骤: 1. 首先,确保您已经安装了 `Xamarin.Forms` 和 `Prism`(如果使用 Prism 框架)的相关包。 2. 在 `MainViewModel` 中创建一个 `INavigationService` 的属性,用于导航操作。示例如下: ```csharp using Prism.Navigation; public class MainViewModel : INotifyPropertyChanged { private readonly INavigationService _navigationService; public MainViewModel(INavigationService navigationService) { _navigationService = navigationService; } // 其他视图模型代码... private async Task NavigateToPage() { await _navigationService.NavigateAsync("YourPage"); } } ``` 3. 确保您已经在 App 的构造函数中注册了 `INavigationService`。示例如下: ```csharp using Prism; using Prism.Ioc; using Prism.Unity; using Xamarin.Forms; public partial class App : PrismApplication { public App(IPlatformInitializer initializer = null) : base(initializer) { } protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<YourPage, YourPageViewModel>(); // 其他页面和视图模型的注册... } protected override void OnInitialized() { InitializeComponent(); NavigationService.NavigateAsync("YourPage"); } } ``` 4. 然后,您可以在 `MainViewModel` 的方法或命令中使用 `_navigationService` 来导航到其他页面。示例如下: ```csharp private async Task NavigateToPage() { await _navigationService.NavigateAsync("YourPage"); } ``` 这样,当您调用 `NavigateToPage` 方法时,将会导航到名为 "YourPage" 的页面。 请注意,上述示例假设您使用了 Prism 框架。如果您使用的是其他框架或没有使用任何框架,导航操作的实现可能会有所不同。因此,请根据您的具体情况进行相应的调整。 希望这能帮助到您。如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值