我有一个下面实现的AES加密代码。并且,它最多可以正确显示15位数字,但相同的代码不能显示16位数字的正确结果。例如“ 4111111111111111”。我不知道为什么会这样,它正在加密16位数字,但是当我尝试解密它时,它显示了错误的结果
我几乎尝试了所有算法,但都失败了。
谢谢。
public class AES {
private static SecretKeySpec secretKey;
private static byte[] key;
public static void setKey(String myKey)
{
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String encrypt(String strToEncrypt, String secret)
{
try
{
setKey(secret);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.encodeToString((cipher.doFinal(strToEncrypt.getBytes("UTF-8"))),Base64.DEFAULT);
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
我的活动代码
public class MainActivity extends AppCompatActivity
{
private static final String Password = "ABCDEFGHIJKLMNOP";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String toEncode = "4111111111111111";
String enC ;
enC = AES.encrypt(toEncode, Password);
System.out.println("ENC: " + enC );
}
}