• [android]DES/3DES/AES加密方式


    DES 支持8位加密解密,3Des支持24位,Aes支持32位。3Des是Des算法做三次。位数的单位是字节byte。不是bits。

    3Des是把24位分成3组。第一组八位用来加密,第二组8位用于解密,第三组8位用于加密,所以。假设秘钥为123456781234567812345678(3组1-8),则相当于做了一次12345678的Des加密。比如:第一次用12345678秘钥对123进行加密得到 "LDiFUdf0iew=",然后用第二组的12345678对其进行解密(逆向加密过程)。得到了123,第三次重新加密得到 "LDiFUdf0iew="。


    三种加密方式代码里不同的地方:

    byte temp[] = new byte[位数];

    SecretKey des_key = new SecretKeySpec(temp, "加密算法");

    加密算法名相应的是:Aes Des Desede

    改了这两个地方就能够实现不同加密方式。

    加密方式底层不一样。DES是把原文编程2进制的一串01。然后和密文做运算,交换什么什么位置。

    public class MainActivity extends Activity implements OnClickListener {
    
    	private EditText des_key, des_src, des_dst;
    	private Button btn_encode, btn_decode;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		des_key = (EditText) findViewById(R.id.des_key);
    		des_src = (EditText) findViewById(R.id.des_src);
    		des_dst = (EditText) findViewById(R.id.des_dst);
    		btn_encode = (Button) findViewById(R.id.btn_encode);
    		btn_decode = (Button) findViewById(R.id.btn_decode);
    		btn_encode.setOnClickListener(this);
    		btn_decode.setOnClickListener(this);
    	}
    
    	@Override
    	public void onClick(View v) {
    		String key_str = des_key.getText().toString();
    		if (!TextUtils.isEmpty(key_str)) {
    			try {
    				// DES无论长了短了。都变成八位
    				// AES 长度变为128 new SecretKeySpec(temp, "Aes");
    				// 3Des 长度变为24 new SecretKeySpec(temp, "Desced");
    				byte temp[] = new byte[8];
    				byte b[] = key_str.getBytes("UTF-8");
    				System.arraycopy(b, 0, temp, 0, Math.min(b.length, temp.length));
    				// Des仅仅支持八位
    				SecretKey des_key = new SecretKeySpec(temp, "Des");
    				Cipher cipher = Cipher.getInstance("Des");
    				switch (v.getId()) {
    				case R.id.btn_encode:
    					String src_str = des_src.getText().toString();
    					if (!TextUtils.isEmpty(src_str)) {
    						cipher.init(Cipher.ENCRYPT_MODE, des_key);
    						byte[] bytes = cipher
    								.doFinal(src_str.getBytes("UTF-8"));
    						// 是用Base64编码的二进制字节数组
    						des_dst.setText(Base64.encodeToString(bytes,
    								Base64.DEFAULT));
    					}
    					break;
    
    				case R.id.btn_decode:
    					String dst_str = des_dst.getText().toString();
    					if (!TextUtils.isEmpty(dst_str)) {
    						cipher.init(Cipher.DECRYPT_MODE, des_key);
    						// 是用Base64编码的二进制字节数组
    						byte[] bytes = cipher.doFinal(Base64.decode(dst_str,
    								Base64.DEFAULT));
    						des_src.setText(new String(bytes, "UTF-8"));
    					}
    					break;
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }



    
    布局:
    

    <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <EditText
            android:id="@+id/des_key"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="秘钥" />
    
        <EditText
            android:id="@+id/des_src"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="原文" />
    
        <EditText
            android:id="@+id/des_dst"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="密文" />
    
        <Button
            android:id="@+id/btn_encode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加密" />
    
        <Button
            android:id="@+id/btn_decode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="解密" />
    
    </LinearLayout></span>





  • 相关阅读:
    软件开发环境-开发环境、测试环境、生产环境的区别
    软件开发环境-开发环境、测试环境、生产环境的区别
    软件开发环境-开发环境、测试环境、生产环境的区别
    Proof of Stake FAQ
    【转】Ouroboros:一个可证明安全的PoS区块链协议 (共识介绍)
    可验证随机函数VRF
    Randao 可证公平随机数(VRF)白皮书
    VRF介绍
    随机数概论——VRF,Commit Reveal,BLS的原理及应用
    『分片技术分析』从分片开始了解区块链扩容方式
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6931865.html
Copyright © 2020-2023  润新知