由於android和後臺進行傳輸數據爲了數據的安全我們一般都會對數據進行加密處理,一般比較常用的都是對稱加密(DES)。這樣可以防止攻擊者截獲網絡包獲取數據。但是這這種方式也是有風險,就是如果攻擊者反編譯了你的代碼,就可以獲取你的祕鑰那麼就可以解析你的數據了。
所以我們在傳輸及其重要的數據的時候會使用非對稱加密,非對稱加密分爲公鑰和私鑰,用公鑰進行加密,用祕鑰進行解密。一般是客戶端用公鑰鑰把數據加密後傳給後臺,後臺再用私鑰進行解密。這樣就算攻擊者反編譯了你的代碼也無法解密
使用方式如下
public class TestRAS {
/** 指定加密算法爲RSA */
private static final String ALGORITHM = "RSA";
/** 密鑰長度,用來初始化 */
private static final int KEYSIZE = 512;
static String privateKeyString;
static String publicKeyString;
/**
* 得到祕鑰的方法
* 生成的公鑰和私鑰記錄下來用於以後使用
* @throws Exception
*/
private static void generateKeyPair() throws Exception {
/** RSA算法要求有一個可信任的隨機數源 */
SecureRandom secureRandom = new SecureRandom();
/** 爲RSA算法創建一個KeyPairGenerator對象 */
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
/** 利用上面的隨機數據源初始化這個KeyPairGenerator對象 */
keyPairGenerator.initialize(KEYSIZE, secureRandom);
/** 生成密匙對 */
KeyPair keyPair = keyPairGenerator.generateKeyPair();
/** 得到公鑰 */
PublicKey publicKey = keyPair.getPublic();
/** 得到私鑰 */
PrivateKey privateKey = keyPair.getPrivate();
/**得到字符串*/
privateKeyString=getKeyString(privateKey);
publicKeyString=getKeyString(publicKey);
System.out.println("公鑰:"+publicKeyString);
System.out.println("私鑰:"+privateKeyString);
}
/**
* 得到公鑰
*
* @param key
* 密鑰字符串(經過base64編碼)
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 得到私鑰
*
* @param key
* 密鑰字符串(經過base64編碼)
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 得到密鑰字符串(經過base64編碼)
*
* @return
*/
public static String getKeyString(Key key) throws Exception {
byte[] keyBytes = key.getEncoded();
String s = (new BASE64Encoder()).encode(keyBytes);
return s;
}
/**
* 加密方法
* @param source 源數據
* @return
* @throws Exception
*/
public static String encrypt(String source) throws Exception {
generateKeyPair();
PublicKey publicKey = null;
ObjectInputStream ois = null;
publicKey=getPublicKey(publicKeyString);
/** 得到Cipher對象來實現對源數據的RSA加密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] b = source.getBytes();
/** 執行加密操作 */
byte[] b1 = cipher.doFinal(b);
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b1);
}
/**
* 解密算法
* @param cryptograph 密文
* @return
* @throws Exception
*/
public static String decrypt(String cryptograph) throws Exception {
PrivateKey privateKey;
privateKey=getPrivateKey(privateKeyString);
/** 得到Cipher對象對已用公鑰加密的數據進行RSA解密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(cryptograph);
/** 執行解密操作 */
byte[] b = cipher.doFinal(b1);
return new String(b);
}
public static void main(String[] args) throws Exception {
String source = "恭喜發財!";
System.out.println("準備用公鑰加密的字符串爲:" + source);
String cryptograph = encrypt(source);
System.out.print("用公鑰加密後的結果爲:" + cryptograph);
System.out.println();
String target = decrypt(cryptograph);
System.out.println("用私鑰解密後的字符串爲:" + target);
System.out.println();
}
}