开发者

Java symmetric key file generation and use

I'm working on a school homework, in which I'm sending files from one host to another. As a part of it, I need to encrypt the files being sent using symmetric cryptography.

So, I guess there should be a k开发者_如何学JAVAey file that I need to store on both ends. Thus the server uses that key to encrypt and the sender uses the same key to decrypt. The hosts are located on different places.

So, I need a key here, how can I generate this key and also read it from the disk? Or if I'm on a wrong track, I'd appreciate if someone shed some light over here. Thanks.


You can use already existing libraries, or you can write a simple one yourself.
For example, a key you can use:

String key = "kA}#rP~McSOF~";

The way you can use the key is for example: to encrypt add the char from the key to the char from your file you want to decrypt. Except when the key char is a thilde (~), take the complement of the char.

String txtToEncrypt = "StackOverflow";
bytes[] encrypted = txtToEncrypt.getBytes();
for (int i = 0; i < encrypted.length; i++)
{
    int keyIndex = i % key.length();
    int cK = key.charAt(keyIndex);
    int cE = encrypted[i];
    if (cK == '~')
    {
        cE = ~cE;
    } else
    {
        cE = (cE + cK) % 255;
    }
    encrypted[i] = (byte) cE;
}

Now it is up to you to find the decrypt algoritm. :D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜