Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
briar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian Dehm
briar
Commits
0f7d27cd
Verified
Commit
0f7d27cd
authored
6 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
Refactor MessageEncrypter main method.
parent
2c99a75b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bramble-core/src/main/java/org/briarproject/bramble/crypto/MessageEncrypter.java
+64
-36
64 additions, 36 deletions
...ava/org/briarproject/bramble/crypto/MessageEncrypter.java
with
64 additions
and
36 deletions
bramble-core/src/main/java/org/briarproject/bramble/crypto/MessageEncrypter.java
+
64
−
36
View file @
0f7d27cd
...
@@ -152,59 +152,47 @@ public class MessageEncrypter {
...
@@ -152,59 +152,47 @@ public class MessageEncrypter {
}
}
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
public
static
void
main
(
String
[]
args
)
{
if
(
args
.
length
<
1
)
{
if
(
args
.
length
<
1
)
{
printUsage
();
printUsage
();
return
;
System
.
exit
(
1
)
;
}
}
SecureRandom
random
=
new
SecureRandom
();
MessageEncrypter
encrypter
=
new
MessageEncrypter
(
random
);
if
(
args
[
0
].
equals
(
"generate"
))
{
if
(
args
[
0
].
equals
(
"generate"
))
{
if
(
args
.
length
!=
3
)
{
if
(
args
.
length
!=
3
)
{
printUsage
();
printUsage
();
return
;
System
.
exit
(
1
);
}
try
{
generateKeyPair
(
args
[
1
],
args
[
2
]);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
exit
(
2
);
}
}
// Generate a key pair
KeyPair
keyPair
=
encrypter
.
generateKeyPair
();
PrintStream
out
=
new
PrintStream
(
new
FileOutputStream
(
args
[
1
]));
out
.
print
(
StringUtils
.
toHexString
(
keyPair
.
getPublic
().
getEncoded
()));
out
.
flush
();
out
.
close
();
out
=
new
PrintStream
(
new
FileOutputStream
(
args
[
2
]));
out
.
print
(
StringUtils
.
toHexString
(
keyPair
.
getPrivate
().
getEncoded
()));
out
.
flush
();
out
.
close
();
}
else
if
(
args
[
0
].
equals
(
"encrypt"
))
{
}
else
if
(
args
[
0
].
equals
(
"encrypt"
))
{
if
(
args
.
length
!=
2
)
{
if
(
args
.
length
!=
2
)
{
printUsage
();
printUsage
();
return
;
System
.
exit
(
1
);
}
try
{
encryptMessage
(
args
[
1
]);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
exit
(
2
);
}
}
// Encrypt a decrypted message
InputStream
in
=
new
FileInputStream
(
args
[
1
]);
byte
[]
keyBytes
=
StringUtils
.
fromHexString
(
readFully
(
in
).
trim
());
PublicKey
publicKey
=
encrypter
.
getKeyParser
().
parsePublicKey
(
keyBytes
);
String
message
=
readFully
(
System
.
in
);
byte
[]
plaintext
=
message
.
getBytes
(
Charset
.
forName
(
"UTF-8"
));
byte
[]
ciphertext
=
encrypter
.
encrypt
(
publicKey
,
plaintext
);
System
.
out
.
println
(
AsciiArmour
.
wrap
(
ciphertext
,
LINE_LENGTH
));
}
else
if
(
args
[
0
].
equals
(
"decrypt"
))
{
}
else
if
(
args
[
0
].
equals
(
"decrypt"
))
{
if
(
args
.
length
!=
2
)
{
if
(
args
.
length
!=
2
)
{
printUsage
();
printUsage
();
return
;
System
.
exit
(
1
);
}
try
{
decryptMessage
(
args
[
1
]);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
exit
(
2
);
}
}
// Decrypt an encrypted message
InputStream
in
=
new
FileInputStream
(
args
[
1
]);
byte
[]
keyBytes
=
StringUtils
.
fromHexString
(
readFully
(
in
).
trim
());
PrivateKey
privateKey
=
encrypter
.
getKeyParser
().
parsePrivateKey
(
keyBytes
);
byte
[]
ciphertext
=
AsciiArmour
.
unwrap
(
readFully
(
System
.
in
));
byte
[]
plaintext
=
encrypter
.
decrypt
(
privateKey
,
ciphertext
);
System
.
out
.
println
(
new
String
(
plaintext
,
Charset
.
forName
(
"UTF-8"
)));
}
else
{
}
else
{
printUsage
();
printUsage
();
System
.
exit
(
1
);
}
}
}
}
...
@@ -216,6 +204,46 @@ public class MessageEncrypter {
...
@@ -216,6 +204,46 @@ public class MessageEncrypter {
System
.
err
.
println
(
"MessageEncrypter decrypt <private_key_file>"
);
System
.
err
.
println
(
"MessageEncrypter decrypt <private_key_file>"
);
}
}
private
static
void
generateKeyPair
(
String
publicKeyFile
,
String
privateKeyFile
)
throws
Exception
{
SecureRandom
random
=
new
SecureRandom
();
MessageEncrypter
encrypter
=
new
MessageEncrypter
(
random
);
KeyPair
keyPair
=
encrypter
.
generateKeyPair
();
PrintStream
out
=
new
PrintStream
(
new
FileOutputStream
(
publicKeyFile
));
out
.
print
(
StringUtils
.
toHexString
(
keyPair
.
getPublic
().
getEncoded
()));
out
.
flush
();
out
.
close
();
out
=
new
PrintStream
(
new
FileOutputStream
(
privateKeyFile
));
out
.
print
(
StringUtils
.
toHexString
(
keyPair
.
getPrivate
().
getEncoded
()));
out
.
flush
();
out
.
close
();
}
private
static
void
encryptMessage
(
String
publicKeyFile
)
throws
Exception
{
SecureRandom
random
=
new
SecureRandom
();
MessageEncrypter
encrypter
=
new
MessageEncrypter
(
random
);
InputStream
in
=
new
FileInputStream
(
publicKeyFile
);
byte
[]
keyBytes
=
StringUtils
.
fromHexString
(
readFully
(
in
).
trim
());
PublicKey
publicKey
=
encrypter
.
getKeyParser
().
parsePublicKey
(
keyBytes
);
String
message
=
readFully
(
System
.
in
);
byte
[]
plaintext
=
message
.
getBytes
(
Charset
.
forName
(
"UTF-8"
));
byte
[]
ciphertext
=
encrypter
.
encrypt
(
publicKey
,
plaintext
);
System
.
out
.
println
(
AsciiArmour
.
wrap
(
ciphertext
,
LINE_LENGTH
));
}
private
static
void
decryptMessage
(
String
privateKeyFile
)
throws
Exception
{
SecureRandom
random
=
new
SecureRandom
();
MessageEncrypter
encrypter
=
new
MessageEncrypter
(
random
);
InputStream
in
=
new
FileInputStream
(
privateKeyFile
);
byte
[]
keyBytes
=
StringUtils
.
fromHexString
(
readFully
(
in
).
trim
());
PrivateKey
privateKey
=
encrypter
.
getKeyParser
().
parsePrivateKey
(
keyBytes
);
byte
[]
ciphertext
=
AsciiArmour
.
unwrap
(
readFully
(
System
.
in
));
byte
[]
plaintext
=
encrypter
.
decrypt
(
privateKey
,
ciphertext
);
System
.
out
.
println
(
new
String
(
plaintext
,
Charset
.
forName
(
"UTF-8"
)));
}
private
static
String
readFully
(
InputStream
in
)
throws
IOException
{
private
static
String
readFully
(
InputStream
in
)
throws
IOException
{
String
newline
=
System
.
getProperty
(
"line.separator"
);
String
newline
=
System
.
getProperty
(
"line.separator"
);
StringBuilder
stringBuilder
=
new
StringBuilder
();
StringBuilder
stringBuilder
=
new
StringBuilder
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment