[英]I have javax.crypto.BadPaddingException: RSA Cryptography
當消息太長時,我對RSA加密有問題。
我的密碼:
public static byte[] encrypt(byte[] dataToEncrypt, PublicKey pubk){
try{
System.out.println("in encrypt");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubk);
byte[] encrypted = new byte[dataToEncrypt.length];
System.out.println("Imagebyte length: " + dataToEncrypt.length);
byte[] temp = new byte[53];
byte[] temp2 = null;
int x, y, z = 0;
int repeats = dataToEncrypt.length / 53;
System.out.println("Iterations: " + repeats);
for (z = 0; z < repeats; z++) {
System.out.println("Iteration number: " + z);
int offset = z * 53;
for (x = 0; x < 53; x++) {
temp[x] = (byte) dataToEncrypt[offset + x];
}
temp2 = cipher.doFinal(temp);
for (y = 0; y < 53; y++) {
encrypted[offset + y] = (byte) temp2[y];
}
temp2 = null;
}
return encrypted;
}catch(Exception e){
return null;
}
}
public static byte[] decrypt(byte[] bytesIn, PrivateKey prvk){
try {
System.out.println("in decrypt");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println("read out" + prvk);
cipher.init(Cipher.DECRYPT_MODE, prvk);
byte[] decrypted = new byte[bytesIn.length];
System.out.println("Imagebyte length: " + bytesIn.length);
byte[] temp = new byte[53];
byte[] temp2 = null;
int x, y, z = 0;
int repeats = bytesIn.length / 53;
System.out.println("Iterations: " + repeats);
for (z = 0; z < repeats; z++) {
System.out.println("Iteration number: " + z);
int offset = z * 53;
for (x = 0; x < 53; x++) {
temp[x] = (byte) bytesIn[offset + x];
}
temp2 = cipher.doFinal(temp);
for (y = 0; y < 53; y++) {
decrypted[offset + y] = (byte) temp2[y];
}
temp2 = null;
}
return decrypted;
} catch (Exception e) {
System.err.println(e);
return null;
}
}
主要:
public static void main(String args[]) {
MsgAtoB_1 msg= new MsgAtoB_1(new BigInteger(String.valueOf(System.currentTimeMillis())), "login");
byte [] tab = MySerializer.serialize(msg);
KeyPair key = Keys.generateKeyPair();
byte [] crypt = MyCipher.encrypt(tab, key.getPublic());
byte [] decrypt = MyCipher.decrypt(crypt,key.getPrivate() );
if(tab.equals(decrypt)) System.out.println("yes it's work ");
else System.out.println("KOOOOOOOOOOO");
}
輸出:
run:
in encrypt
Imagebyte length: 319
Iterations: 6
Iteration number: 0
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5
in decrypt
read outsun.security.rsa.RSAPrivateCrtKeyImpl@fff5bef2
Imagebyte length: 319
Iterations: 6
Iteration number: 0
javax.crypto.BadPaddingException: Data must start with zero
KOOOOOOOOOOO
您指定了填充模式,這意味着密碼對輸入數據進行填充並返回不同(更大)長度的數組。
特別是,兩種方法中的以下行均從密碼接收數據,但不以任何方式使用返回數據的長度:
temp2 = cipher.doFinal(temp);
加密時,基本上是將每個“塊”都截斷為53個字節(選擇數字的方式和方式是另一個問題)。 解密時,您BadPaddingException
截斷后的數據傳遞給密碼,該密碼可能會導致BadPaddingException
。
通過在某種調整大小的數組(例如, ByteArrayOutputStream
)中收集各個加密的塊並存儲每個塊的長度以在解碼時能夠分割流的方式來“修復”代碼是可能的,但最終會實現不佳“種流”密碼。
您應該考慮使用提供即時流功能的其他密碼(例如AES)和其他密碼模式(例如CTR)。
您好的解決方案是:使用Java和RSA 加密和解密大數據使用Java和RSA加密和解密大數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.