[英]AES decryption using java
I have been looking into this for a while now but just cant seem to spot the problem. 我已经研究了一段时间,但似乎无法发现问题。 I have included the method in question, and the given output. 我已经包含了有问题的方法和给定的输出。 No errors exist, just simply an inaccurate result returned from the decrypting back of encrypted/encoded data. 没有错误存在,仅是解密加密/编码数据后返回的错误结果。
Can anyone spot the mis-coding? 谁能发现错误的编码?
METHOD 方法
public static void register(){
try{
// CREATE KEY AND CIPHER
byte[] key = username.getBytes();
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
// ENCRYPT DATA
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherText = cipher.doFinal(password.getBytes("UTF8"));
// ENCODE DATA
String encodedString = new String(Base64.encodeBase64(cipherText), "UTF-8");
// SAVE VARIABLE
p = new String(encodedString);
// PRINT DATA
System.out.println("PLAINTEXT KEY: " + username);
System.out.println("MODIFIED PASSWORD: " + password);
System.out.println("ENCRYPTED PASSWORD: " + new String(cipherText));
System.out.println("ENCODED PASSWORD: " + encodedString);
System.out.println("P (ENCODED): " + p);
System.out.println("");
// SAVE TO DISK
try {
File file = new File("C://Welcome/License.txt");
file.getParentFile().mkdirs();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(new String(p));
bw.close();
}
catch(FileNotFoundException ex){
ex.printStackTrace();
}
catch(IOException ex){
ex.printStackTrace();
}
p = "";
// CREATE A FOLDER FOR FILES
try {
File dir = new File("C://IronFortress/Files");
dir.mkdir();
}
catch(Exception e){
e.printStackTrace();
}
// READ DATA FROM DISK
String fileName = "C:/Welcome/License.txt";
String line0 = null;
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
if((line0 = br.readLine()) != null){
p = (line0);
}
br.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
// DECODE PASSWORD
String decodedString = new String(Base64.decodeBase64(p));
// CREATE KEY AND CIPHER
key = username.getBytes();
Cipher cipher2 = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey2 = new SecretKeySpec(key, "AES");
// DECRYPT PASSWORD
cipher2.init(Cipher.DECRYPT_MODE, secretKey2);
byte[] cipherText2 = cipher.doFinal(decodedString.getBytes("UTF8"));
// PRINT DATA
System.out.println("P (DECODED): " + p);
System.out.println("ENCODED PASSWORD: " + decodedString);
System.out.println("DECRYPTED PASSWORD: " + new String(cipherText2));
}
catch (Exception e){
e.printStackTrace();
}
}
OUTPUT 输出值
KEY: mmmmmmmmmmmmmmmm
PASSWORD: mxxxxxxxxxxxxxxx
ENCRYPTED PASSWORD: CvsoR—:bÜjçÆä†wÖ×_uK)¼i">ŠÏMrE
ENCODED PASSWORD: Q3Zzb1KXOmLc58bkhnfW1191SwQpvGkiPhmKz01yRQc=
P (ENCODED): Q3Zzb1KXOmLc58bkhnfW1191SwQpvGkiPhmKz01yRQc=
P (DECODED): Q3Zzb1KXOmLc58bkhnfW1191SwQpvGkiPhmKz01yRQc=
ENCODED PASSWORD: CvsoR—:bÜçÆä†wÖ×_uK)¼i">ŠÏMrE
DECRYPTED PASSWORD: ƒÑjQ:y¯€n»×l¬JÍœ£,cá±ß…c}˜àzð`{úÀØ…¢1vŸlЩÚ
The problem is as with the previous question: 问题与上一个问题相同:
String decodedString = new String(Base64.decodeBase64(p));
p
is the Base64-encoded ciphertext. p
是Base64编码的密文。 If you decode it, you get a ciphertext with arbitrary bytes. 如果对它进行解码,则会得到带有任意字节的密文。 When you make a string out of the bytes, you silently throw away some bytes which makes the plaintext unrecoverable. 当您从字节中取出字符串时,您会无声地丢弃一些字节,从而使纯文本无法恢复。
Try: 尝试:
byte[] decoded = Base64.decodeBase64(p);
byte[] cipherText2 = cipher.doFinal(decoded); // this isn't ciphertext, but plaintext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.