所以我想弄清楚如何在java中加密和解密RSA。加密通過javas API完成,並通過biginteger解密。我完成了,但是biginteger有時會給我奇怪的輸出。這是我的代碼:使用java RSA加密並使用BigInteger解密
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class cryptoAndBigIntegerFIX {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException{
Security.addProvider(new BouncyCastleProvider());
System.out.println("input <> encrypted <> decrypted");
cryptoAndBigIntegerFIX.keygen();
BigInteger encryptbytes;
BigInteger decryptbytes;
//Multiple tests with powers of 3 for some reason :D
for(int i=1;i<1000;i*=3){
encryptbytes = cryptoAndBigIntegerFIX.encrypt(new BigInteger(""+i));
System.out.print(i + " <> " + encryptbytes.intValue() + " <> ");
decryptbytes = cryptoAndBigIntegerFIX.decrypt(encryptbytes);
System.out.println(decryptbytes.intValue());
}
}
public static RSAPrivateKey priv;
public static RSAPublicKey pub;
public static void keygen() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException{
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(512);
KeyPair keyPair = generator.generateKeyPair();
priv = (RSAPrivateKey) keyPair.getPrivate();
pub = (RSAPublicKey) keyPair.getPublic();
}
//Encrypt with javas API
public static BigInteger encrypt(BigInteger bg) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException{
byte[] encoded;
Cipher cipher=Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, pub);
encoded=cipher.doFinal(bg.toByteArray());
return new BigInteger(encoded);
}
//Decrypt manually
public static BigInteger decrypt(BigInteger bg){
BigInteger decoded = bg.modPow(priv.getPrivateExponent(),priv.getModulus());
return decoded;
}
}
這使輸出爲:
input <> encrypted <> decrypted
1 <> 1 <> 1
3 <> 1088098617 <> 3
9 <> 1947497039 <> 9
27 <> -1665331145 <> 27
81 <> -1064046970 <> 81
243 <> -599005266 <> 243
729 <> -1534949160 <> 729
這是正確的,因爲我想加密和解密三種權力。 但有時它會給出錯誤的輸出,如:
input <> encrypted <> decrypted
1 <> 1 <> 1
3 <> 1693488667 <> 3
9 <> -924345856 <> 9
27 <> 777525903 <> 144224668
81 <> -1602799071 <> 765474161
243 <> -227258229 <> 243
729 <> 1097077312 <> 296615835
這很奇怪嗎?任何想法我的代碼有什麼問題?這是biginteger還是我生成密鑰的方式?
爲什麼? 'Cipher'提供了一個解密模式以及一個加密模式。 – EJP
這是我的講師給出的「任務」,以證明他們是同一件事。他回覆給我說我也需要做一個JUNIT測試。他沒有對我在上面顯示的這些錯誤提出任何意見,所以也許他沒有注意到這些錯誤,因爲他編譯時沒有得到任何錯誤的答案。事情是,當我有時得到錯誤的答案時,我甚至不能做出JUNIT測試用例。 – David