Android 登录密码信息进行RSA加密示例
程序员文章站
2023-12-18 14:09:58
首先 有服务端生产一对公钥和私钥 我们在进行加密之前,先从服务器获取公钥,获取到公钥串之后,在对密码进行加密。
复制代码 代码如下:
map.put("password...
首先 有服务端生产一对公钥和私钥 我们在进行加密之前,先从服务器获取公钥,获取到公钥串之后,在对密码进行加密。
复制代码 代码如下:
map.put("password", new string(hex.encodehex(rsautils.encryptbypublickey(password,rsastr))));
此处,就是在登陆之前,对密码进行加密
password:登录密码 rsastr:从服务器获取的公钥串
hex.encodehex:对加密后的密码串进行编码,项目中需要集成相应的jar。
下面给出 rsautils的代码
import java.io.bytearrayoutputstream; import java.security.key; import java.security.keyfactory; import java.security.keypair; import java.security.keypairgenerator; import java.security.privatekey; import java.security.publickey; import java.security.signature; import java.security.interfaces.rsaprivatekey; import java.security.interfaces.rsapublickey; import java.security.spec.pkcs8encodedkeyspec; import java.security.spec.x509encodedkeyspec; import java.util.hashmap; import java.util.map; import javax.crypto.cipher; /** * <p> * rsa公钥/私钥/签名工具包 * </p> * <p> * 罗纳德·李维斯特(ron [r]ivest)、阿迪·萨莫尔(adi [s]hamir)和伦纳德·阿德曼(leonard [a]dleman) * </p> * <p> * 字符串格式的密钥在未在特殊说明情况下都为base64编码格式<br/> * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,<br/> * 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全 * </p> * * @author icewee * @version 1.0 * @date 2012-4-26 */ public class rsautils { /** * 加密算法rsa */ public static final string key_algorithm = "rsa"; /** * 签名算法 */ public static final string signature_algorithm = "md5withrsa"; /** * 获取公钥的key */ private static final string public_key = "rsapublickey"; /** * 获取私钥的key */ private static final string private_key = "rsaprivatekey"; /** * rsa最大加密明文大小 */ private static final int max_encrypt_block = 117; /** * rsa最大解密密文大小 */ private static final int max_decrypt_block = 128; /** * <p> * 生成密钥对(公钥和私钥) * </p> * * @return * @throws exception */ public static map<string, object> genkeypair() throws exception { keypairgenerator keypairgen = keypairgenerator.getinstance(key_algorithm); keypairgen.initialize(1024); keypair keypair = keypairgen.generatekeypair(); rsapublickey publickey = (rsapublickey) keypair.getpublic(); rsaprivatekey privatekey = (rsaprivatekey) keypair.getprivate(); map<string, object> keymap = new hashmap<string, object>(2); keymap.put(public_key, publickey); keymap.put(private_key, privatekey); return keymap; } /** * <p> * 用私钥对信息生成数字签名 * </p> * * @param data 已加密数据 * @param privatekey 私钥(base64编码) * @return * @throws exception */ public static string sign(byte[] data, string privatekey) throws exception { byte[] keybytes = base64utils.decode(privatekey); pkcs8encodedkeyspec pkcs8keyspec = new pkcs8encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance(key_algorithm); privatekey privatek = keyfactory.generateprivate(pkcs8keyspec); signature signature = signature.getinstance(signature_algorithm); signature.initsign(privatek); signature.update(data); return base64utils.encode(signature.sign()); } /** * <p> * 校验数字签名 * * </p> * * @param data 已加密数据 * @param publickey 公钥(base64编码) * @param sign 数字签名 * @return * @throws exception */ public static boolean verify(byte[] data, string publickey, string sign) throws exception { byte[] keybytes = base64utils.decode(publickey); x509encodedkeyspec keyspec = new x509encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance(key_algorithm); publickey publick = keyfactory.generatepublic(keyspec); signature signature = signature.getinstance(signature_algorithm); signature.initverify(publick); signature.update(data); return signature.verify(base64utils.decode(sign)); } /** * <p> * 私钥解密 * </p> * * @param encrypteddata 已加密数据 * @param privatekey 私钥(base64编码) * @return * @throws exception */ public static byte[] decryptbyprivatekey(byte[] encrypteddata, string privatekey) throws exception { byte[] keybytes = base64utils.decode(privatekey); pkcs8encodedkeyspec pkcs8keyspec = new pkcs8encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance(key_algorithm); key privatek = keyfactory.generateprivate(pkcs8keyspec); // cipher cipher = cipher.getinstance(keyfactory.getalgorithm()); cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding"); cipher.init(cipher.decrypt_mode, privatek); int inputlen = encrypteddata.length; bytearrayoutputstream out = new bytearrayoutputstream(); int offset = 0; byte[] cache; int i = 0; // 对数据分段解密 while (inputlen - offset > 0) { if (inputlen - offset > max_decrypt_block) { cache = cipher.dofinal(encrypteddata, offset, max_decrypt_block); } else { cache = cipher.dofinal(encrypteddata, offset, inputlen - offset); } out.write(cache, 0, cache.length); i++; offset = i * max_decrypt_block; } byte[] decrypteddata = out.tobytearray(); out.close(); return decrypteddata; } /** * <p> * 公钥解密 * </p> * * @param encrypteddata 已加密数据 * @param publickey 公钥(base64编码) * @return * @throws exception */ public static byte[] decryptbypublickey(byte[] encrypteddata, string publickey) throws exception { byte[] keybytes = base64utils.decode(publickey); x509encodedkeyspec x509keyspec = new x509encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance(key_algorithm); key publick = keyfactory.generatepublic(x509keyspec); // cipher cipher = cipher.getinstance(keyfactory.getalgorithm()); cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding"); cipher.init(cipher.decrypt_mode, publick); int inputlen = encrypteddata.length; bytearrayoutputstream out = new bytearrayoutputstream(); int offset = 0; byte[] cache; int i = 0; // 对数据分段解密 while (inputlen - offset > 0) { if (inputlen - offset > max_decrypt_block) { cache = cipher.dofinal(encrypteddata, offset, max_decrypt_block); } else { cache = cipher.dofinal(encrypteddata, offset, inputlen - offset); } out.write(cache, 0, cache.length); i++; offset = i * max_decrypt_block; } byte[] decrypteddata = out.tobytearray(); out.close(); return decrypteddata; } /** * <p> * 公钥加密 * </p> * * @param sourcedata 源数据 * @param publickey 公钥(base64编码) * @return * @throws exception */ public static byte[] encryptbypublickey(string sourcedata, string publickey) throws exception { byte[] data = sourcedata.getbytes(); byte[] keybytes = base64utils.decode(publickey); x509encodedkeyspec x509keyspec = new x509encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance(key_algorithm); key publick = keyfactory.generatepublic(x509keyspec); // 对数据加密 // cipher cipher = cipher.getinstance(keyfactory.getalgorithm()); cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding"); cipher.init(cipher.encrypt_mode, publick); int inputlen = data.length; bytearrayoutputstream out = new bytearrayoutputstream(); int offset = 0; byte[] cache; int i = 0; // 对数据分段加密 while (inputlen - offset > 0) { if (inputlen - offset > max_encrypt_block) { cache = cipher.dofinal(data, offset, max_encrypt_block); } else { cache = cipher.dofinal(data, offset, inputlen - offset); } out.write(cache, 0, cache.length); i++; offset = i * max_encrypt_block; } byte[] encrypteddata = out.tobytearray(); out.close(); return encrypteddata; } /** * <p> * 私钥加密 * </p> * * @param data 源数据 * @param privatekey 私钥(base64编码) * @return * @throws exception */ public static byte[] encryptbyprivatekey(byte[] data, string privatekey) throws exception { byte[] keybytes = base64utils.decode(privatekey); pkcs8encodedkeyspec pkcs8keyspec = new pkcs8encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance(key_algorithm); key privatek = keyfactory.generateprivate(pkcs8keyspec); // cipher cipher = cipher.getinstance(keyfactory.getalgorithm()); cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding"); cipher.init(cipher.encrypt_mode, privatek); int inputlen = data.length; bytearrayoutputstream out = new bytearrayoutputstream(); int offset = 0; byte[] cache; int i = 0; // 对数据分段加密 while (inputlen - offset > 0) { if (inputlen - offset > max_encrypt_block) { cache = cipher.dofinal(data, offset, max_encrypt_block); } else { cache = cipher.dofinal(data, offset, inputlen - offset); } out.write(cache, 0, cache.length); i++; offset = i * max_encrypt_block; } byte[] encrypteddata = out.tobytearray(); out.close(); return encrypteddata; } /** * <p> * 获取私钥 * </p> * * @param keymap 密钥对 * @return * @throws exception */ public static string getprivatekey(map<string, object> keymap) throws exception { key key = (key) keymap.get(private_key); return base64utils.encode(key.getencoded()); } /** * <p> * 获取公钥 * </p> * * @param keymap 密钥对 * @return * @throws exception */ public static string getpublickey(map<string, object> keymap) throws exception { key key = (key) keymap.get(public_key); return base64utils.encode(key.getencoded()); } // 把byte[]元素之间添加空格,并转化成字符串返回, public static string bytetostring(byte[] resouce){ stringbuffer sb = new stringbuffer(); for (int i = 0; i < resouce.length; i++) { if (i == resouce.length-1) { sb.append(byte.tostring(resouce[i])); }else{ sb.append(byte.tostring(resouce[i])); sb.append(" "); } } return sb.tostring(); } // 把字符串按照空格进行拆分成数组,然后转化成byte[],返回 public static byte[] stringtobyte(string resouce){ string[] strarr = resouce.split(" "); int len = strarr.length; byte[] clone = new byte[len]; for (int i = 0; i < len; i++) { clone[i] = byte.parsebyte(strarr[i]); } return clone; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。