bouncycastle java 中 AES/CBC/ISO10126Padding 模式的问题
当我运行此代码时,每次运行时,控制台上打印的“加密字符串:”都是不同的。即加密和解密模式为 AES/CBC/ISO10126Padding 但加密的字符串确实解密成功。 当我在“AES”模式下运行相同的代码时,控制台上打印的“加密字符串:”每次都是相同的。请您告诉我为什么在 AES/CBC/ISO10126Padding 的情况下,每次运行时输出都是不同的。
import java.io.FileOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
//import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
public class AES_Encrypt_Decrypt_Bouncy_Castle {
public static SecretKeySpec getKey() {
try {
String keyFromfile = "ZoyNMZzsOYh7BxwcgJseBji95hmVTlgKe/9KFY44Jzg=";
byte[] keyBytes = Base64.decode(keyFromfile.getBytes());
byte[] finalKeyBytes = new byte[32];
for (int i = 0; i < 32; i++) {
finalKeyBytes[i] = keyBytes[i];
}
System.out.println("keyBytes="+keyBytes.length);
System.out.println("Key is "+ keyBytes);
for(int j=32;j<keyBytes.length;j++){
System.out.println("keyBytes="+keyBytes[j]);
}
SecretKeySpec skeySpec = new SecretKeySpec(finalKeyBytes, "AES");
return skeySpec;
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) throws Exception {
SecretKeySpec skeySpec = getKey();
byte [] iv = Base64.decode("AWNCK2F/9llI0Rs+dQB36Q==");
IvParameterSpec initializationVector = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initializationVector);
String message = "Brian12345678";
byte[] encrypted = cipher.doFinal(message.getBytes());
System.out.println("Decoded plain text = "+new String (encrypted));
byte[] encodedEncryptedText = Base64.encode(encrypted);
System.out.println("encrypted string: " + new String (encodedEncryptedText));
Cipher cipher2 = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());
cipher2.init(Cipher.DECRYPT_MODE, skeySpec,initializationVector);
byte[] original = cipher2.doFinal(Base64.decode(encodedEncryptedText));
String originalString = new String(original);
System.out.println("Original string: " + originalString);
}
}
When I run this code the "encrypted string:" printed on the console is different each time I run. i.e. the encryption and decryption mode is AES/CBC/ISO10126Padding However the encrypted string does decrypt successfully.
When I run the same code in 'AES' mode the "encrypted string:" printed on the console is the same each time. Please can you tell me why in the case of AES/CBC/ISO10126Padding the output is diff each time i run it.
import java.io.FileOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
//import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
public class AES_Encrypt_Decrypt_Bouncy_Castle {
public static SecretKeySpec getKey() {
try {
String keyFromfile = "ZoyNMZzsOYh7BxwcgJseBji95hmVTlgKe/9KFY44Jzg=";
byte[] keyBytes = Base64.decode(keyFromfile.getBytes());
byte[] finalKeyBytes = new byte[32];
for (int i = 0; i < 32; i++) {
finalKeyBytes[i] = keyBytes[i];
}
System.out.println("keyBytes="+keyBytes.length);
System.out.println("Key is "+ keyBytes);
for(int j=32;j<keyBytes.length;j++){
System.out.println("keyBytes="+keyBytes[j]);
}
SecretKeySpec skeySpec = new SecretKeySpec(finalKeyBytes, "AES");
return skeySpec;
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) throws Exception {
SecretKeySpec skeySpec = getKey();
byte [] iv = Base64.decode("AWNCK2F/9llI0Rs+dQB36Q==");
IvParameterSpec initializationVector = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initializationVector);
String message = "Brian12345678";
byte[] encrypted = cipher.doFinal(message.getBytes());
System.out.println("Decoded plain text = "+new String (encrypted));
byte[] encodedEncryptedText = Base64.encode(encrypted);
System.out.println("encrypted string: " + new String (encodedEncryptedText));
Cipher cipher2 = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());
cipher2.init(Cipher.DECRYPT_MODE, skeySpec,initializationVector);
byte[] original = cipher2.doFinal(Base64.decode(encodedEncryptedText));
String originalString = new String(original);
System.out.println("Original string: " + originalString);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自维基百科填充页面:
这可以解释为什么加密的数据不同,但可以成功解密。
From the wikipedia padding page:
That could explain why the encrypted data is different, but can decrypt successfully.