在Android使用RSA 演算法加解密

來源:互聯網
上載者:User

首先我們可以從看到:明文--->公開金鑰--->密文   密文-->密鑰-->明文

    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. }      
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.