2013-10-27 131 views
4

我正在做一个简单的程序来使用Java中的RSA算法进行加密/解密。我创建一个密码对象如下:使用Java的RSA加密/解密

//Create a Cipher object 
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding"); 

我做加密通过调用加密功能:

String cipher=encrypt(textByte, pair, rsaCipher); 
System.out.println("The Encryption using RSA Algorithm : "+cipher); 

和解密为:

//Decryption 
String plain=decrypt(Base64.decodeBase64(cipher),pair, rsaCipher); 
System.out.println("The Decryption using RSA Algorithm : "+plain); 

当我显示输出,解密输出返回原始文本前的长空格: enter image description here

但是,当我将用于创建密码对象的代码编辑为: //创建密码对象 密码rsaCipher = Cipher.getInstance(“RSA”);

即,删除操作模式和填充参数,问题得到解决,输出变为: enter image description here

问题出在哪里。在第一种情况下(当空间出现时),我指定了NoPadding?为什么空格出现在解密的消息中?即使我使用填充,我预计这不应该发生。

编辑: 这是加密和解密方法:

public static String encrypt(byte[] textBytes, KeyPair pair, Cipher rsaCipher) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException 
{ 
    //get the public key 
    PublicKey pk=pair.getPublic(); 


    //Initialize the cipher for encryption. Use the public key. 
    rsaCipher.init(Cipher.ENCRYPT_MODE, pk); 

    //Perform the encryption using doFinal 
    byte[] encByte = rsaCipher.doFinal(textBytes); 

    // converts to base64 for easier display. 
    byte[] base64Cipher = Base64.encodeBase64(encByte); 

    return new String(base64Cipher); 
}//end encrypt 

public static String decrypt(byte[] cipherBytes, KeyPair pair, Cipher rsaCipher) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException 
{ 
    //get the public key 
    PrivateKey pvk=pair.getPrivate(); 

    //Create a Cipher object 
    //Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding"); 

    //Initialize the cipher for encryption. Use the public key. 
    rsaCipher.init(Cipher.DECRYPT_MODE, pvk); 

    //Perform the encryption using doFinal 
    byte[] decByte = rsaCipher.doFinal(cipherBytes); 

    return new String(decByte); 

}//end decrypt 
+0

你能告诉我们你的'encrypt'和'decrypt'方法吗? – Craigy

回答

5

你的问题确实与填充。对于安全的RSA功能,某些填充(实际上PKCS#1 1.5或OAEP填充)是必需的。此外,它需要找到加密的纯文本的开始和结束。

RSA的模幂运算使用大整数进行。然后这些操作的结果被表示为八位组串。这些八位字符串基本上是一个整数的大端,无符号,固定长度表示。这些整数用00值字节填充(这在RSA标准中称为I2OS原语)。所以你看到的是模幂运算的结果,00填充仍然存在。

长话短说,总是使用填充方案。现在,OAEP会更可取。将其与混合加密方案一起使用,或者使用更高级别的容器格式,例如CMS或PGP。

0

RSA的模幂运算使用大整数进行。然后这些操作的结果被表示为八位组串。这些八位字符串基本上是一个整数的大端,无符号,固定长度表示。这些整数用00值字节填充(这在RSA标准中称为I2OS原语)。所以你看到的是模幂运算的结果,00填充仍然存在。

-2

希望这会有所帮助! :)

import java.util.*; 
import java.math.*; 
class RSA 
{ 
public static void main(String args[]) 
{ 
BigInteger one, p, q, E, D, n,P,Q; 
Scanner s = new Scanner(System.in); 
Scanner t = new Scanner(System.in); 
System.out.println("Enter A's prime number!"); 
p = s.nextBigInteger(); 
System.out.println("Enter B's prime number!"); 
q = s.nextBigInteger(); 
n = p.multiply(q); 
P = p.subtract(BigInteger.ONE); 
Q = q.subtract(BigInteger.ONE); 
int x = 0; 
do 
{ 
System.out.println("Enter Public key "); 
E =s.nextBigInteger(); 
if(((P.gcd(E)).equals(BigInteger.ONE))&&((Q.gcd(E)).equals(BigInteger.ONE))) 
{x++;} 
}while(x==0); 
for(int i = 1;;i++) 
{ 
D=new BigInteger(String.valueOf(i)); 
if(((D.multiply(E)).mod(P.multiply(Q))).equals(BigInteger.ONE)) 
break; 
} 
System.out.println("Enter Plain text!"); 
String in = "", out ="", text = t.nextLine(); 
for(int i = 0;i < text.length();i++){ 
BigInteger T = new BigInteger(String.valueOf((int)(text.charAt(i)))), O, TF; 
O = T.modPow(E,n); 
out += (char)O.intValue(); 
TF = O.modPow(D,n); 
in += (char)TF.intValue(); 
} 
System.out.println("Encrypted text : " + out); 
System.out.println("Decrypted text : "+ in); 
} 
} 
+1

你能否解释创建这个需要的改变来帮助OP进一步处理他们的问题? –