我正在尝试创建一个加密ATOM-128中任何给定字符串的Android应用程序 例如:输入"你好" /输出" MIH3 + / CC + qCC" 我已经用c#(windows桌面应用程序)尝试了它并且当我在android studio中尝试用java做同样的事情时它运行良好我有这样的结果: 输入"你好" outut" 2662144270646427486464"
这是代码
public class ATOM {
public static String Encrypt(String clearText)
{
String key = "/128GhIoPQROSTeUbADfgHijKLM+n0pFWXY456xyzB7=39VaqrstJklmNuZvwcdEC";
StringBuilder result = new StringBuilder();
int i = 0;
int[] indexes = new int[4];
int[] chars = new int[3];
do
{
chars[0] = i + 1 > clearText.length() ? 0 : (int)clearText.toCharArray()[i++];
chars[1] = i + 2 > clearText.length() ? 0 : (int)clearText.toCharArray()[i++];
chars[2] = i + 3 > clearText.length() ? 0 : (int)clearText.toCharArray()[i++];
indexes[0] = chars[0] >> 2;
indexes[1] = ((chars[0] & 3) << 4) | (chars[1] >> 4);
indexes[2] = ((chars[1] & 15) << 2) | (chars[2] >> 6);
indexes[3] = chars[2] & 63;
if ((char)chars[1] == 0)
{
indexes[2] = 64;
indexes[3] = 64;
}
else if ((char)chars[2] == 0)
{
indexes[3] = 64;
}
for (int index : indexes)
{
result.append(index);
}
}
while (i < clearText.length());
return result.toString();
}
public static String Decrypt(String clearText)
{
String key = "/128GhIoPQROSTeUbADfgHijKLM+n0pFWXY456xyzB7=39VaqrstJklmNuZvwcdEC";
StringBuilder result = new StringBuilder();
int[] indexes = new int[4];
int[] chars = new int[3];
int i = 0;
do
{
indexes[0] = key.indexOf(i++);
indexes[1] = key.indexOf(i++);
indexes[2] = key.indexOf(i++);
indexes[3] = key.indexOf(i++);
chars[0] = (indexes[0] << 2) | (indexes[1] >> 4);
chars[1] = (indexes[1] & 15) << 4 | (indexes[2] >> 2);
chars[2] = (indexes[2] & 3) << 6 | indexes[3];
result.append((char)chars[0]);
if (indexes[2] != 64)
result.append((char)chars[1]);
if (indexes[3] != 64)
result.append((char)chars[2]);
}
while (i < clearText.length());
return result.toString();
}
}
答案 0 :(得分:0)
查看StringBuilder doc:
public StringBuilder append (int i)
Appends the string representation of the specified int value.
The int value is converted to a string according to the rule defined by valueOf(int).
所以每次打电话
result.append(index);
您附加实际数字的字符串表示形式。你需要做的首先是将你的int解析成char ASCII表示。
result.append(((char)index));
应该这样做。
链接到doc: http://developer.android.com/reference/java/lang/StringBuilder.html#append(int)