public class SymmetricEncryption
extends java.lang.Object
genpassword
utility.
The algorithm used is quite naive, each byte of the message is XOR-ed with the corresponding
byte of a secret key. If the key is shorter, its pointer is restarted from the beginning
and the process continues until the entire message is processed. Evidently, applying this
algorithm twice using the same password/key we obtain the original message:
(a XOR b) XOR b = a
The difference between the format of encrypted and plain message occurs because the encrypted message, in the initial form can contain non-printable ascii codes. So it is converted to a hexadecimal representation, which uses twice as many characters but they are limited to hexadecimal digits.
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
SYM_KEY
The key used for encrypting and decrypting the message.
|
Constructor and Description |
---|
SymmetricEncryption() |
Modifier and Type | Method and Description |
---|---|
private static byte[] |
asBytes(java.lang.String hexString)
Converts a hexadecimal representation to initial bytes array.
|
static java.lang.String |
decrypt(java.lang.String codedMessage)
Decryption method.
|
static java.lang.String |
encrypt(java.lang.String plainMessage)
Encryption method.
|
static void |
main(java.lang.String[] args)
The command-line utility.
|
private static byte[] |
process(java.lang.String passwd,
byte[] data)
This is the core algorithm.
|
private static java.lang.String |
toHexString(byte[] code)
Utility method.
|
private static final java.lang.String SYM_KEY
public static void main(java.lang.String[] args)
> java SymmetricEncryption plain [coded]If only
plain
is provided, then the argument is encrypted with symmetric algorithm
and the result is printed to STDOUT.
If the 2nd parameter is also provided (coded
) then it is used for verification
(against the encrypted plain
). On success, the application returns normally without
printing anything. Otherwise a message is printed to STDERR and -2 is returned to OS.
args
- The parameters passed in by OS.public static java.lang.String encrypt(java.lang.String plainMessage)
plainMessage
- The message to be encoded in plain ASCII.public static java.lang.String decrypt(java.lang.String codedMessage)
codedMessage
- The encoded message. Since it is hex-encoded, its size must be even and each
character a hexadecimal digit.private static java.lang.String toHexString(byte[] code)
code
- An array of bytes to be processed. In this case this is the encrypted message.code
.private static byte[] asBytes(java.lang.String hexString)
hexString
- The input string. Must have an even length and all characters be valid hexadecimal
digits.private static byte[] process(java.lang.String passwd, byte[] data)
passwd
is transformed into a byte array, then the message is linearly processed:
each byte is XOR-ed with the corresponding byte from the key.passwd
- The password / key used for coding.data
- The data to be processed.