凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。

下面,我们就来在安卓上实现此技术:

  首先,我们需要创建一个安卓工程,然后在布局文件中加入按钮用以实现点击事件,这些就不重复叙述了,读者可以在以前的安卓项目中学习,我们主要讲点击事件:

    如下所示代码为加密代码:


public void onClick(View v)
		{
		if (v.isPressed())// 加密
			{
			String str_p = et_plaintext.getText().toString();
				char []str_p_char = null;
				try
				{
				str_p_char=str_p.toCharArray();
				}
				catch(Exception e)
				{
				System.out.println("Exception");
				}
				int len=str_p_char.length;
				int k=6;
				for(int j=0;j<len;j++)
				{
				System.out.println(str_p_char[j]);//输出得到的字符串,也就是从文本编辑框得到的明文
				//对明文进行加密
								
				{
					int value_ascii=(str_p_char[j]+k);
					str_p_char[j]=(char) (value_ascii);
				}
				}
					StringBuffer sb = new StringBuffer();
					for(int i = 0; i <len; i++)
					{ 
					  sb. append(str_p_char[i]);
					}
					str_p = sb.toString();
				 System.out.println("加密后的字符串:"+str_p);
				et_plaintext.setEnabled(true);
				Toast.makeText(Result_PTC.this, "已加密...",Toast.LENGTH_SHORT).show();
				///直接转入到加密后的文件中
				System.out.println("转入到加密以后的文件....,不管你信不信,代码是这样的");
				//创建一个意图
				Intent intent=new Intent(Result_PTC.this,Result_CTP.class);
				System.out.println("如果我运行了,那就是已正常的从加密转到解密页面了");
				//传递自定义数据
				//第一种传值方式,创建一个Bundle对象
				//传递自定义数据
				PassIntent passIntent=new PassIntent();
				passIntent.id_opt=id_opt;
				passIntent.x=ptc_x;
				passIntent.y=ptc_y;
				passIntent.name_intent=ptc_name_intent;
				passIntent.str_ptc=str_p;
				intent.putExtra("passIntent", passIntent);
				//启动另外一个Activity
				startActivity(intent);
				System.out.println("如果我运行了,那就是已正常的从加密转到解密页面了");
				finish();
				
		}




   如下所示为解密代码:

@Override
		public void onClick(View v) 
	{
	if (v.isPressed())// 解密
	{
								
	String str_c = et_ciphertext.getText().toString();
	System.out.println(str_c);//输出得到的字符串,也就是从文本编辑框得到的密文
		char []str_c_char = null;
	try
	{
	str_c_char=str_c.toCharArray();
	 }
	catch(Exception e)
	{
	System.out.println("Exception");
	}
	int len=str_c_char.length;
	int k=6;
	 for(int j=0;j<len;j++)
	{
	System.out.println(str_c_char[j]);//输出得到的字符串,也就是从文本编辑框得到的密		
              {
			int value_ascii=(str_c_char[j]-k);
			str_c_char[j]=(char) (value_ascii);
		}
	}
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i <len; i++)
		{ 
			sb. append(str_c_char[i]);
		}
		str_c = sb.toString();
		 System.out.println("加密后的字符串:"+str_c);
		et_ciphertext.setEnabled(true);
		Toast.makeText(Result_CTP.this, "已解密...",Toast.LENGTH_SHORT).show();
		//创建一个意图
		Intent intent=new Intent(Result_CTP.this,Result_PTC.class);
									
		PassIntent passIntent=new PassIntent();
		passIntent.id_opt= id_opt;
		passIntent.x=ctp_x;
		passIntent.y=ctp_y;
		passIntent.name_intent=ctp_name_intent;
								
		passIntent.str_ctp=str_c;
		intent.putExtra("passIntent", passIntent);
		//启动另外一个Activity
		startActivity(intent);
		 finish();
	}

上面的代码分别对ASCII码进行了的加减k,对应这加密与解密。


加密效果为:

android 密码存储 开发 android的密码_android

解密效果为:

android 密码存储 开发 android的密码_信息安全_02