How can I check the result of encryption (SHA1PRNG and AES) in Java?
I made a class which has a method to encrypt data using SHA1PRNG and AES algorithm.
public String encrypt(String str, String pw) throws Exception{
byte bytes = pw.getBytes();
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(bytes);
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128,sr);
SecretKey skey = kgen.generateKey();
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(),"AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte encrypted = c.doFinal(str.getBytes());
return Hex.encodeHexString(encrypted);
}
I used this method in my main.
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
enc.encrypt("abcde", "abcdfg");
System.out.println(enc);
}
My result is
com.dsmentoring.kmi.Encrytion@34340fab
just my packagename + class name + and some number ( I'm guessing this is reference address to the actual data?)
I want to see the result of my encryption like 'a13efx34123fdv....... ' like this. What do I need to add in my main method? Any advice?
java aes sha1
add a comment |
I made a class which has a method to encrypt data using SHA1PRNG and AES algorithm.
public String encrypt(String str, String pw) throws Exception{
byte bytes = pw.getBytes();
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(bytes);
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128,sr);
SecretKey skey = kgen.generateKey();
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(),"AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte encrypted = c.doFinal(str.getBytes());
return Hex.encodeHexString(encrypted);
}
I used this method in my main.
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
enc.encrypt("abcde", "abcdfg");
System.out.println(enc);
}
My result is
com.dsmentoring.kmi.Encrytion@34340fab
just my packagename + class name + and some number ( I'm guessing this is reference address to the actual data?)
I want to see the result of my encryption like 'a13efx34123fdv....... ' like this. What do I need to add in my main method? Any advice?
java aes sha1
1
You are printing the instance rather than the value it returns
– karthick
Nov 26 '18 at 8:07
@karthick How can I print the value? I tried to print 'str' or 'pw' or 'decrypted' but can't figure out how
– Jin Lee
Nov 26 '18 at 8:11
1
Just a few feedback. Java 8 onwards, you should useSecureRandom.getInstanceStrong()instead ofSHA1PRNG.Cipher.getInstance("AES")defaults to ECB mode which is not secured. You should ideally use an authenticated encryption mode like GCM. You should avoid storing password inStringasStringis immutable and you can't clear it. Therefore in heap dumps it may appear. You should also clearSecretKeyandSecretKeySpecafter use. I recommend that you read. stackoverflow.com/a/53015144/1235935 for complete Java implementation
– Saptarshi Basu
Nov 26 '18 at 16:29
@Saptarshi Basu Thank you so much. It is very helpful~~
– Jin Lee
Nov 26 '18 at 23:43
add a comment |
I made a class which has a method to encrypt data using SHA1PRNG and AES algorithm.
public String encrypt(String str, String pw) throws Exception{
byte bytes = pw.getBytes();
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(bytes);
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128,sr);
SecretKey skey = kgen.generateKey();
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(),"AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte encrypted = c.doFinal(str.getBytes());
return Hex.encodeHexString(encrypted);
}
I used this method in my main.
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
enc.encrypt("abcde", "abcdfg");
System.out.println(enc);
}
My result is
com.dsmentoring.kmi.Encrytion@34340fab
just my packagename + class name + and some number ( I'm guessing this is reference address to the actual data?)
I want to see the result of my encryption like 'a13efx34123fdv....... ' like this. What do I need to add in my main method? Any advice?
java aes sha1
I made a class which has a method to encrypt data using SHA1PRNG and AES algorithm.
public String encrypt(String str, String pw) throws Exception{
byte bytes = pw.getBytes();
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(bytes);
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128,sr);
SecretKey skey = kgen.generateKey();
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(),"AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte encrypted = c.doFinal(str.getBytes());
return Hex.encodeHexString(encrypted);
}
I used this method in my main.
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
enc.encrypt("abcde", "abcdfg");
System.out.println(enc);
}
My result is
com.dsmentoring.kmi.Encrytion@34340fab
just my packagename + class name + and some number ( I'm guessing this is reference address to the actual data?)
I want to see the result of my encryption like 'a13efx34123fdv....... ' like this. What do I need to add in my main method? Any advice?
java aes sha1
java aes sha1
asked Nov 26 '18 at 8:04
Jin LeeJin Lee
96214
96214
1
You are printing the instance rather than the value it returns
– karthick
Nov 26 '18 at 8:07
@karthick How can I print the value? I tried to print 'str' or 'pw' or 'decrypted' but can't figure out how
– Jin Lee
Nov 26 '18 at 8:11
1
Just a few feedback. Java 8 onwards, you should useSecureRandom.getInstanceStrong()instead ofSHA1PRNG.Cipher.getInstance("AES")defaults to ECB mode which is not secured. You should ideally use an authenticated encryption mode like GCM. You should avoid storing password inStringasStringis immutable and you can't clear it. Therefore in heap dumps it may appear. You should also clearSecretKeyandSecretKeySpecafter use. I recommend that you read. stackoverflow.com/a/53015144/1235935 for complete Java implementation
– Saptarshi Basu
Nov 26 '18 at 16:29
@Saptarshi Basu Thank you so much. It is very helpful~~
– Jin Lee
Nov 26 '18 at 23:43
add a comment |
1
You are printing the instance rather than the value it returns
– karthick
Nov 26 '18 at 8:07
@karthick How can I print the value? I tried to print 'str' or 'pw' or 'decrypted' but can't figure out how
– Jin Lee
Nov 26 '18 at 8:11
1
Just a few feedback. Java 8 onwards, you should useSecureRandom.getInstanceStrong()instead ofSHA1PRNG.Cipher.getInstance("AES")defaults to ECB mode which is not secured. You should ideally use an authenticated encryption mode like GCM. You should avoid storing password inStringasStringis immutable and you can't clear it. Therefore in heap dumps it may appear. You should also clearSecretKeyandSecretKeySpecafter use. I recommend that you read. stackoverflow.com/a/53015144/1235935 for complete Java implementation
– Saptarshi Basu
Nov 26 '18 at 16:29
@Saptarshi Basu Thank you so much. It is very helpful~~
– Jin Lee
Nov 26 '18 at 23:43
1
1
You are printing the instance rather than the value it returns
– karthick
Nov 26 '18 at 8:07
You are printing the instance rather than the value it returns
– karthick
Nov 26 '18 at 8:07
@karthick How can I print the value? I tried to print 'str' or 'pw' or 'decrypted' but can't figure out how
– Jin Lee
Nov 26 '18 at 8:11
@karthick How can I print the value? I tried to print 'str' or 'pw' or 'decrypted' but can't figure out how
– Jin Lee
Nov 26 '18 at 8:11
1
1
Just a few feedback. Java 8 onwards, you should use
SecureRandom.getInstanceStrong() instead of SHA1PRNG. Cipher.getInstance("AES") defaults to ECB mode which is not secured. You should ideally use an authenticated encryption mode like GCM. You should avoid storing password in String as String is immutable and you can't clear it. Therefore in heap dumps it may appear. You should also clear SecretKey and SecretKeySpec after use. I recommend that you read. stackoverflow.com/a/53015144/1235935 for complete Java implementation– Saptarshi Basu
Nov 26 '18 at 16:29
Just a few feedback. Java 8 onwards, you should use
SecureRandom.getInstanceStrong() instead of SHA1PRNG. Cipher.getInstance("AES") defaults to ECB mode which is not secured. You should ideally use an authenticated encryption mode like GCM. You should avoid storing password in String as String is immutable and you can't clear it. Therefore in heap dumps it may appear. You should also clear SecretKey and SecretKeySpec after use. I recommend that you read. stackoverflow.com/a/53015144/1235935 for complete Java implementation– Saptarshi Basu
Nov 26 '18 at 16:29
@Saptarshi Basu Thank you so much. It is very helpful~~
– Jin Lee
Nov 26 '18 at 23:43
@Saptarshi Basu Thank you so much. It is very helpful~~
– Jin Lee
Nov 26 '18 at 23:43
add a comment |
1 Answer
1
active
oldest
votes
You're printing the Encryption object instead of the result of the function call.
You can do this instead:
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
String result = enc.encrypt("abcde", "abcdfg");
System.out.println(result);
}
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',
autoActivateHeartbeat: false,
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%2f53476866%2fhow-can-i-check-the-result-of-encryption-sha1prng-and-aes-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
You're printing the Encryption object instead of the result of the function call.
You can do this instead:
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
String result = enc.encrypt("abcde", "abcdfg");
System.out.println(result);
}
add a comment |
You're printing the Encryption object instead of the result of the function call.
You can do this instead:
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
String result = enc.encrypt("abcde", "abcdfg");
System.out.println(result);
}
add a comment |
You're printing the Encryption object instead of the result of the function call.
You can do this instead:
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
String result = enc.encrypt("abcde", "abcdfg");
System.out.println(result);
}
You're printing the Encryption object instead of the result of the function call.
You can do this instead:
public static void main(String args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
String result = enc.encrypt("abcde", "abcdfg");
System.out.println(result);
}
answered Nov 26 '18 at 8:18
KraylogKraylog
4,82511525
4,82511525
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.
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%2f53476866%2fhow-can-i-check-the-result-of-encryption-sha1prng-and-aes-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
1
You are printing the instance rather than the value it returns
– karthick
Nov 26 '18 at 8:07
@karthick How can I print the value? I tried to print 'str' or 'pw' or 'decrypted' but can't figure out how
– Jin Lee
Nov 26 '18 at 8:11
1
Just a few feedback. Java 8 onwards, you should use
SecureRandom.getInstanceStrong()instead ofSHA1PRNG.Cipher.getInstance("AES")defaults to ECB mode which is not secured. You should ideally use an authenticated encryption mode like GCM. You should avoid storing password inStringasStringis immutable and you can't clear it. Therefore in heap dumps it may appear. You should also clearSecretKeyandSecretKeySpecafter use. I recommend that you read. stackoverflow.com/a/53015144/1235935 for complete Java implementation– Saptarshi Basu
Nov 26 '18 at 16:29
@Saptarshi Basu Thank you so much. It is very helpful~~
– Jin Lee
Nov 26 '18 at 23:43