CodeDocu.com

Android Java: AVM Fritz box login
 
Connecting an Android app with an AVM Fritz box requires two important numbers.
Firstly, the challenge of number (challenge) and then a SessionID (SID ).
You get the challenge and SID from a Fritzbox router, if you send a blank login request to the login URL.
Then you get a challenge number. It appends the challenge number and an encrypted password in a second login request as a parameter, then it gets back a SessionID.
 
With these two numbers one can then via WebRequests the complete Web pages of a Fritzbox router go through and evaluate.
 
The following is the complete login script for an Android app to log in the Fritz box as an app
Android Java: AVM Fritz box login
 
 
Complete Java code to login and determine in an AVM Fritz.box


public String sSID=""; //SessionID from Fritz-box
public int intLogin_Loop=0;

public void btnRead_onClick(View view) {
//----------------< btnRead_onClick() >----------------
sSID="";intLogin_Loop=0;
String sURL_Login=
"http://fritz.box/login_sid.lua";
fp_Get_SID_by_Login(sURL_Login);
//----------------</ btnRead_onClick() >----------------
}

public void fp_Get_SID_by_Login(String sURL_Login)
{
//----------------< fp_Login_Fritz_Volley_Instance() >----------------
//String sURL_Login = "http://fritz.box/login_sid.lua";
StringRequest stringRequest = new StringRequest(Request.Method.GET, sURL_Login,
new Response.Listener<String>()
{
public void onResponse(String response) {
//--------< Login1 >--------
fp_calculate_SID_by_XAML_Text(response.toString());
//--------</ Login1 >--------
}
},
new Response.ErrorListener()
{
public void onErrorResponse(VolleyError error) {
txtRead.setText(error.getMessage());
}
});
//< execute >
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
//</ execute >
//----------------</ fp_Login_Fritz_Volley_Instance() >----------------
}

private void fp_calculate_SID_by_XAML_Text(String sXML)
{
//----------------< fp_calculate_SID_by_XAML_Text() >----------------
/*in the first loop we get the challengeNr
* and in the second loop we get the sid number
*/
intLogin_Loop++;
//< check >
if (intLogin_Loop>2) {sSID="";return;} //only max 2 times
if (sSID!="")return; //sSID is known
//</ check >
InputSource inputSource = new InputSource(new StringReader(sXML));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder =
null;
try {
builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(inputSource);

//--< Sid + ChallengeCode >--
//*first get the challenge and the sessionID
String challenge = get_Value_of_Node(doc, "Challenge");
String sid = get_Value_of_Node( doc,
"SID");
//--</ Sid + ChallengeCode >--

//< check>
if (sid.equals("0000000000000000") )
{
//----< Login mit Challenge-Hash >----
//*wenn die Challenge vorhanden ist

txtRead.setText(sid );
String benutzername =
"xxxxxxxx";
String kennwort =
ctlPassword.getText().toString();
String sChallenge_with_hash= get_String_Challenge_with_Hash(challenge, kennwort);

String sURL_Login =
"http://fritz.box/login_sid.lua?username=" + benutzername + "&response=" + sChallenge_with_hash;
fp_Get_SID_by_Login(sURL_Login);
//----</ Login mit Challenge-Hash >----
}
else
{
sSID=sid; //ok done
txtRead.setText("DONE: " + sXML);
}

}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
catch (SAXException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
//----------------</ fp_calculate_SID_by_XAML_Text() >----------------
}

public String get_String_Challenge_with_Hash(String challenge, String kennwort)
{
//----------------< get_String_Challenge_with_Hash() >----------------
/*combine the callenge-Number plus the password.inHashMD5_encoded
*/
return challenge + "-" + get_Hash_MD5_UTF16(challenge + "-" + kennwort);
//----------------</ get_String_Challenge_with_Hash() >----------------
}

public String get_Hash_MD5_UTF16(String input) {
//----------------< get_Hash_MD5_UTF16() >----------------
/*Encode a string with Hash MD5 in UTF-16LE */
//input="test";
//test->soll->"c8059e2ec7419f590e79d7f1b774bfe6" string

final String MD5 = "MD5";
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance(
"MD5");
byte[] array = md.digest(input.getBytes("UTF-16LE"));
StringBuffer sb =
new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] &
0xFF) | 0x100).substring(1, 3));
}
//< output result >
return sb.toString();
//</ output result >
} catch (java.security.NoSuchAlgorithmException e) {
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
//----------------</ get_Hash_MD5_UTF16() >----------------
}


private String get_Value_of_Node(Document dom,String sNode_TagName)
{
//----------------< get_Value_of_Node() >----------------
/* get the value of a 1-element node*/

Element rootElement = dom.getDocumentElement();
NodeList items = rootElement.getElementsByTagName(sNode_TagName);

//< check >
if(items.getLength()<=0){return null;}
//</ check >

//< output >
Node node0=items.item(0);
return node0.getFirstChild().getTextContent() ;
//</ output >
//----------------</ get_Value_of_Node() >----------------
}
 

 
 
MainActivity.xaml
Android Java: AVM Fritz box login
 
And the complete associated MainActivity.xml (corresponds to the Mainpage.xaml in UWP)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.rue25.demo_03_read_webpage.MainActivity">

<
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read Website"
android:id="@+id/btnRead"
android:onClick="btnRead_onClick"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="51dp" />

<
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="web text"
android:id="@+id/txtWeb"
android:layout_below="@+id/btnRead"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="45dp" />

<
EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/uiPassword"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/btnRead"
android:layout_toEndOf="@+id/btnRead" />

<
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Password:"
android:id="@+id/lblPassword"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</
RelativeLayout>
 

 

Software Entwicklung Stuttgart NĂ¼rtingen
Suche Projekte C#, WPF, Windows App,ASP.Net, vb.Net, WinForms, SQL Server, Access, Excel