2012-07-05 9 views
2

누군가이 프로그램의 주요 내용을 설명해 주실 수 있습니까? 나는 코드를보고있어, 나는 완전히 잃어 버렸어. 암호화/암호 해독 단계에 대한 설명이 필요합니다. 한 번에 AES 192 키를 생성한다고 생각하지만 100 % 확실하지는 않습니다. 바이트/ivBytes 중 하나를 사용할지 모르겠습니다.AES 암호화/해독 자바 탄력 성 설명?

import java.security.Key; 
import javax.crypto.Cipher; 
import javax.crypto.KeyGenerator; 
import javax.crypto.spec.IvParameterSpec; 

public class RandomKey 
{ 
    public static void main(String[] args) throws Exception 
    { 
    byte[] input = new byte[] { 
         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 
         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; 


    byte[] ivBytes = new byte[] { 
         0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }; 

    //initializing a new initialization vector 
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); 
    //what does this actually do? 
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); 
    //what does this do? 
    KeyGenerator generator = KeyGenerator.getInstance("AES","BC"); 
    //I assume this generates a key size of 192 bits 
    generator.init(192); 
    //does this generate a random key? 
    Key encryptKey = generator.generateKey(); 

    System.out.println("input: " +toHex(input)); 

    //encryption phase 
    cipher.init(Cipher.ENCRYPT_MODE, encryptKey, ivSpec); 
    //what is this doing? 
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; 
    //what is this doing? 
    int ctLength = cipher.update(input, 0, input.length, cipherText,0); 

    //getting the cipher text length i assume? 
    ctLength += cipher.doFinal (cipherText, ctLength); 
    System.out.println ("Cipher: " +toHex(cipherText) + " bytes: " + ctLength); 


    //decryption phase 
    cipher.init(Cipher.DECRYPT_MODE, encryptKey, ivSpec); 
    //storing the ciphertext in plaintext i'm assuming? 
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; 
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); 
    //getting plaintextLength i think? 
    ptLength= cipher.doFinal (plainText, ptLength); 
    System.out.println("plain: " + toHex(plainText, ptLength)); 

    } 

private static String digits = "abcdef"; 

public static String toHex(byte[] data, int length) 
{ 
    StringBuffer buf = new StringBuffer(); 

    for (int i=0; i!= length; i++) 
    { 
     int v = data[i] & 0xff; 

     buf.append(digits.charAt(v >>4)); 
     buf.append(digits.charAt(v & 0xf)); 
    } 
    return buf.toString(); 

} 

public static String toHex(byte[] data) 
{ 
    return toHex(data, data.length); 
} 
} 

답변

1

여기를 시도해 보셨습니까?

http://docs.oracle.com/javase/6/docs/api/javax/crypto/KeyGenerator.html

http://docs.oracle.com/javase/6/docs/api/javax/crypto/Cipher.html

당신이 코드는 암호화와 자바 바이트 데이터의 암호를 해독하는 방법에 꽤 똑바로 앞으로 코드 예입니다.

클래스 설명서를 읽었 으면이 코드의 동작에 대한 질문을 더 잘 나타낼 수 있습니다.

+0

감사! 그건 분명히 많은 것들을 정리하는 데 도움이됩니다. – Programmer0

2
//what does this actually do? 
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); 

이 더 패딩 (NoPadding)와 카운터 모드 (CTR)에서 설정 한 AES의 사이퍼의 인스턴스를 얻을 수있는 탄력이 성 ("BC") 공급자를 사용합니다. Block Cypher modesPadding에 대한 위키피디아를 참조하십시오. Javadoc도 도움이 될 것입니다.

//what does this do? 
KeyGenerator generator = KeyGenerator.getInstance("AES","BC"); 

다시 Bouncy Castle 공급자를 사용하여 AES 용 키 생성기를 설정합니다. 자세한 내용은 Javadoc을 참조하십시오.

//what is this doing? 
byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; 

암호화 된 출력을 저장할 수있는 크기의 바이트 배열을 설정합니다.

//what is this doing? 
int ctLength = cipher.update(input, 0, input.length, cipherText,0); 

사실상 encyphering을하고 있습니다. 좋은 설명을 얻으려면 Javadoc에서 update() 메소드를 확인하십시오. cyphertext 길이를 업데이트하는 += 그것은이다 에서

//getting the cipher text length i assume? 
ctLength += cipher.doFinal (cipherText, ctLength); 

번호 봐. 다시 update()doFinal() 메소드의 차이점에 대한 Javadoc을 읽으십시오.