Reading a stored SecretKey from file
with this below code i can store SecretKey into file:
public static SecretKey generateKey() throws NoSuchAlgorithmException
// Generate a 256-bit key
final int outputKeyLength = 256;
SecureRandom secureRandom = new SecureRandom();
// Do *not* seed secureRandom! Automatically seeded from system entropy.
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(outputKeyLength, secureRandom);
yourKey = keyGenerator.generateKey();
return yourKey;
yourKey = generateKey();
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "encrypt" + File.separator, "config.xml");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte filesBytes = yourKey.getEncoded();
bos.write(filesBytes);
bos.flush();
bos.close();
now how can i read this file and pass bytes into SecretKey variable? for example:
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "convert" + File.separator, "config.xml");
BufferedInputStream buf = new BufferedInputStream(
new FileInputStream(file));
int length = (int) file.length();
byte audio_data = new byte[length];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = buf.read(audio_data)) != -1)
output.write(audio_data, 0, bytesRead);
byte inarry = output.toByteArray();
yourKey=inarry;
problem is yourKey=inarry;
and pass bytes into yourKey
variable, how can i resolve that?
android
add a comment |
with this below code i can store SecretKey into file:
public static SecretKey generateKey() throws NoSuchAlgorithmException
// Generate a 256-bit key
final int outputKeyLength = 256;
SecureRandom secureRandom = new SecureRandom();
// Do *not* seed secureRandom! Automatically seeded from system entropy.
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(outputKeyLength, secureRandom);
yourKey = keyGenerator.generateKey();
return yourKey;
yourKey = generateKey();
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "encrypt" + File.separator, "config.xml");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte filesBytes = yourKey.getEncoded();
bos.write(filesBytes);
bos.flush();
bos.close();
now how can i read this file and pass bytes into SecretKey variable? for example:
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "convert" + File.separator, "config.xml");
BufferedInputStream buf = new BufferedInputStream(
new FileInputStream(file));
int length = (int) file.length();
byte audio_data = new byte[length];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = buf.read(audio_data)) != -1)
output.write(audio_data, 0, bytesRead);
byte inarry = output.toByteArray();
yourKey=inarry;
problem is yourKey=inarry;
and pass bytes into yourKey
variable, how can i resolve that?
android
I really don't get you want to do.You what to write abyte
to a file and read it later?
– Afshin
Nov 14 '18 at 13:38
@Afshin storingSecretKey
to file and read it and then pass that into SecretKey variable such asyourKey
– DolDurma
Nov 14 '18 at 13:46
add a comment |
with this below code i can store SecretKey into file:
public static SecretKey generateKey() throws NoSuchAlgorithmException
// Generate a 256-bit key
final int outputKeyLength = 256;
SecureRandom secureRandom = new SecureRandom();
// Do *not* seed secureRandom! Automatically seeded from system entropy.
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(outputKeyLength, secureRandom);
yourKey = keyGenerator.generateKey();
return yourKey;
yourKey = generateKey();
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "encrypt" + File.separator, "config.xml");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte filesBytes = yourKey.getEncoded();
bos.write(filesBytes);
bos.flush();
bos.close();
now how can i read this file and pass bytes into SecretKey variable? for example:
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "convert" + File.separator, "config.xml");
BufferedInputStream buf = new BufferedInputStream(
new FileInputStream(file));
int length = (int) file.length();
byte audio_data = new byte[length];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = buf.read(audio_data)) != -1)
output.write(audio_data, 0, bytesRead);
byte inarry = output.toByteArray();
yourKey=inarry;
problem is yourKey=inarry;
and pass bytes into yourKey
variable, how can i resolve that?
android
with this below code i can store SecretKey into file:
public static SecretKey generateKey() throws NoSuchAlgorithmException
// Generate a 256-bit key
final int outputKeyLength = 256;
SecureRandom secureRandom = new SecureRandom();
// Do *not* seed secureRandom! Automatically seeded from system entropy.
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(outputKeyLength, secureRandom);
yourKey = keyGenerator.generateKey();
return yourKey;
yourKey = generateKey();
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "encrypt" + File.separator, "config.xml");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte filesBytes = yourKey.getEncoded();
bos.write(filesBytes);
bos.flush();
bos.close();
now how can i read this file and pass bytes into SecretKey variable? for example:
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "convert" + File.separator, "config.xml");
BufferedInputStream buf = new BufferedInputStream(
new FileInputStream(file));
int length = (int) file.length();
byte audio_data = new byte[length];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = buf.read(audio_data)) != -1)
output.write(audio_data, 0, bytesRead);
byte inarry = output.toByteArray();
yourKey=inarry;
problem is yourKey=inarry;
and pass bytes into yourKey
variable, how can i resolve that?
android
android
edited Nov 14 '18 at 14:21
Fantômas
32.6k156388
32.6k156388
asked Nov 14 '18 at 13:32
DolDurmaDolDurma
3,3811267136
3,3811267136
I really don't get you want to do.You what to write abyte
to a file and read it later?
– Afshin
Nov 14 '18 at 13:38
@Afshin storingSecretKey
to file and read it and then pass that into SecretKey variable such asyourKey
– DolDurma
Nov 14 '18 at 13:46
add a comment |
I really don't get you want to do.You what to write abyte
to a file and read it later?
– Afshin
Nov 14 '18 at 13:38
@Afshin storingSecretKey
to file and read it and then pass that into SecretKey variable such asyourKey
– DolDurma
Nov 14 '18 at 13:46
I really don't get you want to do.You what to write a
byte
to a file and read it later?– Afshin
Nov 14 '18 at 13:38
I really don't get you want to do.You what to write a
byte
to a file and read it later?– Afshin
Nov 14 '18 at 13:38
@Afshin storing
SecretKey
to file and read it and then pass that into SecretKey variable such as yourKey
– DolDurma
Nov 14 '18 at 13:46
@Afshin storing
SecretKey
to file and read it and then pass that into SecretKey variable such as yourKey
– DolDurma
Nov 14 '18 at 13:46
add a comment |
1 Answer
1
active
oldest
votes
Even though I never do this myself, if you want to write SecretKey
into a file and read it later, you can use ObjectOutputStream
and ObjectInputStream
.
SecretKey
is Serializable
, so you can use with those 2 funtions.
write like:
FileOutputStream fout = new FileOutputStream("G:\address.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(yourKey);
oos.close();
fout.close();
read like:
FileInputStream fin = new FileInputStream("G:\address.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
SecretKey yourKey = (SecretKey) ois.readObject();
ois.close();
fin.close();
But as I told, you should normally never do it like this. If you want to just store your key, you need to use KeyStore
. If you need to save it for sending, you need to save it encrypted. Key is the most important part of a cryptography algorithm, so you should never save it like this in a file.
and how can i read? and how can i pass in toSecretKey
variable
– DolDurma
Nov 14 '18 at 13:50
@DolDurma As I told,I have never done it like this(and I think it should never be done like this). But this is reading/writingSerializable
object. updated for reading too.
– Afshin
Nov 14 '18 at 13:52
this is not problem, thanks
– DolDurma
Nov 14 '18 at 14:32
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%2f53301439%2freading-a-stored-secretkey-from-file%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
Even though I never do this myself, if you want to write SecretKey
into a file and read it later, you can use ObjectOutputStream
and ObjectInputStream
.
SecretKey
is Serializable
, so you can use with those 2 funtions.
write like:
FileOutputStream fout = new FileOutputStream("G:\address.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(yourKey);
oos.close();
fout.close();
read like:
FileInputStream fin = new FileInputStream("G:\address.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
SecretKey yourKey = (SecretKey) ois.readObject();
ois.close();
fin.close();
But as I told, you should normally never do it like this. If you want to just store your key, you need to use KeyStore
. If you need to save it for sending, you need to save it encrypted. Key is the most important part of a cryptography algorithm, so you should never save it like this in a file.
and how can i read? and how can i pass in toSecretKey
variable
– DolDurma
Nov 14 '18 at 13:50
@DolDurma As I told,I have never done it like this(and I think it should never be done like this). But this is reading/writingSerializable
object. updated for reading too.
– Afshin
Nov 14 '18 at 13:52
this is not problem, thanks
– DolDurma
Nov 14 '18 at 14:32
add a comment |
Even though I never do this myself, if you want to write SecretKey
into a file and read it later, you can use ObjectOutputStream
and ObjectInputStream
.
SecretKey
is Serializable
, so you can use with those 2 funtions.
write like:
FileOutputStream fout = new FileOutputStream("G:\address.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(yourKey);
oos.close();
fout.close();
read like:
FileInputStream fin = new FileInputStream("G:\address.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
SecretKey yourKey = (SecretKey) ois.readObject();
ois.close();
fin.close();
But as I told, you should normally never do it like this. If you want to just store your key, you need to use KeyStore
. If you need to save it for sending, you need to save it encrypted. Key is the most important part of a cryptography algorithm, so you should never save it like this in a file.
and how can i read? and how can i pass in toSecretKey
variable
– DolDurma
Nov 14 '18 at 13:50
@DolDurma As I told,I have never done it like this(and I think it should never be done like this). But this is reading/writingSerializable
object. updated for reading too.
– Afshin
Nov 14 '18 at 13:52
this is not problem, thanks
– DolDurma
Nov 14 '18 at 14:32
add a comment |
Even though I never do this myself, if you want to write SecretKey
into a file and read it later, you can use ObjectOutputStream
and ObjectInputStream
.
SecretKey
is Serializable
, so you can use with those 2 funtions.
write like:
FileOutputStream fout = new FileOutputStream("G:\address.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(yourKey);
oos.close();
fout.close();
read like:
FileInputStream fin = new FileInputStream("G:\address.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
SecretKey yourKey = (SecretKey) ois.readObject();
ois.close();
fin.close();
But as I told, you should normally never do it like this. If you want to just store your key, you need to use KeyStore
. If you need to save it for sending, you need to save it encrypted. Key is the most important part of a cryptography algorithm, so you should never save it like this in a file.
Even though I never do this myself, if you want to write SecretKey
into a file and read it later, you can use ObjectOutputStream
and ObjectInputStream
.
SecretKey
is Serializable
, so you can use with those 2 funtions.
write like:
FileOutputStream fout = new FileOutputStream("G:\address.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(yourKey);
oos.close();
fout.close();
read like:
FileInputStream fin = new FileInputStream("G:\address.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
SecretKey yourKey = (SecretKey) ois.readObject();
ois.close();
fin.close();
But as I told, you should normally never do it like this. If you want to just store your key, you need to use KeyStore
. If you need to save it for sending, you need to save it encrypted. Key is the most important part of a cryptography algorithm, so you should never save it like this in a file.
edited Nov 14 '18 at 13:51
answered Nov 14 '18 at 13:46
AfshinAfshin
3,0361625
3,0361625
and how can i read? and how can i pass in toSecretKey
variable
– DolDurma
Nov 14 '18 at 13:50
@DolDurma As I told,I have never done it like this(and I think it should never be done like this). But this is reading/writingSerializable
object. updated for reading too.
– Afshin
Nov 14 '18 at 13:52
this is not problem, thanks
– DolDurma
Nov 14 '18 at 14:32
add a comment |
and how can i read? and how can i pass in toSecretKey
variable
– DolDurma
Nov 14 '18 at 13:50
@DolDurma As I told,I have never done it like this(and I think it should never be done like this). But this is reading/writingSerializable
object. updated for reading too.
– Afshin
Nov 14 '18 at 13:52
this is not problem, thanks
– DolDurma
Nov 14 '18 at 14:32
and how can i read? and how can i pass in to
SecretKey
variable– DolDurma
Nov 14 '18 at 13:50
and how can i read? and how can i pass in to
SecretKey
variable– DolDurma
Nov 14 '18 at 13:50
@DolDurma As I told,I have never done it like this(and I think it should never be done like this). But this is reading/writing
Serializable
object. updated for reading too.– Afshin
Nov 14 '18 at 13:52
@DolDurma As I told,I have never done it like this(and I think it should never be done like this). But this is reading/writing
Serializable
object. updated for reading too.– Afshin
Nov 14 '18 at 13:52
this is not problem, thanks
– DolDurma
Nov 14 '18 at 14:32
this is not problem, thanks
– DolDurma
Nov 14 '18 at 14:32
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%2f53301439%2freading-a-stored-secretkey-from-file%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
I really don't get you want to do.You what to write a
byte
to a file and read it later?– Afshin
Nov 14 '18 at 13:38
@Afshin storing
SecretKey
to file and read it and then pass that into SecretKey variable such asyourKey
– DolDurma
Nov 14 '18 at 13:46