(一)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"

創作者介紹
創作者 阿智 的頭像
阿智

阿智Family遊記

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


留言列表 (8)

發表留言
  • thief
  • 請問一下
    文章的最後一段

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

    該如何加入,我爬了文,但是還是不了解...
  • 阿智
  • about java.security

    Hello:
    主要是要找到該檔案"java.security",例如在我的電腦裡面,是放在"C:\Program Files\Java\jre1.5.0_17\lib\security"與"C:\Program Files\Java\jdk1.5.0_17\jre\lib\security"中。
    1. 打開該檔案,可以找到預設的security.provider,如下所示(共六個):
    security.provider.1=sun.security.provider.Sun
    security.provider.2=sun.security.rsa.SunRsaSign
    security.provider.3=com.sun.net.ssl.internal.ssl.Provider
    security.provider.4=com.sun.crypto.provider.SunJCE
    security.provider.5=sun.security.jgss.SunProvider
    security.provider.6=com.sun.security.sasl.Provider

    2. 只要加上"security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider"即可嘍!

    Good Luck
  • sally
  • .您好:請問有用c寫的rsa加密和解密嗎?
  • 阿智
  • C寫的RSA加解密,可以參考OpenSSL或是Crypto++這兩種Open Source嘍!
  • abck0123
  • 你好!我想問為何BLOCK SIZE是53?
  • LINE行銷軟體
  • r02mn6
    網﹉頁﹉設○計﹎ wWw.SEOuL.cOm.tw
    tplQ2C
  • fg24r0
  • ﹎喔~原來-~買﹂性﹋藥﹎品﹎可﹋以﹂`~這麼簡~單﹂ㄛ﹋

    577UP.Com
  • f23n2y
  • 謝謝,受教嘍。﹂
    還﹋是﹍要〇去○ aAAsHOps。﹎coM 品﹌質﹉不◎錯,老婆很喜歡。