Android RSA加密解密功能的实现
2015-07-27 14:15
746 查看
本文主要是实现Android RSA加密解密功能,在网上参考了很多人提供的方法,自己改写成一个关于Android RSA加密解密文件的功能模块,方便今后参考使用。
1.在编写代码前,需要准备好RSA的密钥对。关于RSA密钥对的生成网上有很多。可以去下载openssl-0.9.8k_WIN32,打开cmd控制台进入其bin目录,执行以下操作:
(1)先来生成私钥:
openssl genrsa -out rsa_private_key.pem 1024
(2)根据私钥生成公钥:
openssl rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout
(3)这时候的私钥还不能直接被使用,需要进行PKCS#8编码:
openssl pkcs8 -topk8 -in rsa_private_key.pem -out pkcs8_rsa_private_key.pem -nocrypt
注意:把生成的密钥对pkcs8_rsa_private_key.pem,rsa_public_key.pem放到工程目录的assets目录下;
2.把获取到的密钥对放到工程目录的assets目录下之后,就可以开始编写代码了。MainActivity
3.RSAUtils
4.Base64Utils
5.布局文件 activity_rsamain.xml
6.Manifest文件配置
这里不需要添加权限
1.在编写代码前,需要准备好RSA的密钥对。关于RSA密钥对的生成网上有很多。可以去下载openssl-0.9.8k_WIN32,打开cmd控制台进入其bin目录,执行以下操作:
(1)先来生成私钥:
openssl genrsa -out rsa_private_key.pem 1024
(2)根据私钥生成公钥:
openssl rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout
(3)这时候的私钥还不能直接被使用,需要进行PKCS#8编码:
openssl pkcs8 -topk8 -in rsa_private_key.pem -out pkcs8_rsa_private_key.pem -nocrypt
注意:把生成的密钥对pkcs8_rsa_private_key.pem,rsa_public_key.pem放到工程目录的assets目录下;
2.把获取到的密钥对放到工程目录的assets目录下之后,就可以开始编写代码了。MainActivity
public class MainActivity extends Activity implements OnClickListener { private final String TAG="MainActivity"; private Button encryptionBtn, decryptionBtn;// 加密,解密 private EditText inputContent,outputEncryContent, outputDecryContent;// 需加密的内容,加密后的内容,解密后的内容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rsamain); initView(); } private void initView() { encryptionBtn = (Button) findViewById(R.id.encryption_btn); decryptionBtn = (Button) findViewById(R.id.decryption_btn); encryptionBtn.setOnClickListener(this); decryptionBtn.setOnClickListener(this); inputContent = (EditText) findViewById(R.id.input_content); outputEncryContent= (EditText) findViewById(R.id.output_encrycontent); outputDecryContent = (EditText) findViewById(R.id.output_decrycontent); } @Override public void onClick(View v) { switch (v.getId()) { // 加密 case R.id.encryption_btn: String source = inputContent.getText().toString().trim(); Log.d(TAG, source); try { Log.d(TAG, "进入"); // 从文件中得到公钥 InputStream inPublic = getAssets().open("rsa_public_key.pem"); PublicKey publicKey = RSAUtils.loadPublicKey(inPublic); // 加密 byte[] encryptByte = RSAUtils.encryptData(source.getBytes(), publicKey); // 为了方便观察吧加密后的数据用base64加密转一下,要不然看起来是乱码,所以解密是也是要用Base64先转换 String afterencrypt = Base64Utils.encode(encryptByte); Log.d(TAG, afterencrypt ); outputEncryContent.setText(afterencrypt); } catch (Exception e) { e.printStackTrace(); } break; // 解密 case R.id.decryption_btn: String encryptContent = outputEncryContent.getText().toString().trim(); try { // 从文件中得到私钥 InputStream inPrivate = getResources().getAssets().open("pkcs8_rsa_private_key.pem"); PrivateKey privateKey = RSAUtils.loadPrivateKey(inPrivate); // 因为RSA加密后的内容经Base64再加密转换了一下,所以先Base64解密回来再给RSA解密 byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(encryptContent), privateKey); String decryptStr = new String(decryptByte); Log.d(TAG, decryptStr); outputDecryContent.setText(decryptStr); } catch (Exception e) { e.printStackTrace(); } break; default: break; } } }
3.RSAUtils
@SuppressLint("TrulyRandom") public final class RSAUtils { @SuppressWarnings("unused") private static String TAG="RSAUtils"; private static String RSA = "RSA"; /** * 随机生成RSA密钥对(默认密钥长度为1024) * * @return */ public static KeyPair generateRSAKeyPair() { return generateRSAKeyPair(1024); } /** * 随机生成RSA密钥对 * * @param keyLength * 密钥长度,范围:512~2048 * 一般1024 * @return */ @SuppressLint("TrulyRandom") public static KeyPair generateRSAKeyPair(int keyLength) { try { KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA); kpg.initialize(keyLength); return kpg.genKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } /** * 用公钥加密 * 每次加密的字节数,不能超过密钥的长度值减去11 * * @param data * 需加密数据的byte数据 * @param pubKey * 公钥 * @return 加密后的byte型数据 */ public static byte[] encryptData(byte[] data, PublicKey publicKey) { try { Cipher cipher = Cipher.getInstance(RSA); // 编码前设定编码方式及密钥 cipher.init(Cipher.ENCRYPT_MODE, publicKey); // 传入编码数据并返回编码结果 return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 用私钥解密 * * @param encryptedData * 经过encryptedData()加密返回的byte数据 * @param privateKey * 私钥 * @return */ public static byte[] decryptData(byte[] encryptedData, PrivateKey privateKey) { try { Cipher cipher = Cipher.getInstance(RSA); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(encryptedData); } catch (Exception e) { return null; } } /** * 通过公钥byte[](publicKey.getEncoded())将公钥还原,适用于RSA算法 * * @param keyBytes * @return * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static PublicKey getPublicKey(byte[] keyBytes) throws NoSuchAlgorithmException, InvalidKeySpecException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA); PublicKey publicKey = keyFactory.generatePublic(keySpec); return publicKey; } /** * 通过私钥byte[]将公钥还原,适用于RSA算法 * * @param keyBytes * @return * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static PrivateKey getPrivateKey(byte[] keyBytes) throws NoSuchAlgorithmException, InvalidKeySpecException { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } /** * 使用N、e值还原公钥 * * @param modulus * @param publicExponent * @return * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static PublicKey getPublicKey(String modulus, String publicExponent) throws NoSuchAlgorithmException, InvalidKeySpecException { BigInteger bigIntModulus = new BigInteger(modulus); BigInteger bigIntPrivateExponent = new BigInteger(publicExponent); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent); KeyFactory keyFactory = KeyFactory.getInstance(RSA); PublicKey publicKey = keyFactory.generatePublic(keySpec); return publicKey; } /** * 使用N、d值还原私钥 * * @param modulus * @param privateExponent * @return * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static PrivateKey getPrivateKey(String modulus, String privateExponent) throws NoSuchAlgorithmException, InvalidKeySpecException { BigInteger bigIntModulus = new BigInteger(modulus); BigInteger bigIntPrivateExponent = new BigInteger(privateExponent); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent); KeyFactory keyFactory = KeyFactory.getInstance(RSA); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } /** * 从字符串中加载公钥 * * @param publicKeyStr * 公钥数据字符串 * @throws Exception * 加载公钥时产生的异常 */ public static PublicKey loadPublicKey(String publicKeyStr) throws Exception { try { byte[] buffer = Base64Utils.decode(publicKeyStr); KeyFactory keyFactory = KeyFactory.getInstance(RSA); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (NoSuchAlgorithmException e) { throw new Exception("无此算法"); } catch (InvalidKeySpecException e) { throw new Exception("公钥非法"); } catch (NullPointerException e) { throw new Exception("公钥数据为空"); } } /** * 从字符串中加载私钥<br> * 加载时使用的是PKCS8EncodedKeySpec(PKCS#8编码的Key指令)。 * * @param privateKeyStr * @return * @throws Exception */ public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception { try { byte[] buffer = Base64Utils.decode(privateKeyStr); // X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance(RSA); return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { throw new Exception("无此算法"); } catch (InvalidKeySpecException e) { throw new Exception("私钥非法"); } catch (NullPointerException e) { throw new Exception("私钥数据为空"); } } /** * 从文件中输入流中加载公钥 * * @param in * 公钥输入流 * @throws Exception * 加载公钥时产生的异常 */ public static PublicKey loadPublicKey(InputStream in) throws Exception { try { return loadPublicKey(readKey(in)); } catch (IOException e) { throw new Exception("公钥数据流读取错误"); } catch (NullPointerException e) { throw new Exception("公钥输入流为空"); } } /** * 从文件中加载私钥 * * @param keyFileName * 私钥文件名 * @return 是否成功 * @throws Exception */ public static PrivateKey loadPrivateKey(InputStream in) throws Exception { try { return loadPrivateKey(readKey(in)); } catch (IOException e) { throw new Exception("私钥数据读取错误"); } catch (NullPointerException e) { throw new Exception("私钥输入流为空"); } } /** * 读取密钥信息 * * @param in * @return * @throws IOException */ private static String readKey(InputStream in) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(in)); String readLine = null; StringBuilder sb = new StringBuilder(); while ((readLine = br.readLine()) != null) { if (readLine.charAt(0) == '-') { continue; } else { sb.append(readLine); sb.append('\r'); } } return sb.toString(); } /** * 打印公钥信息 * * @param publicKey */ public static void printPublicKeyInfo(PublicKey publicKey) { RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; System.out.println("----------RSAPublicKey----------"); System.out.println("Modulus.length=" + rsaPublicKey.getModulus().bitLength()); System.out.println("Modulus=" + rsaPublicKey.getModulus().toString()); System.out.println("PublicExponent.length=" + rsaPublicKey.getPublicExponent().bitLength()); System.out.println("PublicExponent=" + rsaPublicKey.getPublicExponent().toString()); } public static void printPrivateKeyInfo(PrivateKey privateKey) { RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey; System.out.println("----------RSAPrivateKey ----------"); System.out.println("Modulus.length=" + rsaPrivateKey.getModulus().bitLength()); System.out.println("Modulus=" + rsaPrivateKey.getModulus().toString()); System.out.println("PrivateExponent.length=" + rsaPrivateKey.getPrivateExponent().bitLength()); System.out.println("PrivatecExponent=" + rsaPrivateKey.getPrivateExponent().toString()); } }
4.Base64Utils
public final class Base64Utils { private static char[] base64EncodeChars = new char[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; private static byte[] base64DecodeChars = new byte[] {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1}; /** * 加密 * @param data * @return */ public static String encode(byte[] data) { if (data == null || data.length < 1) return null; StringBuilder sb = new StringBuilder(); int len = data.length; int i = 0; int b1, b2, b3; while (i < len) { b1 = data[i++] & 0xff; if (i == len) { sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[(b1 & 0x3) << 4]); sb.append("=="); break; } b2 = data[i++] & 0xff; if (i == len) { sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); sb.append(base64EncodeChars[(b2 & 0x0f) << 2]); sb.append("="); break; } b3 = data[i++] & 0xff; sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]); sb.append(base64EncodeChars[b3 & 0x3f]); } return sb.toString(); } /** * 解密 * @param str * @return */ public static byte[] decode(String str) { if (TextUtils.isEmpty(str)) return null; try { return decodePrivate(str); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return new byte[]{}; } private static byte[] decodePrivate(String str) throws UnsupportedEncodingException { if (TextUtils.isEmpty(str)) return null; StringBuilder sb = new StringBuilder(); byte[] data = str.getBytes("US-ASCII"); int len = data.length; int i = 0; int b1, b2, b3, b4; while (i < len) { do { b1 = base64DecodeChars[data[i++]]; } while (i < len && b1 == -1); if (b1 == -1) break; do { b2 = base64DecodeChars[data[i++]]; } while (i < len && b2 == -1); if (b2 == -1) break; sb.append((char) ((b1 << 2) | ((b2 & 0x30) >>> 4))); do { b3 = data[i++]; if (b3 == 61) return sb.toString().getBytes("iso8859-1"); b3 = base64DecodeChars[b3]; } while (i < len && b3 == -1); if (b3 == -1) break; sb.append((char) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2))); do { b4 = data[i++]; if (b4 == 61) return sb.toString().getBytes("iso8859-1"); b4 = base64DecodeChars[b4]; } while (i < len && b4 == -1); if (b4 == -1) break; sb.append((char) (((b3 & 0x03) << 6) | b4)); } return sb.toString().getBytes("iso8859-1"); } }
5.布局文件 activity_rsamain.xml
<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" tools:context="com.example.rsa.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RSA加密解密功能测试" android:textSize="24dip" android:textStyle="bold" android:layout_gravity="center" android:layout_marginTop="40dip"/> <EditText android:id="@+id/input_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入需加密的内容" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <Button android:id="@+id/encryption_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加密" /> <Button android:id="@+id/decryption_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解密" /> </LinearLayout> <EditText android:id="@+id/output_encrycontent" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="加密后的内容" /> <EditText android:id="@+id/output_decrycontent" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="解密后的内容" /> </LinearLayout>
6.Manifest文件配置
这里不需要添加权限
相关文章推荐
- Android ndk 单步调试
- Android碎片第三期
- Android Studio手动导入第三方类库
- 【第五篇】androidEventbus源代码阅读和分析之发送粘性事件和接收粘性事件代码分析
- android底部弹出菜单
- android listView点击item,高亮显示
- Android完全退出程序
- android service(二)
- 通过html页面打开Android本地的app
- Android ServiceManager启动
- android4.4修改设置界面里,蓝牙默认名称
- 最新百度地图android sdk自定义覆盖物
- 基于Monkey的Android自动化测试
- Android 防止快速 点击 多次
- 【Android4.4蓝牙代码分析】- 蓝牙Enable过程
- Android 多分辨率自适应总结
- Android RIL
- Android屏幕适配方案
- AccessibilityService辅助类用法(Android 中的另类钩子)
- android反编译