AES-128 encryption in Java
up vote
1
down vote
favorite
I have tried to encrypt some plaintext, which is in bytes, with a key, which is in bytes. However, the output I am getting is not what I am expecting.
public class AES {
public static byte encrypt(byte plainText, byte key) {
try {
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte cipherText = cipher.doFinal(plainText);
return cipherText;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String byteToHex(byte hash) {
StringBuilder sb = new StringBuilder(hash.length * 2);
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
public static void main(String args) {
byte plaintext = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x58,0x63,(byte)0xaa,(byte)0xbb,(byte)0xcc,(byte)0xdd,(byte)0xee,(byte)0xff};
byte key = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
byte encrypted = encrypt(plaintext, key);
System.out.println("Encrypted String : " + byteToHex(encrypted));
}
}
The output I am getting is: 814064943fe05668da1f3d2269a4ee22954f64f2e4e86e9eee82d20216684899
while I am expecting 69c4e0d86a7b0430d8cdb78070b4c55a
. I am using the plaintext and key from here
java encryption aes
|
show 1 more comment
up vote
1
down vote
favorite
I have tried to encrypt some plaintext, which is in bytes, with a key, which is in bytes. However, the output I am getting is not what I am expecting.
public class AES {
public static byte encrypt(byte plainText, byte key) {
try {
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte cipherText = cipher.doFinal(plainText);
return cipherText;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String byteToHex(byte hash) {
StringBuilder sb = new StringBuilder(hash.length * 2);
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
public static void main(String args) {
byte plaintext = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x58,0x63,(byte)0xaa,(byte)0xbb,(byte)0xcc,(byte)0xdd,(byte)0xee,(byte)0xff};
byte key = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
byte encrypted = encrypt(plaintext, key);
System.out.println("Encrypted String : " + byteToHex(encrypted));
}
}
The output I am getting is: 814064943fe05668da1f3d2269a4ee22954f64f2e4e86e9eee82d20216684899
while I am expecting 69c4e0d86a7b0430d8cdb78070b4c55a
. I am using the plaintext and key from here
java encryption aes
Do you understand what PKCS5 padding does? A hint: I will always make the result longer. So an input of 16 bytes can never result in an output of 16 bytes.
– Codo
Nov 9 at 22:44
No, I have removed it, and use the default. However, I would like to know why I am getting the incorrect output
– Bab
Nov 9 at 22:46
I have also tried to print the encrypted byte array withArray.toString(encrypted )
– Bab
Nov 9 at 22:51
The default padding mode of your AES provider is most likely "PKCS5". So you haven't changed anything... Read about it and you have a good idea why your result is different.
– Codo
Nov 9 at 22:53
Have read about it, and I found out that I had to use NoPadding, which has given me the proper format. However, I am still getting the incorrect output.
– Bab
Nov 9 at 23:00
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have tried to encrypt some plaintext, which is in bytes, with a key, which is in bytes. However, the output I am getting is not what I am expecting.
public class AES {
public static byte encrypt(byte plainText, byte key) {
try {
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte cipherText = cipher.doFinal(plainText);
return cipherText;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String byteToHex(byte hash) {
StringBuilder sb = new StringBuilder(hash.length * 2);
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
public static void main(String args) {
byte plaintext = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x58,0x63,(byte)0xaa,(byte)0xbb,(byte)0xcc,(byte)0xdd,(byte)0xee,(byte)0xff};
byte key = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
byte encrypted = encrypt(plaintext, key);
System.out.println("Encrypted String : " + byteToHex(encrypted));
}
}
The output I am getting is: 814064943fe05668da1f3d2269a4ee22954f64f2e4e86e9eee82d20216684899
while I am expecting 69c4e0d86a7b0430d8cdb78070b4c55a
. I am using the plaintext and key from here
java encryption aes
I have tried to encrypt some plaintext, which is in bytes, with a key, which is in bytes. However, the output I am getting is not what I am expecting.
public class AES {
public static byte encrypt(byte plainText, byte key) {
try {
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte cipherText = cipher.doFinal(plainText);
return cipherText;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String byteToHex(byte hash) {
StringBuilder sb = new StringBuilder(hash.length * 2);
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
public static void main(String args) {
byte plaintext = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x58,0x63,(byte)0xaa,(byte)0xbb,(byte)0xcc,(byte)0xdd,(byte)0xee,(byte)0xff};
byte key = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
byte encrypted = encrypt(plaintext, key);
System.out.println("Encrypted String : " + byteToHex(encrypted));
}
}
The output I am getting is: 814064943fe05668da1f3d2269a4ee22954f64f2e4e86e9eee82d20216684899
while I am expecting 69c4e0d86a7b0430d8cdb78070b4c55a
. I am using the plaintext and key from here
java encryption aes
java encryption aes
edited Nov 9 at 22:44
asked Nov 9 at 22:31
Bab
12910
12910
Do you understand what PKCS5 padding does? A hint: I will always make the result longer. So an input of 16 bytes can never result in an output of 16 bytes.
– Codo
Nov 9 at 22:44
No, I have removed it, and use the default. However, I would like to know why I am getting the incorrect output
– Bab
Nov 9 at 22:46
I have also tried to print the encrypted byte array withArray.toString(encrypted )
– Bab
Nov 9 at 22:51
The default padding mode of your AES provider is most likely "PKCS5". So you haven't changed anything... Read about it and you have a good idea why your result is different.
– Codo
Nov 9 at 22:53
Have read about it, and I found out that I had to use NoPadding, which has given me the proper format. However, I am still getting the incorrect output.
– Bab
Nov 9 at 23:00
|
show 1 more comment
Do you understand what PKCS5 padding does? A hint: I will always make the result longer. So an input of 16 bytes can never result in an output of 16 bytes.
– Codo
Nov 9 at 22:44
No, I have removed it, and use the default. However, I would like to know why I am getting the incorrect output
– Bab
Nov 9 at 22:46
I have also tried to print the encrypted byte array withArray.toString(encrypted )
– Bab
Nov 9 at 22:51
The default padding mode of your AES provider is most likely "PKCS5". So you haven't changed anything... Read about it and you have a good idea why your result is different.
– Codo
Nov 9 at 22:53
Have read about it, and I found out that I had to use NoPadding, which has given me the proper format. However, I am still getting the incorrect output.
– Bab
Nov 9 at 23:00
Do you understand what PKCS5 padding does? A hint: I will always make the result longer. So an input of 16 bytes can never result in an output of 16 bytes.
– Codo
Nov 9 at 22:44
Do you understand what PKCS5 padding does? A hint: I will always make the result longer. So an input of 16 bytes can never result in an output of 16 bytes.
– Codo
Nov 9 at 22:44
No, I have removed it, and use the default. However, I would like to know why I am getting the incorrect output
– Bab
Nov 9 at 22:46
No, I have removed it, and use the default. However, I would like to know why I am getting the incorrect output
– Bab
Nov 9 at 22:46
I have also tried to print the encrypted byte array with
Array.toString(encrypted )
– Bab
Nov 9 at 22:51
I have also tried to print the encrypted byte array with
Array.toString(encrypted )
– Bab
Nov 9 at 22:51
The default padding mode of your AES provider is most likely "PKCS5". So you haven't changed anything... Read about it and you have a good idea why your result is different.
– Codo
Nov 9 at 22:53
The default padding mode of your AES provider is most likely "PKCS5". So you haven't changed anything... Read about it and you have a good idea why your result is different.
– Codo
Nov 9 at 22:53
Have read about it, and I found out that I had to use NoPadding, which has given me the proper format. However, I am still getting the incorrect output.
– Bab
Nov 9 at 23:00
Have read about it, and I found out that I had to use NoPadding, which has given me the proper format. However, I am still getting the incorrect output.
– Bab
Nov 9 at 23:00
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
There are two things incorrect in your code.
You use PKCS5 padding while the original example doesn't use any padding.
You incorrectly copied the plain text.
So:
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
...
byte plaintext = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0x88,
(byte) 0x99, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 0xee, (byte) 0xff };
Result:
Encrypted String : 69c4e0d86a7b0430d8cdb78070b4c55a
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234103%2faes-128-encryption-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
There are two things incorrect in your code.
You use PKCS5 padding while the original example doesn't use any padding.
You incorrectly copied the plain text.
So:
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
...
byte plaintext = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0x88,
(byte) 0x99, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 0xee, (byte) 0xff };
Result:
Encrypted String : 69c4e0d86a7b0430d8cdb78070b4c55a
add a comment |
up vote
2
down vote
accepted
There are two things incorrect in your code.
You use PKCS5 padding while the original example doesn't use any padding.
You incorrectly copied the plain text.
So:
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
...
byte plaintext = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0x88,
(byte) 0x99, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 0xee, (byte) 0xff };
Result:
Encrypted String : 69c4e0d86a7b0430d8cdb78070b4c55a
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
There are two things incorrect in your code.
You use PKCS5 padding while the original example doesn't use any padding.
You incorrectly copied the plain text.
So:
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
...
byte plaintext = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0x88,
(byte) 0x99, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 0xee, (byte) 0xff };
Result:
Encrypted String : 69c4e0d86a7b0430d8cdb78070b4c55a
There are two things incorrect in your code.
You use PKCS5 padding while the original example doesn't use any padding.
You incorrectly copied the plain text.
So:
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
...
byte plaintext = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0x88,
(byte) 0x99, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 0xee, (byte) 0xff };
Result:
Encrypted String : 69c4e0d86a7b0430d8cdb78070b4c55a
answered Nov 9 at 23:11
Codo
50.2k11110147
50.2k11110147
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234103%2faes-128-encryption-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you understand what PKCS5 padding does? A hint: I will always make the result longer. So an input of 16 bytes can never result in an output of 16 bytes.
– Codo
Nov 9 at 22:44
No, I have removed it, and use the default. However, I would like to know why I am getting the incorrect output
– Bab
Nov 9 at 22:46
I have also tried to print the encrypted byte array with
Array.toString(encrypted )
– Bab
Nov 9 at 22:51
The default padding mode of your AES provider is most likely "PKCS5". So you haven't changed anything... Read about it and you have a good idea why your result is different.
– Codo
Nov 9 at 22:53
Have read about it, and I found out that I had to use NoPadding, which has given me the proper format. However, I am still getting the incorrect output.
– Bab
Nov 9 at 23:00