Exemplo n.º 1
0
        private byte[] encrypt(byte[] plain, byte[][] _iv)
        {
            if (passphrase == null)
            {
                return(plain);
            }

            if (cipher == null)
            {
                cipher = genCipher();
            }
            byte[] iv = _iv[0] = new byte[cipher.getIVSize()];

            if (random == null)
            {
                random = genRandom();
            }
            random.fill(iv, 0, iv.Length);

            byte[] key     = genKey(passphrase, iv);
            byte[] encoded = plain;

            // PKCS#5Padding
            {
                //int bsize=cipher.getBlockSize();
                int    bsize = cipher.getIVSize();
                byte[] foo   = new byte[(encoded.Length / bsize + 1) * bsize];
                Array.Copy(encoded, 0, foo, 0, encoded.Length);
                int padding = bsize - encoded.Length % bsize;
                for (int i = foo.Length - 1; (foo.Length - padding) <= i; i--)
                {
                    foo[i] = (byte)padding;
                }
                encoded = foo;
            }

            try
            {
                cipher.init(Cipher.ENCRYPT_MODE, key, iv);
                cipher.update(encoded, 0, encoded.Length, encoded, 0);
            }
            catch //(Exception e)
            {
                //Console.Error.WriteLine(e);
            }
            Util.bzero(key);
            return(encoded);
        }
Exemplo n.º 2
0
        bool decrypt_rsa()
        {
            byte[] p_array;
            byte[] q_array;
            byte[] dmp1_array;
            byte[] dmq1_array;
            byte[] iqmp_array;

            try
            {
                byte[] plain;
                if (encrypted)
                {
                    if (keytype == OPENSSH)
                    {
                        cipher.init(Cipher.DECRYPT_MODE, key, iv);
                        plain = new byte[encoded_data.Length];
                        cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
                    }
                    else if (keytype == FSECURE)
                    {
                        for (int i = 0; i < iv.Length; i++)
                        {
                            iv[i] = 0;
                        }
                        cipher.init(Cipher.DECRYPT_MODE, key, iv);
                        plain = new byte[encoded_data.Length];
                        cipher.update(encoded_data, 0, encoded_data.Length, plain, 0);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    if (n_array != null)
                    {
                        return(true);
                    }
                    plain = encoded_data;
                }

                if (keytype == FSECURE)
                {              // FSecure
                    Buffer buf = new Buffer(plain);
                    int    foo = buf.getInt();
                    if (plain.Length != foo + 4)
                    {
                        return(false);
                    }
                    e_array = buf.getMPIntBits();
                    d_array = buf.getMPIntBits();
                    n_array = buf.getMPIntBits();
                    byte[] u_array = buf.getMPIntBits();
                    p_array = buf.getMPIntBits();
                    q_array = buf.getMPIntBits();
                    return(true);
                }

                int index  = 0;
                int length = 0;

                if (plain[index] != 0x30)
                {
                    return(false);
                }
                index++; // SEQUENCE
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }

                if (plain[index] != 0x02)
                {
                    return(false);
                }
                index++; // INTEGER
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                index += length;

                //Console.Error.WriteLine("int: len="+length);
                //System.err.print(Integer.toHexString(plain[index-1]&0xff)+":");
                //Console.Error.WriteLine("");

                index++;
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                n_array = new byte[length];
                Array.Copy(plain, index, n_array, 0, length);
                index += length;

                /*
                 * Console.Error.WriteLine("int: N len="+length);
                 * for(int i=0; i<n_array.Length; i++){
                 * System.err.print(Integer.toHexString(n_array[i]&0xff)+":");
                 * }
                 * Console.Error.WriteLine("");
                 */
                index++;
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                e_array = new byte[length];
                Array.Copy(plain, index, e_array, 0, length);
                index += length;

                /*
                 * Console.Error.WriteLine("int: E len="+length);
                 * for(int i=0; i<e_array.Length; i++){
                 * System.err.print(Integer.toHexString(e_array[i]&0xff)+":");
                 * }
                 * Console.Error.WriteLine("");
                 */
                index++;
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                d_array = new byte[length];
                Array.Copy(plain, index, d_array, 0, length);
                index += length;

                /*
                 * Console.Error.WriteLine("int: D len="+length);
                 * for(int i=0; i<d_array.Length; i++){
                 * System.err.print(Integer.toHexString(d_array[i]&0xff)+":");
                 * }
                 * Console.Error.WriteLine("");
                 */

                index++;
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                p_array = new byte[length];
                Array.Copy(plain, index, p_array, 0, length);
                index += length;

                /*
                 * Console.Error.WriteLine("int: P len="+length);
                 * for(int i=0; i<p_array.Length; i++){
                 * System.err.print(Integer.toHexString(p_array[i]&0xff)+":");
                 * }
                 * Console.Error.WriteLine("");
                 */
                index++;
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                q_array = new byte[length];
                Array.Copy(plain, index, q_array, 0, length);
                index += length;

                /*
                 * Console.Error.WriteLine("int: q len="+length);
                 * for(int i=0; i<q_array.Length; i++){
                 * System.err.print(Integer.toHexString(q_array[i]&0xff)+":");
                 * }
                 * Console.Error.WriteLine("");
                 */
                index++;
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                dmp1_array = new byte[length];
                Array.Copy(plain, index, dmp1_array, 0, length);
                index += length;

                /*
                 * Console.Error.WriteLine("int: dmp1 len="+length);
                 * for(int i=0; i<dmp1_array.Length; i++){
                 * System.err.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
                 * }
                 * Console.Error.WriteLine("");
                 */
                index++;
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                dmq1_array = new byte[length];
                Array.Copy(plain, index, dmq1_array, 0, length);
                index += length;

                /*
                 * Console.Error.WriteLine("int: dmq1 len="+length);
                 * for(int i=0; i<dmq1_array.Length; i++){
                 * System.err.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
                 * }
                 * Console.Error.WriteLine("");
                 */
                index++;
                length = plain[index++] & 0xff;
                if ((length & 0x80) != 0)
                {
                    int foo = length & 0x7f; length = 0;
                    while (foo-- > 0)
                    {
                        length = (length << 8) + (plain[index++] & 0xff);
                    }
                }
                iqmp_array = new byte[length];
                Array.Copy(plain, index, iqmp_array, 0, length);
                index += length;

                /*
                 * Console.Error.WriteLine("int: iqmp len="+length);
                 * for(int i=0; i<iqmp_array.Length; i++){
                 * System.err.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
                 * }
                 * Console.Error.WriteLine("");
                 */
            }
            catch //(Exception e)
            {
                //Console.Error.WriteLine(e);
                return(false);
            }
            return(true);
        }