سلام استاد نحوه اینکد کردن یه ریشته رو
با ی مثال خیلی ساده وروان درحد که ی ریشته رو با یه پسود رمز گذاری کنه و نتجه که همون متن اینکد شده است return کنه توضیح بدین ممنون
من سرچ زدم با عملگر xor کد زیاد بود ولی متوجه از نحوه کار کردنش نشدم برای همین اینجا تایپیک باز کردم که با این عملگر اشنا بشیمI
باسلام
با این کد . اینکریپت مکنم ی استرینگ ولی وقت نمایش تو تکس ویو ارور میده دلیلش چیست ؟
درکل میخوام ی استرینگ اینکریپت ودیکریپت کنم اگه میشه یه مثال بزنید
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private final String txt1 = "ffffffffffff";
private final String key = "ggggggggggg";
private static String Result;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView)this.findViewById(R.id.txt);
xor(txt1, key);
txt.setText(Result);
}
private static String xor(final String txt, final String key) {
char[] txtchar = txt.toCharArray();
char[] keychar = key.toCharArray();
int spos = 0;
for (int pos = 0; pos < txt.length(); ++pos) {
txtchar[pos] =(char) (((byte)txtchar[pos]) ^((byte)keychar[pos]));
if (spos >= key.length()) {
spos = 0;
}
}
Result = txtchar.toString();
return Result;
}}
اینم ارور
11-18 09:12:10.759: E/AndroidRuntime(2462): FATAL EXCEPTION: main
11-18 09:12:10.759: E/AndroidRuntime(2462): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.encoding/com.example.encoding.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=11; index=11
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.os.Looper.loop(Looper.java:137)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-18 09:12:10.759: E/AndroidRuntime(2462): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 09:12:10.759: E/AndroidRuntime(2462): at java.lang.reflect.Method.invoke(Method.java:511)
11-18 09:12:10.759: E/AndroidRuntime(2462): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-18 09:12:10.759: E/AndroidRuntime(2462): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-18 09:12:10.759: E/AndroidRuntime(2462): at dalvik.system.NativeStart.main(Native Method)
11-18 09:12:10.759: E/AndroidRuntime(2462): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=11; index=11
11-18 09:12:10.759: E/AndroidRuntime(2462): at com.example.encoding.MainActivity.xor(MainActivity.java:31)
11-18 09:12:10.759: E/AndroidRuntime(2462): at com.example.encoding.MainActivity.onCreate(MainActivity.java:19)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.app.Activity.performCreate(Activity.java:4465)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-18 09:12:10.759: E/AndroidRuntime(2462): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-18 09:12:10.759: E/AndroidRuntime(2462): ... 11 more
11-18 09:21:25.179: E/AndroidRuntime(2481): FATAL EXCEPTION: main
11-18 09:21:25.179: E/AndroidRuntime(2481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.encoding/com.example.encoding.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=11; index=11
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.os.Looper.loop(Looper.java:137)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-18 09:21:25.179: E/AndroidRuntime(2481): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 09:21:25.179: E/AndroidRuntime(2481): at java.lang.reflect.Method.invoke(Method.java:511)
11-18 09:21:25.179: E/AndroidRuntime(2481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-18 09:21:25.179: E/AndroidRuntime(2481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-18 09:21:25.179: E/AndroidRuntime(2481): at dalvik.system.NativeStart.main(Native Method)
11-18 09:21:25.179: E/AndroidRuntime(2481): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=11; index=11
11-18 09:21:25.179: E/AndroidRuntime(2481): at com.example.encoding.MainActivity.xor(MainActivity.java:31)
11-18 09:21:25.179: E/AndroidRuntime(2481): at com.example.encoding.MainActivity.onCreate(MainActivity.java:19)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.app.Activity.performCreate(Activity.java:4465)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-18 09:21:25.179: E/AndroidRuntime(2481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-18 09:21:25.179: E/AndroidRuntime(2481): ... 11 more
یه مثال ساده میزنم. اول این کلاس رو تعریف کنین:
public class MyXOR {
public static make(String text, String key) {
return base64Encode(xorWithKey(base64Decode(text), base64Decode(key)));
}
private static byte[] xorWithKey(byte[] text, byte[] key) {
byte[] result = new byte[text.length];
for (int i = 0; i < text.length; i++) {
result[i] = (byte) (text[i] ^ key[i % key.length]);
}
return result;
}
private static byte[] base64Decode(String text) {
try {
BASE64Decoder d = new BASE64Decoder();
return d.decodeBufffer(text);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String base64Encode(byte[] bytes) {
BASE64Encoder e = new BASE64Encode();
return e.encode(bytes).replaceAll("\s", "");
}
}
نحوهی استفاده:
String text = edtText.getText().toString();
String key = edtKey.getText().toString();
txtResult.setText(MyXOR.make(text, key));