gpt4 book ai didi

java - DES 加密 : Given final block not properly padded

转载 作者:行者123 更新时间:2023-11-30 09:15:10 25 4
gpt4 key购买 nike

我得到了这段代码:

static ReadableByteChannel readChannel = null;
static WritableByteChannel writeChannel = null;
static SecretKey key = makeKeyFromPassword("chuj".getBytes());

public static SecretKey makeKeyFromPassword(byte[] password) {

try {
key = KeyGenerator.getInstance("DES").generateKey();
byte[] encoded = key.getEncoded();
return new SecretKeySpec(encoded, "DES");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

public static void run(int mode) throws Exception {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);

//initializing cipher...
Cipher cipher = javax.crypto.Cipher.getInstance("DES");
cipher.init(mode, key);

int read = -1;
while((read = readChannel.read(readBuffer)) != -1){
readBuffer.flip();
cipher.doFinal(readBuffer, writeBuffer);
writeChannel.write(writeBuffer);
readBuffer.clear();
writeBuffer.clear();
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub\
FileOutputStream fos = null;
String inFileString = "C:\\test.txt"; // Valid file pathname
String fileString = "C:\\des.txt"; // Valid file pathname
int mode = Cipher.ENCRYPT_MODE;
FileSystem fs = FileSystems.getDefault();
Path fp = fs.getPath(inFileString);

try {
readChannel = FileChannel.open(fp, EnumSet.of(StandardOpenOption.READ));
fos = new FileOutputStream(fileString);
writeChannel = Channels.newChannel(fos);
run(mode);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

加密工作无一异常(exception)。但是当我尝试解密数据时(模式 == DECRYPT_MODE 并切换文件名)

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:357)
at javax.crypto.CipherSpi.bufferCrypt(CipherSpi.java:767)
at javax.crypto.CipherSpi.engineDoFinal(CipherSpi.java:721)
at javax.crypto.Cipher.doFinal(Cipher.java:2382)
at Test.run(Test.java:57)
at Test.main(Test.java:77)

弹出。

我尝试了不同的 key ,但没有成功。任何帮助将不胜感激。[我必须使用 channel ,此代码仅针对较大类的一部分进行测试]

最佳答案

两件事:

  1. 首先,makeKeyFromPassword方法中的password变量没有关联。返回的 SecretKey 是随机的,因此不可能在程序的两次单独运行中解码数据。
  2. 其次,cipher.doFinal(readBuffer, writeBuffer) 之后应该有writeBuffer.flip() ;-)

工作代码(请注意用于生成DES key 的密码至少需要8个字符):

public class Test {

static ReadableByteChannel readChannel = null;
static WritableByteChannel writeChannel = null;
static SecretKey key = makeKeyFromPassword("abcdefghi".getBytes());
static byte b[];

public static SecretKey makeKeyFromPassword(byte[] password) {
try {
DESKeySpec dks = new DESKeySpec(password);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
return skf.generateSecret(dks);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void run(int mode) throws Exception {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);

// initializing cipher...
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);

while (readChannel.read(readBuffer) != -1) {
readBuffer.flip();
cipher.doFinal(readBuffer, writeBuffer);
writeBuffer.flip();
writeChannel.write(writeBuffer);
readBuffer.clear();
writeBuffer.clear();
}
}

static void f(String inFileString, String fileString, int mode) {
FileOutputStream fos = null;
FileSystem fs = FileSystems.getDefault();
Path fp = fs.getPath(inFileString);

try {
readChannel = FileChannel.open(fp,
EnumSet.of(StandardOpenOption.READ));
fos = new FileOutputStream(fileString);
writeChannel = Channels.newChannel(fos);
run(mode);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
f("C:\\test.txt", "C:\\des.txt", Cipher.ENCRYPT_MODE);
System.out.println("Encrypted.");
f("C:\\des.txt", "C:\\undes.txt", Cipher.DECRYPT_MODE);
System.out.println("Decrypted.");
}

}

关于java - DES 加密 : Given final block not properly padded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com