(一)RSA加密(字串大小不限)

private byte[] encryptData;

public RSAEncryptEngin (byte[] data, RSAPublicKey publicKey){
    
  try {
   Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
   cipher.init(Cipher.ENCRYPT_MODE, publicKey);
   
   ByteArrayOutputStream out = new ByteArrayOutputStream(53);
   int blockSize = 53;
   int thisBlockSize;
   for(int i = 0; i<data.length; i+=blockSize){
    
     
         if (data.length - i <= blockSize)
         {
              thisBlockSize = data.length - i;
          }
          else
          {
               thisBlockSize = blockSize;
          }
    
         out.write(cipher.doFinal(data, i, thisBlockSize));

   }
   encryptData = out.toByteArray();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }

 

 

(二)RSA解密,根據key長度判斷所需要的Block Size

 private byte[] plainText;
 private int blockSize;
 private static int KEY512 = 64;
 private static int KEY1024 = 128;
 private static int KEY2048 = 256;

public RSADecryptEngin(byte[] encryptData, RSAPrivateKey privateKey){

int keyLength = privateKey.getModulus().bitLength();
  Cipher rsaCipher;
  
  if(keyLength == 512){
   blockSize = this.KEY512;
  }
  if(keyLength == 1024){
   blockSize = this.KEY1024;
  }
  if(keyLength == 2048){
   blockSize = this.KEY2048;
  }
  try {
   ByteArrayOutputStream out = new ByteArrayOutputStream(blockSize);
   rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
   rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);
   
   int j = 0;
   
            while (encryptData.length - j * blockSize > 0) {
                out.write(rsaCipher.doFinal(encryptData, j * blockSize, blockSize));
                j++;
            }

   
         plainText = out.toByteArray();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

public String getUTF8_PlainText() throws UnsupportedEncodingException{
  return new String(plainText,"UTF-8");
 }

 

 

(三)RSA Generate Key pair

private RSAPrivateKey priKey;
private RSAPublicKey pubKey;

public GenerateKeyPair(int keysize){
       
        Security.addProvider(new BouncyCastleProvider());
        KeyPairGenerator generator;
        try {
            generator = KeyPairGenerator.getInstance("RSA");
            generator.initialize(keysize);
            KeyPair keyPair = generator.generateKeyPair();
            priKey = (RSAPrivateKey)keyPair.getPrivate();
            pubKey = (RSAPublicKey)keyPair.getPublic();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
                   
    }

備註一:在JRE/lib/security/java.security以及JDK/jre/lib/security/java.security加入
"security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider"

arrow
arrow
    全站熱搜

    阿智 發表在 痞客邦 留言(8) 人氣()