android代码16进制公钥进行RSA加密
2017-01-13 15:47
423 查看
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
public final class RSAUtils {
/**
* 获取公钥方法(16进制公钥)
* @param modulus 公钥字符串(128字节)
* @param exponent 公钥指数 "10001"
* @return 公钥
*/
public static RSAPublicKey getPublicKey(String modulus, String exponent) {
try {
BigInteger n = new BigInteger(modulus, 16); // 此处为进制数
BigInteger e = new BigInteger(exponent, 16);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(n, e);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 加密
*
* @param key
* 加密的密钥
* @param data
* 待加密的明文数据
* @return 加密后的数据
* @throws Exception
*/
public static byte[] encrypt(byte[] bt_plaintext, PublicKey key)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bt_encrypted = cipher.doFinal(bt_plaintext);
return bt_encrypted;
}
}
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
public final class RSAUtils {
/**
* 获取公钥方法(16进制公钥)
* @param modulus 公钥字符串(128字节)
* @param exponent 公钥指数 "10001"
* @return 公钥
*/
public static RSAPublicKey getPublicKey(String modulus, String exponent) {
try {
BigInteger n = new BigInteger(modulus, 16); // 此处为进制数
BigInteger e = new BigInteger(exponent, 16);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(n, e);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 加密
*
* @param key
* 加密的密钥
* @param data
* 待加密的明文数据
* @return 加密后的数据
* @throws Exception
*/
public static byte[] encrypt(byte[] bt_plaintext, PublicKey key)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bt_encrypted = cipher.doFinal(bt_plaintext);
return bt_encrypted;
}
}
相关文章推荐
- Android RSA公钥加密
- 详细讲解Android对自己的应用代码进行混淆加密防止反编译
- Android RSA加密 在服务端进行RSA解密不成功的问题
- 我的Android进阶之旅------)Android采用AES+RSA的加密机制对http请求进行加密
- 关于JAVA中RSA加签解签,私钥加密公钥解密和公钥加密私钥解密代码步骤
- 详细讲解Android对自己的应用代码进行混淆加密防止反编译
- RSA之php私钥签名与android、ios公钥加密
- Android在NDK层使用OpenSSL进行RSA加密
- Android使用KeyStore对数据进行加密的示例代码
- 我的Android进阶之旅------>Android采用AES+RSA的加密机制对http请求进行加密
- 详细讲解Android对自己的应用代码进行混淆加密防止反编译
- 关于Android 使用服务器公钥RSA加密 数据到服务器RSA解密报错问题
- 我的Android进阶之旅------>Android采用AES+RSA的加密机制对http请求进行加密
- Android 登录密码信息进行RSA加密示例
- rsa从cer证书中拿公钥进行加密
- Android加密方式的实现代码MD5-RSA-DES
- 关于JAVA中RSA加签解签,私钥加密公钥解密和公钥加密私钥解密代码详解
- Android RSA 公钥加密、解密
- Android使用RSA加密和解密的示例代码
- C#使用RSA进行私钥加密公钥解密