2008-11-26 3 views
2

모든 파일에 ~/암호/nsdl/암호는 GCJ로 컴파일 here 자바 파일, compile.sh이 프로그램이 IllegalStateException을 유발하는 이유를 알 수 있습니까?

[email protected] ~/Cipher/nsdl/crypto $ echo test | ./cryptTest encrypt deadbeefdeadbeefdeadbeefdeadbeef deadbeef Blowfish CBC > test 
null 
Exception in thread "main" java.lang.IllegalStateException: cipher is not for encrypting or decrypting 
    at javax.crypto.Cipher.update(libgcj.so.81) 
    at javax.crypto.CipherOutputStream.write(libgcj.so.81) 
    at nsdl.crypto.BlockCrypt.encrypt(cryptTest) 
    at nsdl.crypto.cryptTest.main(cryptTest) 

BlockCrypt.java 참조를 찾을 수 있습니다

package nsdl.crypto; 

import java.io.*; 
import java.security.spec.*; 
import javax.crypto.*; 
import javax.crypto.spec.*; 

public class BlockCrypt { 
Cipher ecipher; 
Cipher dcipher; 
byte[] keyBytes; 
byte[] ivBytes; 
SecretKey key; 
AlgorithmParameterSpec iv; 
byte[] buf = new byte[1024]; 

BlockCrypt(String keyStr, String ivStr, String algorithm, String mode) { 
    try { 
     ecipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding"); 
     dcipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding"); 

     keyBytes = hexStringToByteArray(keyStr); 
     ivBytes = hexStringToByteArray(ivStr); 

     key = new SecretKeySpec(keyBytes, algorithm); 
     iv = new IvParameterSpec(ivBytes); 

     ecipher.init(Cipher.ENCRYPT_MODE, key, iv); 
     dcipher.init(Cipher.DECRYPT_MODE, key, iv); 
    } catch (Exception e) { 
     System.err.println(e.getMessage()); 
    } 
} 

public void encrypt(InputStream in, OutputStream out) { 
    try { 
     // out: where the plaintext goes to become encrypted 
     out = new CipherOutputStream(out, ecipher); 

     // in: where the plaintext comes from 
     int numRead = 0; 
     while ((numRead = in.read(buf)) >= 0) { 
      out.write(buf, 0, numRead); 
     } 
     out.close(); 
    } catch (IOException e) { 
     System.err.println(e.getMessage()); 
    } 
} 

public void decrypt(InputStream in, OutputStream out) { 
    try { 
     // in: where the plaintext come from, decrypted on-the-fly 
     in = new CipherInputStream(in, dcipher); 

     // out: where the plaintext goes 
     int numRead = 0; 
     while ((numRead = in.read(buf)) >= 0) { 
      out.write(buf, 0, numRead); 
     } 
     out.flush(); 
     out.close(); 
    } catch (IOException e) { 
     System.err.println(e.getMessage()); 
    } 
} 
public static byte[] hexStringToByteArray(String s) { 
    int len = s.length(); 
    byte[] data = new byte[len/2]; 
    for (int i = 0; i < len; i += 2) { 
     data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) 
     + Character.digit(s.charAt(i+1), 16)); 
    } 
    return data; 
} 
} 

cryptTest.java :

package nsdl.crypto; 

import nsdl.crypto.BlockCrypt; 

public class cryptTest { 

public static void main (String args[]) { 
    if (args.length != 5) { 
     System.err.println("Usage: cryptTest (encrypt|decrypt) key iv algorithm mode"); 
     System.err.println("Takes input from STDIN. Output goes to STDOUT."); 
    } else { 
     String operation = args[0]; 
     String key = args[1]; 
     String iv = args[2]; 
     String algorithm = args[3]; 
     String mode = args[4]; 
     BlockCrypt blockCrypt = new BlockCrypt(key, iv, algorithm, mode); 
     if (operation.equalsIgnoreCase("encrypt")) { 
      blockCrypt.encrypt(System.in, System.out); 
     } else if (operation.equalsIgnoreCase("decrypt")) { 
      blockCrypt.decrypt(System.in, System.out); 
     } else { 
      System.err.println("Invalid operation. Use (encrypt|decrypt)."); 
     } 
    } 
} 
} 
+0

예외가 발생하는 위치를 파악하는 데 도움이 될 수 있습니다. – Darron

+0

그것은 던져 질 곳을 말합니다 : nsdl.crypto.BlockCrypt.encrypt (cryptTest) – Ray

답변

2

암호는 으로 초기화 된 것처럼 사용하려고하면 IllegalStateException이 throw됩니다.

BlockCrypt의 생성자에있는 catch 블록에 유의하십시오. 메시지가없는 예외를 catch하고 System.err에 "null"을 인쇄 중입니다. 실행을 중단하는 대신 생성자 —에서 예외를 throw하여 계속 항해합니다.

System.err.println(e.getMessage())e.printStackTrace() 또는 적어도 System.err.println(e)으로 대체하면 더 자세한 정보를 얻을 수 있습니다. 내 생각 엔 ecipher.init()이 64 비트가 아닌 32 비트 IV를 제공하기 때문에 예외를 던지고있는 것 같습니다.

+0

시간을내어 답변 해 주셔서 감사합니다. IV가 64 비트인지 확인하면 프로그램이 완벽하게 작동합니다! 과학 박람회 프로젝트가 끝나면 크레딧을받을 수 있습니다. (나는 고맙다는 것을 내가 할 수있는 것을 정말로 모른다. 나는 8 학년에 불과하다.) – nsdel

0

는 아마도 the source for javax.crypto.Cipher보고이 메이크업 감각을하는 데 도움이? 원본에서 오류 메시지를 찾는 것조차 실제로 파악할 수 없었습니다. 행운을 빕니다!

관련 문제