编码:
String encode = URLEncoder.encode(content, "UTF-8");
解码:
String decode = URLDecoder.decode(result, "UTF-8");
3.
Des
加密:
try {
// 初始化 设置加密解密的类型
Cipher cipher = Cipher.getInstance("DES");
// 初始化密码
SecretKeySpec keySpec = new SecretKeySpec(str.getBytes(),"DES");
try {
//设置此次操作是加密还是解密
cipher.init(Cipher.ENCRYPT_MODE,keySpec);
// 加密
final byte[] finalResult = cipher.doFinal(password.getBytes());
//经过base64处理避免出现乱码情况
byte[] encode = Base64.encode(finalResult, Base64.DEFAULT);
String result = new String(encode);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
解密:
try {
Cipher cipherDecode = Cipher.getInstance("DES");
SecretKeySpec spec = new SecretKeySpec(password.getBytes(),"DES");
cipherDecode.init(Cipher.DECRYPT_MODE,spec);
// 获取textView加密后的结果
byte[] decode = Base64.decode(result, Base64.DEFAULT);
byte[] doFinal = cipherDecode.doFinal(decode);
String result = new String(doFinal);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
4.Aes
加密:
try {
// 初始化 设置加密解密的类型
Cipher cipher = Cipher.getInstance("AES");
// 初始化密码
SecretKeySpec keySpec = new SecretKeySpec(password.getBytes(),"AES");
try {
//设置此次操作是加密还是解密
cipher.init(Cipher.ENCRYPT_MODE,keySpec);
// 加密
final byte[] finalResult = cipher.doFinal(str.getBytes());
//经过base64处理避免出现乱码情况
byte[] encode = Base64.encode(finalResult, Base64.DEFAULT);
String result = new String(encode);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
解密:
try {
Cipher cipherDecode = Cipher.getInstance("AES");
SecretKeySpec spec = new SecretKeySpec(passwords.getBytes(),"AES");
cipherDecode.init(Cipher.DECRYPT_MODE,spec);
// 获取textView加密后的结果
byte[] decode = Base64.decode(result, Base64.DEFAULT);
byte[] doFinal = cipherDecode.doFinal(decode);
String result = new String(doFinal);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
5.RSA
private PublicKey mPublicKey;
private PrivateKey mPrivateKey;
//生成公钥和密钥
try {
// 初始化密码对生成器
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
// 设置密码的长度
generator.initialize(1024);
// 生成密码对
KeyPair keyPair = generator.generateKeyPair();
// 获取公钥
mPublicKey = keyPair.getPublic();
//获取私钥
mPrivateKey = keyPair.getPrivate();
//////////////////////////把私钥存储String到本地//////////////////////////////
byte[] mPrivateKeyEncoded = mPrivateKey.getEncoded();
byte[] encode = Base64.encode(mPrivateKeyEncoded, Base64.DEFAULT);
String key = new String(encode);
SharedPreferences sp = getSharedPreferences("day02", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("privateKey",key);
edit.commit();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
加密:
try {
Cipher cipher = Cipher.getInstance("RSA");
// 设置加密或者解密模式 ,传入公钥
cipher.init(Cipher.ENCRYPT_MODE,mPublicKey);
byte[] encode = cipher.doFinal(content.getBytes());
byte[] bytes = Base64.encode(encode, Base64.DEFAULT);
String result = new String(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
解密:
String result = mTextView.getText().toString();
byte[] decode = Base64.decode(result, Base64.DEFAULT);
try {
Cipher ciphers = Cipher.getInstance("RSA");
ciphers.init(Cipher.DECRYPT_MODE,mPrivateKey);
byte[] bytes = ciphers.doFinal(decode);
String result = new String(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
通过服务器给我们的字符串来解密
// 1 通过字符串生成私钥
SharedPreferences sharedPreferences = getSharedPreferences("wenjian", Context.MODE_PRIVATE);
String stringKey = sharedPreferences.getString("privateKey", "");
if (TextUtils.isEmpty(stringKey)){
Log.d("TAG", "onClick: 私钥没有存成功");
return;
}else {
Log.d("TAG", "onClick: StringKey == "+stringKey);
}
//因为我们存私钥的经过Base64编码过,所以我们必须解码回去
byte[] bytesKey = Base64.decode(stringKey, Base64.DEFAULT);
// 通过byte数组生成公钥或者私钥
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytesKey);
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(spec);
// 2 解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,privateKey);
byte[] decode = Base64.decode(result, Base64.DEFAULT);
byte[] doFinal = cipher.doFinal(decode);
mTextView.setText(new String(doFinal));
isEncode = false;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
6.MD5摘要
//摘要
try {
// 初始化
MessageDigest digest = MessageDigest.getInstance("MD5");
//指定 byte数组来进行摘要
digest.update(str.getBytes());
// 获取摘要的结果
byte[] results = digest.digest();
// 创建Buffer来累加字符串
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < results.length; i++) {
byte result = results[i];
// 把得到结果 传化成16进制的字符串
String s = Integer.toHexString(result & 0xFF);
if (s.length() == 1) {
buffer.append("0");
}
buffer.append(s);
}
String result = buffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
7.
Sha1
//摘要
try {
// 初始化
MessageDigest digest = MessageDigest.getInstance("SHA1");
//指定 byte数组来进行摘要
digest.update(content.getBytes());
// 获取摘要的结果
byte[] results = digest.digest();
// 创建Buffer来累加字符串
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < results.length; i++) {
byte result = results[i];
// 把得到结果 传化成16进制的字符串
String s = Integer.toHexString(result & 0xFF);
//
if (s.length()==1){
buffer.append("0");
}
buffer.append(s);
}
String result = buffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
这里仅仅是几种加密解密的代码,具体介绍有时间再做,基本也就这么多东西,能用就行了。