在没有自动填充的情况下,AES加密和解密16字节数组的最简单方法是什么?我找到了使用外部库的解决方案,但我希望尽可能避免这种情况.
我目前的代码是
SecretKeySpec skeySpec = new SecretKeySpec(getCryptoKeyByteArray(length=16)); // 128 bits
Cipher encryptor = Cipher.getInstance("AES");
encryptor.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = encryptor.doFinal(plain);
Run Code Online (Sandbox Code Playgroud)
如何防止填充?该plain
数据总是固定长度和包括它自己的填充.如何允许plain
16个字节而不会导致encrypted
32个字节?
我在我的项目中使用 RSA 加密,我需要将字节数组加密为加密字节数组,他的长度小于密钥长度(我使用 2048 位密钥)。当我在 c# 中使用 RSACryptoServiceProvider 类时,它总是将它加密为一个字节数组,他的长度是密钥长度(256 个字节)。我知道我不能将它加密成比密钥更长的大小,但是,我可以将它加密成更小的一个(原始未加密的字节长度数组)吗?
我再次出现在其中一种情况下,它不可能停止/销毁/暂停一个线程..interrupt()没有这个技巧,并且不推荐使用.stop()和.suspend().
很简单的例子:
public class TimerThread extends Thread {
private JPanel colorPanel;
public TimerThread(JPanel colorPanel) {
this.colorPanel = colorPanel;
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
colorPanel.repaint();
} catch (Exception ex) {
//do Nothing
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样做是每秒重新绘制一个JPanel来改变它的颜色.我想从另一个类开始和停止这样的线程:
timer = new Thread(new TimerThread(colorPanel));
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.start();
}
});
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.interrupt();
}
});
Run Code Online (Sandbox Code Playgroud)
显然(?)这不起作用...我知道我可以使用Timer,SwingWorker或声明计时器,timer = new TimerThread(colorPanel);
并在run方法中使用布尔值而不是"true",但我被要求声明计时器作为"线程",没有别的.
令我惊讶的是(或者这是愚蠢的?),即使这样也行不通:
startButton.addActionListener(new ActionListener() {
public …
Run Code Online (Sandbox Code Playgroud) 我怎样才能使用sin或cos而不是Math.sin或Math.cos?我试图导入Math.*但我认为我需要用命名空间做一些事情吗?
java ×3
encryption ×2
.net ×1
aes ×1
android ×1
c# ×1
cryptography ×1
math ×1
padding ×1
rsa ×1