先上图:
先创建一个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();
}
}
}
源码下载地址: