例:
A和B约定好秘钥"123456"
(比如客户端和服务端)
A给B发消息:"我喜欢你"
A用 "123456"
对 “我喜欢你” 加密得到结果 “乱码”,把 “乱码” 发给B
B用 "123456"
对 “乱码” 解密得到结果 "我喜欢你"
DES:
数据加密算法(Data Encryption Algorithm,缩写DEA):
运算速度快,资源消耗较少,安全性低
AES:
高级加密标准(英语:Advanced Encryption Standard,缩写AES):
这个标准用来替代原先的DES,运算速度快,安全性高,资源消耗少
package com.i.cipher
import android.util.Base64
import java.security.*
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.DESKeySpec
import javax.crypto.spec.SecretKeySpec
//---------------------------------DES-----------------------------------
fun desEnc(input: String, password: String): String {
val cipher = Cipher.getInstance("DES")
val secretKey = SecretKeyFactory.getInstance("DES")
val keySpec = DESKeySpec(password.toByteArray())
val key: Key = secretKey.generateSecret(keySpec)
cipher.init(Cipher.ENCRYPT_MODE, key)
val encrypt = cipher.doFinal(input.toByteArray())
return Base64.encodeToString(encrypt, Base64.NO_WRAP)
}
fun desDec(input: String, password: String): String {
val cipher = Cipher.getInstance("DES")
val secretKey = SecretKeyFactory.getInstance("DES")
val keySpec = DESKeySpec(password.toByteArray())
val key: Key = secretKey.generateSecret(keySpec)
cipher.init(Cipher.DECRYPT_MODE, key)
val encrypt = cipher.doFinal(Base64.decode(input, Base64.NO_WRAP))
return String(encrypt)
}
//---------------------------------AES-----------------------------------
fun aesEnc(input: String, password: String): String {
val cipher = Cipher.getInstance("AES")
val keySpec = SecretKeySpec(password.toByteArray(), "AES")
cipher.init(Cipher.ENCRYPT_MODE, keySpec)
val encrypt = cipher.doFinal(input.toByteArray())
return Base64.encodeToString(encrypt, Base64.NO_WRAP)
}
fun aesDec(input: String, password: String): String {
val cipher = Cipher.getInstance("AES")
val keySpec = SecretKeySpec(password.toByteArray(), "AES")
cipher.init(Cipher.DECRYPT_MODE, keySpec)
val encrypt = cipher.doFinal(Base64.decode(input, Base64.NO_WRAP))
return String(encrypt)
}
//---------------------------------AES(keyGenerator)推荐-----------------------------------
fun createKey(password: String): Key {
val keyGenerator = KeyGenerator.getInstance("AES")
keyGenerator.init(128, SecureRandom(password.toByteArray()))
return keyGenerator.generateKey()
}
fun encode(key: Key, input: String): String {
val cipher = Cipher.getInstance("AES")
cipher.init(Cipher.ENCRYPT_MODE, key)
val dataByte = cipher.doFinal(input.toByteArray())
val encodeByte = Base64.encodeToString(dataByte, Base64.NO_WRAP)
return encodeByte
}
fun decode(key: Key, input: String): String {
val decodeByte = Base64.decode(input.toByteArray(), Base64.NO_WRAP)
val cipher = Cipher.getInstance("AES")
cipher.init(Cipher.DECRYPT_MODE, key)
val dataByte = cipher.doFinal(decodeByte)
return String(dataByte)
}
测试代码
//base64的基本加解密
btn_base64.setOnClickListener {
val input = "今天天气啦啦啦"
tv_before.setText("加密前:" + input)
val encode = Base64.encodeToString(input.toByteArray(), Base64.NO_WRAP);
tv_after.setText("加密后:" + encode)
val decode = String(Base64.decode(encode, Base64.NO_WRAP))
tv_dec_after.setText("解密后:" + decode)
}
btn_des.setOnClickListener {
val input = "今天天气不错"
//8位密码
val password = "12345678"
tv_before.setText("加密前:" + input)
val encrypt = desEnc(input, password)
tv_after.setText("加密后:" + encrypt)
val decrypt = desDec(encrypt, password)
tv_dec_after.setText("解密后:" + decrypt)
}
btn_aes.setOnClickListener {
val input = "今天天气还行"
//16位密码
val password = "1234567812345678"
tv_before.setText("加密前:" + input)
val encrypt = aesEnc(input, password)
tv_after.setText("加密后:" + encrypt)
val decrypt = aesDec(encrypt, password)
tv_dec_after.setText("解密后:" + decrypt)
}
btn_aes_keyGenerator.setOnClickListener {
val input = "今天天气太好了"
val password = "123456abc"
tv_before.setText("加密前:" + input)
val createKey = createKey(password)
val encode = encode(createKey, input)
tv_after.setText("加密后:" + encode)
val decode = decode(createKey, encode)
tv_dec_after.setText("解密后:" + decode)
}
例:
A生成"APrikey","APubkey"
B生成"BPrikey","BPubkey"
A给B发消息:"我喜欢你"
A用 "BPubkey"
对 “我喜欢你” 加密得到结果 “乱码”,把 “乱码” 发给B
B用 "BPrikey"
对 “乱码” 解密得到结果 "我喜欢你"
B给A发消息同理
//---------------------------------RSA-----------------------------------
fun createKeyPair(): KeyPair {
val generator = KeyPairGenerator.getInstance("RSA")
return generator.genKeyPair()
}
/**
* 私钥加密
*/
fun encByPrivateKey(input: String, privateKey: PrivateKey): String {
val cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.ENCRYPT_MODE, privateKey)
val encrypt = cipher.doFinal(input.toByteArray())
return Base64.encodeToString(encrypt, Base64.NO_WRAP)
}
/**
* 公钥解密
*/
fun decByPublicKey(input: String, publicKey: PublicKey): String {
val decode = Base64.decode(input, Base64.NO_WRAP)
val cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.DECRYPT_MODE, publicKey)
val encrypt = cipher.doFinal(decode)
return String(encrypt)
}
/**
* 公钥加密
*/
fun encByPublicKey(input: String, publicKey: PublicKey): String {
val cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.ENCRYPT_MODE, publicKey)
val encrypt = cipher.doFinal(input.toByteArray())
return Base64.encodeToString(encrypt, Base64.NO_WRAP)
}
/**
* 私钥解密
*/
fun decByPrivateKey(input: String, privateKey: PrivateKey): String {
val decode = Base64.decode(input, Base64.NO_WRAP)
val cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.DECRYPT_MODE, privateKey)
val encrypt = cipher.doFinal(decode)
return String(encrypt)
}
测试代码
//私钥加密,公钥解密
fun priTopub() {
val input = "今天天气真好啊"
tv_before.setText("加密前:" + input)
val keyPair = createKeyPair()
val encByPrivateKey = encByPrivateKey(input, keyPair.private)
tv_after.setText("加密后:" + encByPrivateKey)
val decByPublicKey = decByPublicKey(encByPrivateKey, keyPair.public)
tv_dec_after.setText("解密后:" + decByPublicKey)
}
//公钥加密,私钥解密
fun pubTopri() {
val input = "今天天气非常好啊"
tv_before.setText("加密前:" + input)
val keyPair = createKeyPair()
val encByPublicKey = encByPublicKey(input, keyPair.public)
tv_after.setText("加密后:" + encByPublicKey)
val decByPrivateKey = decByPrivateKey(encByPublicKey, keyPair.private)
tv_dec_after.setText("解密后:" + decByPrivateKey)
}
参考:
https://zhuanlan.zhihu.com/p/49494990
https://blog.csdn.net/u011109931/article/details/79466587
https://blog.csdn.net/hongxue8888/article/details/103850353
源码:https://gitee.com/zzgits/Cipher