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;

                //System.out.println("int: len="+Length);
                //System.out.print(Integer.toHexString(plain[index-1]&0xff)+":");
                //System.out.println("");

                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;
                /*
                System.out.println("int: N len="+Length);
                for(int i=0; i<n_array.Length; i++){
                System.out.print(Integer.toHexString(n_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                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;
                /*
                System.out.println("int: E len="+Length);
                for(int i=0; i<e_array.Length; i++){
                System.out.print(Integer.toHexString(e_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                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;
                /*
                System.out.println("int: D len="+Length);
                for(int i=0; i<d_array.Length; i++){
                System.out.print(Integer.toHexString(d_array[i]&0xff)+":");
                }
                System.out.println("");
                */

                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;
                /*
                System.out.println("int: P len="+Length);
                for(int i=0; i<p_array.Length; i++){
                System.out.print(Integer.toHexString(p_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                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;
                /*
                System.out.println("int: q len="+Length);
                for(int i=0; i<q_array.Length; i++){
                System.out.print(Integer.toHexString(q_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                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;
                /*
                System.out.println("int: dmp1 len="+Length);
                for(int i=0; i<dmp1_array.Length; i++){
                System.out.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                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;
                /*
                System.out.println("int: dmq1 len="+Length);
                for(int i=0; i<dmq1_array.Length; i++){
                System.out.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                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;
                /*
                System.out.println("int: iqmp len="+Length);
                for(int i=0; i<iqmp_array.Length; i++){
                System.out.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
                }
                System.out.println("");
                */
            }
            catch
            {
                //System.out.println(e);
                return false;
            }
            return true;
        }
Пример #2
0
        internal override bool parse(byte [] plain)
        {
            /*
            byte[] p_array;
            byte[] q_array;
            byte[] dmp1_array;
            byte[] dmq1_array;
            byte[] iqmp_array;
            */
            try
            {
                int index=0;
                int Length=0;

                if(vendor==VENDOR_FSECURE)
                {
                    if(plain[index]!=0x30)
                    {                  // FSecure
                        Buffer buf=new Buffer(plain);
                        pub_array=buf.getMPIntBits();
                        prv_array=buf.getMPIntBits();
                        n_array=buf.getMPIntBits();
                        byte[] u_array=buf.getMPIntBits();
                        p_array=buf.getMPIntBits();
                        q_array=buf.getMPIntBits();
                        return true;
                    }
                    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;

                //System.out.println("int: len="+Length);
                //System.out.print(Integer.toHexString(plain[index-1]&0xff)+":");
                //System.out.println("");

                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;
                /*
                System.out.println("int: N len="+Length);
                for(int i=0; i<n_array.Length; i++){
                System.out.print(Integer.toHexString(n_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                index++;
                Length=plain[index++]&0xff;
                if((Length&0x80)!=0)
                {
                    int foo=Length&0x7f; Length=0;
                    while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
                }
                pub_array=new byte[Length];
                Array.Copy(plain, index, pub_array, 0, Length);
                index+=Length;
                /*
                System.out.println("int: E len="+Length);
                for(int i=0; i<pub_array.Length; i++){
                System.out.print(Integer.toHexString(pub_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                index++;
                Length=plain[index++]&0xff;
                if((Length&0x80)!=0)
                {
                    int foo=Length&0x7f; Length=0;
                    while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
                }
                prv_array=new byte[Length];
                Array.Copy(plain, index, prv_array, 0, Length);
                index+=Length;
                /*
                System.out.println("int: prv len="+Length);
                for(int i=0; i<prv_array.Length; i++){
                System.out.print(Integer.toHexString(prv_array[i]&0xff)+":");
                }
                System.out.println("");
                */

                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;
                /*
                System.out.println("int: P len="+Length);
                for(int i=0; i<p_array.Length; i++){
                System.out.print(Integer.toHexString(p_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                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;
                /*
                System.out.println("int: q len="+Length);
                for(int i=0; i<q_array.Length; i++){
                System.out.print(Integer.toHexString(q_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                index++;
                Length=plain[index++]&0xff;
                if((Length&0x80)!=0)
                {
                    int foo=Length&0x7f; Length=0;
                    while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
                }
                ep_array=new byte[Length];
                Array.Copy(plain, index, ep_array, 0, Length);
                index+=Length;
                /*
                System.out.println("int: ep len="+Length);
                for(int i=0; i<ep_array.Length; i++){
                System.out.print(Integer.toHexString(ep_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                index++;
                Length=plain[index++]&0xff;
                if((Length&0x80)!=0)
                {
                    int foo=Length&0x7f; Length=0;
                    while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
                }
                eq_array=new byte[Length];
                Array.Copy(plain, index, eq_array, 0, Length);
                index+=Length;
                /*
                System.out.println("int: eq len="+Length);
                for(int i=0; i<eq_array.Length; i++){
                System.out.print(Integer.toHexString(eq_array[i]&0xff)+":");
                }
                System.out.println("");
                */
                index++;
                Length=plain[index++]&0xff;
                if((Length&0x80)!=0)
                {
                    int foo=Length&0x7f; Length=0;
                    while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
                }
                c_array=new byte[Length];
                Array.Copy(plain, index, c_array, 0, Length);
                index+=Length;
                /*
                System.out.println("int: c len="+Length);
                for(int i=0; i<c_array.Length; i++){
                System.out.print(Integer.toHexString(c_array[i]&0xff)+":");
                }
                System.out.println("");
                */
            }
            catch//(Exception e)
            {
                //System.out.println(e);
                return false;
            }
            return true;
        }
        bool decrypt_dss()
        {
            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);
                        /*
                        for(int i=0; i<plain.Length; i++){
                        System.out.print(Integer.toHexString(plain[i]&0xff)+":");
                        }
                        System.out.println("");
                        */
                    }
                    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(P_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;
                    }
                    P_array=buf.getMPIntBits();
                    G_array=buf.getMPIntBits();
                    Q_array=buf.getMPIntBits();
                    pub_array=buf.getMPIntBits();
                    prv_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;

                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;

                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;

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

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

                index++;
                Length=plain[index++]&0xff;
                if((Length&0x80)!=0)
                {
                    int foo=Length&0x7f; Length=0;
                    while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
                }
                prv_array=new byte[Length];
                Array.Copy(plain, index, prv_array, 0, Length);
                index+=Length;
            }
            catch
            {
                //System.out.println(e);
                //e.printStackTrace();
                return false;
            }
            return true;
        }