在Android使用RSA 算法加解密

首先我们可以从上图看到:明文--->公钥--->密文   密文-->密钥-->明文

    RSA由于public key<公钥> 和private key<密钥>的不同<也叫非对称算法加解密>,极大的提高了文件的安全性。  对称算法是同key的 ,这样很容易被人破解。

   RSA算法:

 

  1. import java.security.Key;   
  2. import java.security.KeyFactory;   
  3. import java.security.KeyPair;   
  4. import java.security.KeyPairGenerator;   
  5. import java.security.PrivateKey;   
  6. import java.security.PublicKey;   
  7. import java.security.interfaces.RSAPrivateKey;   
  8. import java.security.interfaces.RSAPublicKey;   
  9. import java.security.spec.PKCS8EncodedKeySpec;   
  10. import java.security.spec.X509EncodedKeySpec;   
  11.     
  12. import javax.crypto.Cipher;   
  13.     
  14. import sun.misc.BASE64Decoder;   
  15. import sun.misc.BASE64Encoder;   
  16.     
  17.     
  18. public class RSAHelper {   
  19.     
  20.         
  21.       public static PublicKey getPublicKey(String key) throws Exception {   
  22.             byte[] keyBytes;   
  23.             keyBytes = (new BASE64Decoder()).decodeBuffer(key);   
  24.     
  25.             X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);   
  26.             KeyFactory keyFactory = KeyFactory.getInstance("RSA");   
  27.             PublicKey publicKey = keyFactory.generatePublic(keySpec);   
  28.             return publicKey;   
  29.       }   
  30.         
  31.       public static PrivateKey getPrivateKey(String key) throws Exception {   
  32.             byte[] keyBytes;   
  33.             keyBytes = (new BASE64Decoder()).decodeBuffer(key);   
  34.     
  35.             PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);   
  36.             KeyFactory keyFactory = KeyFactory.getInstance("RSA");   
  37.             PrivateKey privateKey = keyFactory.generatePrivate(keySpec);   
  38.             return privateKey;   
  39.       }   
  40.     
  41.         
  42.       public static String getKeyString(Key key) throws Exception {   
  43.             byte[] keyBytes = key.getEncoded();   
  44.             String s = (new BASE64Encoder()).encode(keyBytes);   
  45.             return s;   
  46.       }   
  47.     
  48.     
  49.       public static void main(String[] args) throws Exception {   
  50.     
  51.             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");   
  52.             //密钥位数   
  53.             keyPairGen.initialize(1024);   
  54.             //密钥对   
  55.             KeyPair keyPair = keyPairGen.generateKeyPair();   
  56.     
  57.             // 公钥   
  58.             PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();   
  59.     
  60.             // 私钥   
  61.             PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();   
  62.     
  63.             String publicKeyString = getKeyString(publicKey);   
  64.             System.out.println("public:/n" + publicKeyString);   
  65.     
  66.             String privateKeyString = getKeyString(privateKey);   
  67.             System.out.println("private:/n" + privateKeyString);   
  68.     
  69.             //加解密类   
  70.             Cipher cipher = Cipher.getInstance("RSA");//Cipher.getInstance("RSA/ECB/PKCS1Padding");   
  71.     
  72.             //明文   
  73.             byte[] plainText = "我们都很好!邮件:@sina.com".getBytes();   
  74.     
  75.             //加密   
  76.             cipher.init(Cipher.ENCRYPT_MODE, publicKey);   
  77.             byte[] enBytes = cipher.doFinal(plainText);   
  78.     
  79.            //通过密钥字符串得到密钥   
  80.             publicKey = getPublicKey(publicKeyString);   
  81.             privateKey = getPrivateKey(privateKeyString);   
  82.     
  83.             //解密   
  84.             cipher.init(Cipher.DECRYPT_MODE, privateKey);   
  85.             byte[]deBytes = cipher.doFinal(enBytes);   
  86.     
  87.             publicKeyString = getKeyString(publicKey);   
  88.             System.out.println("public:/n" +publicKeyString);   
  89.     
  90.             privateKeyString = getKeyString(privateKey);   
  91.             System.out.println("private:/n" + privateKeyString);   
  92.     
  93.             String s = new String(deBytes);   
  94.             System.out.println(s);   
  95.     
  96.     
  97.       }   
  98.     
  99. }   

在实际开发中! 我们通过自己的modulus ,publicExponent 这两个是用户加密产生public key 而 privateExponet 用于产生private key  来开发的情况比较多。 

 

  1. import java.math.BigInteger;   
  2.   
  3. import java.security.KeyFactory;   
  4.   
  5. import java.security.PrivateKey;   
  6.   
  7. import java.security.PublicKey;   
  8.   
  9. import java.security.spec.RSAPrivateKeySpec;   
  10.   
  11. import java.security.spec.RSAPublicKeySpec;   
  12.   
  13.     
  14.   
  15. import javax.crypto.Cipher;   
  16.   
  17.     
  18.   
  19. public class RsaKey {   
  20.   
  21.     
  22.   
  23.       public PublicKey getPublicKey(String modulus,String publicExponent) throws Exception {   
  24.   
  25.             BigInteger m = new BigInteger(modulus);   
  26.   
  27.             BigInteger e = new BigInteger(publicExponent);   
  28.   
  29.             RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m,e);   
  30.   
  31.             KeyFactory keyFactory = KeyFactory.getInstance("RSA");     
  32.             PublicKey publicKey = keyFactory.generatePublic(keySpec);   
  33.   
  34.             return publicKey;   
  35.   
  36.       }   
  37.   
  38.     
  39.   
  40.       public PrivateKey getPrivateKey(String modulus,String privateExponent) throws Exception {   
  41.   
  42.             BigInteger m = new BigInteger(modulus);   
  43.   
  44.             BigInteger e = new BigInteger(privateExponent);   
  45.   
  46.             RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m,e);   
  47.   
  48.             KeyFactory keyFactory = KeyFactory.getInstance("RSA");   
  49.   
  50.             PrivateKey privateKey = keyFactory.generatePrivate(keySpec);   
  51.   
  52.             return privateKey;   
  53.   
  54.       }   
  55.   
  56.     
  57.   
  58.       public static void main(String[] args) throws Exception {   
  59.   
  60.             String modulus = "10103166745709600780215616551837697832816413714471062522342538060943596036859967333870827790358555455232243383580565187280643159050869924436081447583051139";   
  61.   
  62.             String publicExponent = "65537";   
  63.   
  64.             String privateExponet = "367979294475011322800474185715497882523349856362702385535371444397399388741997039894583483410120364529325888461124714276674612930833020362278754665756193";   
  65.   
  66.     
  67.   
  68.             RsaKey key = new RsaKey();   
  69.   
  70.             PublicKey publicKey = key.getPublicKey(modulus, publicExponent);   
  71.   
  72.             PrivateKey privateKey = key.getPrivateKey(modulus, privateExponet);   
  73.   
  74.     
  75.   
  76.     
  77.   
  78.             //加解密类   
  79.   
  80.             Cipher cipher = Cipher.getInstance("RSA");   //"RSA/ECB/PKCS1Padding"   就是:“算法/工作模式/填充模式”
  81.   
  82.     
  83.   
  84.             //明文   
  85.   
  86.             byte[] plainText = "hello world !".getBytes();   
  87.   
  88.     
  89.   
  90.             //加密   
  91.   
  92.             cipher.init(Cipher.ENCRYPT_MODE, publicKey);   
  93.   
  94.         byte[] enBytes = cipher.doFinal(plainText);   
  95.   
  96.     
  97.   
  98.     
  99.   
  100.         cipher.init(Cipher.DECRYPT_MODE, privateKey);   
  101.   
  102.             byte[]deBytes = cipher.doFinal(enBytes);   
  103.   
  104.     
  105.   
  106.             String s = new String(deBytes);   
  107.   
  108.             System.out.println(s);   
  109.   
  110.     
  111.   
  112.       }   

 

  1. }      

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值