利用第三方加密库“Bouncy Castle”实现J2ME平台RSA加解密

     最近在做一个J2ME项目,需要用到RSA加解密,查询很多资料,最终找到“Bouncy Castle”才解决问题。

一.约束&思路:

1)RSA算法本身就是有长度限制的,1024位的RSA最多能加密长117字节的分组数据。
2)Bouncy Castle提供四种RSA的加密编码,如下:
RSA/NONE/NoPadding (支持到128个字符)
RSA/NONE/PKCS1Padding (支持到107个字符)
RSA/NONE/OAEPPadding (支持到86个字符)
RSA/NONE/ISO9796-1Padding

3)由于传输的数据大于117个字节,所以采用分组加解密的方法解决此问题,及把传输的数据分成N个117个字节后再传输。

4)代码:采用编码:RSA/NONE/PKCS1Padding

                        /*
			 * @param plainText 加密字符串
			 * @param key  公钥
			 * @return
			 * @throws InvalidCipherTextException
			 */
			public static byte[] encrypt(String plainText, CipherParameters key) throws InvalidCipherTextException {
		        byte[] data = plainText.getBytes();
				RSAEngine baseEngine = new RSAEngine();
		        PKCS1Encoding engine = new PKCS1Encoding(baseEngine);
		        engine.init(true, key);
		 
		        int inputBlockSize = engine.getInputBlockSize();
		        int input_numWholeBlocks = data.length / inputBlockSize;
		        int input_sizeLastBlock = data.length % inputBlockSize;
		        int input_numBlocks;
		        if (input_sizeLastBlock > 0) {
		        	input_numBlocks = input_numWholeBlocks + 1;
		        } else {
		        	input_numBlocks = input_numWholeBlocks;
		        }
		        
		        int outPutBlockSize = engine.getOutputBlockSize();		 
		      
		        byte[] ciphertext = new byte[input_numBlocks * outPutBlockSize];
		        byte[] aux;
		     
		        for (int i = 0; i < input_numWholeBlocks; i++) {
		            aux = engine.processBlock(data, i * inputBlockSize, inputBlockSize);
		            System.arraycopy(aux, 0, ciphertext, i * outPutBlockSize, aux.length);
		        }
		        
		        int WN = input_numBlocks - input_numWholeBlocks;
		        if(WN==1){
		            aux = engine.processBlock(data, input_numWholeBlocks * inputBlockSize, input_sizeLastBlock);
		            System.arraycopy(aux, 0, ciphertext, input_numWholeBlocks * outPutBlockSize, aux.length);
		        }
		 
		        return ciphertext;
		    }
			/**
			 * 
			 * 分组解密
			 * @param data 解密数据
			 * @param key  私钥
			 * @return
			 * @throws InvalidCipherTextException
			 */
			public static String decrypt(byte[] data, CipherParameters key) throws InvalidCipherTextException {
				RSAEngine baseEngine = new RSAEngine();
		        PKCS1Encoding engine = new PKCS1Encoding(baseEngine);
		        engine.init(false, key);
		 
		        int inputBlockSize = engine.getInputBlockSize();
		        int input_numWholeBlocks = data.length / inputBlockSize;

		        int outPutBlockSize = engine.getOutputBlockSize();		 
		        
		        byte[] ciphertext = new byte[input_numWholeBlocks * outPutBlockSize];
		        byte[] aux;		       
		        
		        for (int i = 0; i < input_numWholeBlocks; i++) {
		        	try {
		        		 aux = engine.processBlock(data, i * inputBlockSize, inputBlockSize);
		        		 System.arraycopy(aux, 0, ciphertext, i * outPutBlockSize, aux.length);
					} catch (Exception e) {
						break;
					}  
		        }
		
		        return new String(ciphertext).trim();
		    }

你可能感兴趣的:(算法)