简体   繁体   English

在Android中使用RC4进行图像加密

[英]Image encryption with rc4 in android

I have implemented the rc4 algorithm myself, works like a charm in encrypting and decrypting texts, the problem comes when I try to encrypt/decrypt an image. 我自己实现了rc4算法,就像在加密和解密文本时一样,当我尝试加密/解密图像时就会出现问题。 My output image is always unreadable. 我的输出图像始终不可读。 No exceptions are thrown. 没有异常被抛出。 I'm not sure whether its the encryption or decryption that doesn't work, or maybe both. 我不确定它的加密或解密是否无效,或者两者都不起作用。 Your help will be appreciated. 您的帮助将不胜感激。

RC4 Class

public class RC4 { 公开课RC4 {

private byte[] S = new byte[256];

public RC4 (byte[] key) {

     byte[] T = new byte[256];

    int keylen, j;
    byte t;

    for (int i = 0; i < 256; i++) {

        keylen = key.length;
        S[i] = (byte) i;
        T[i] = key[i % keylen];
    }

    //KSA -  key-scheduling algorithm is used to initialize the permutation in the array "S"
    j = 0;
    for (int i = 0 ; i < 256 ; i++)
    {
        j = ((j + S[i] + T[i]) % 256) & 0xFF;

        //swap
        t = S[i];
        S[i] = S[j];
        S[j] = t;
    }
}

public byte[] encrypt(byte[] plaintext)
{
    int j = 0, i = 0, t, k;
    byte temp;
    byte[] pt,ct, s;

    //deep copy
    s = S.clone();

    pt = plaintext;
    ct = new byte[pt.length];
    for (int jj = 0 ; jj < pt.length; jj++)
    {
        i = ((i + 1) % 256) & 0xFF;
        j = ((j + s[i]) % 256) & 0xFF;

        //classic swap
        temp    = s[jj];
        s[jj]   = s[j];
        s[j]    = temp;

        t = ((s[i] + s[j]) % 256) & 0xFF;

        k = s[t];

        ct[jj] = (byte) (k ^ pt[jj]);
    }
    return ct;
}

public byte[] decrypt(byte[] ciphertext)
{
    return encrypt(ciphertext);
}

} }

Then my calls: Server Side 然后我的电话:服务器端

Bitmap SelectedImage = BitmapFactory.decodeFile(selectedImagePath);
            byte[] b = bitmapToByteArray(SelectedImage);

            RC4 rc4 = new RC4(sharedSecret);
            byte[] encrypted = rc4.encrypt(b);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(encrypted);
            objectOutputStream.flush();

Client Side 客户端

FileOutputStream fos = null;
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                byte[] b = (byte[])objectInputStream.readObject();

                RC4 rc4 = new RC4(sharedSecret);
                byte[] decrypted = rc4.decrypt(b);

                Bitmap bmp = BitmapFactory.decodeByteArray(decrypted, 0, decrypted.length);
                fos = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();

I discovered my error, it was in the encrypt function; 我发现了我的错误,它在加密功能中。 misuse of the counter. 滥用柜台。 Following is the corrected code. 以下是更正的代码。

for (int counter = 0 ; counter < pt.length; counter++)
    {
        i = ((i + 1) % 256) & 0xFF;
        j = ((j + s[i]) % 256) & 0xFF;

        //classic swap
        temp    = s[j];
        s[j]    = s[i];
        s[i]    = temp;

        t = ((s[i] + s[j]) % 256) & 0xFF;

        k = s[t];

        ct[counter] = (byte) (k ^ pt[counter]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM