private Activity mActivity;
private Context mContext;
private Button encryptionNotFixed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_or);
mActivity
= this;
mContext = this;
encryptionContext = (EditText) findViewById(R.id.et_encryption_context);
encryption = (Button) findViewById(R.id.btn_encryption);
encryptionNotFixed = (Button) findViewById(R.id.btn_encryption_not_fixed);
tvEncryption = (TextView) findViewById(R.id.tv_encryption);
decode = (Button) findViewById(R.id.btn_decode);
tvDecode = (TextView) findViewById(R.id.tv_decode);
initListener();
}
private void initListener() {
encryption.setOnClickListener(this);
encryptionNotFixed.setOnClickListener(this);
decode.setOnClickListener(this);
}
private boolean isEncryption = true;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_encryption://固定加密
String encryptionString = encryptionContext.getText().toString().trim();
if (TextUtils.isEmpty(encryptionString)) {
Toast.makeText(mContext, “请输入加密内容”, Toast.LENGTH_SHORT).show();
return;
}
byte[] encode = OrUtils.encrypt(encryptionString.getBytes());
tvEncryption.setText(Base64Encoder.encode(encode));
isEncryption = true;
break;
case R.id.btn_encryption_not_fixed://不固定加密
String encryptionStringNotFixed = encryptionContext.getText().toString().trim();
if (TextUtils.isEmpty(encryptionStringNotFixed)) {
Toast.makeText(mContext, “请输入加密内容”, Toast.LENGTH_SHORT).show();
return;
}
byte[] encodeNotFixed = OrUtils.encode(encryptionStringNotFixed.getBytes());
tvEncryption.setText(Base64Encoder.encode(encodeNotFixed));
isEncryption = false;
break;
case R.id.btn_decode://解密
String decodeString = tvEncryption.getText().toString().trim();
if (TextUtils.isEmpty(decodeString)) {
Toast.makeText(mContext, “请先加密”, Toast.LENGTH_SHORT).show();
return;
}
if (isEncryption) {
byte[] encrypt = OrUtils.encrypt(Base64Decoder.decodeToBytes(decodeString));
tvDecode.setText(new String(encrypt));
} else {
byte[] decode = OrUtils.decode(Base64Decoder.decodeToBytes(decodeString));
tvDecode.setText(new String(decode));
}
break;
}
}
}
OrUtils
/**
- Created by Administrator on 2017/9/20 0020.
*/
public class OrUtils {
private final static int orKey = 0x520;
/**
-
固定key的方式,
-
-
这种方式加密解密 算法一样
-
@param bytes
-
@return
*/
public static byte[] encrypt(byte[] bytes) {
if (bytes == null) {
return null;
}
int len = bytes.length;
int key = orKey;
//int key = 0x12;
for (int i = 0; i < len; i++) {
bytes[i] ^= key;
}
return bytes;
}
/**
-
不固定key的方式
-
-
加密实现
-
@param bytes
-
@return
*/
public static byte[] encode(byte[] bytes) {
if (bytes == null) {
return null;
}
int len = bytes.length;
int key = orKey;
//int key = 0x12;
for (int i = 0; i < len; i++) {
bytes[i] = (byte) (bytes[i] ^ key);
key = bytes[i];
}
return bytes;
}
/**
-
不固定key的方式
-
-
解密实现
-
@param bytes
-
@return
*/
}
int len = bytes.length;
int key = orKey;
//int key = 0x12;
for (int i = 0; i < len; i++) {
bytes[i] = (byte) (bytes[i] ^ key);
key = bytes[i];
}
return bytes;
}
/**
-
不固定key的方式
-
-
解密实现
-
@param bytes
-
@return
*/