2

誰かがこのプログラムが何をしているのかを説明して、いくつかの主要なポイントを指摘できますか? 私はコードを見ていて、完全に迷っています。暗号化/復号化フェーズについて説明が必要です。ある時点で AES 192 キーが生成されると思いますが、100% 確実ではありません。byte/ivBytes が何に使用されているかはわかりません。

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 = "0123456789abcdef";

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);
}
}
4

2 に答える 2

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

これは、Bouncy Castle ("BC") プロバイダーを使用して、パディングなし (NoPadding) でカウンター モード (CTR) に設定された AES サイファーのインスタンスを取得します。Block Cypher モードPaddingについては Wikipedia を参照してください。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);

実際に暗号化を行っています。update()メソッドの適切な説明については、Javadoc を確認してください。

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

いいえ。暗号文の長さを更新+=している ことを確認してください。メソッドとメソッドの違いについては、Javadoc を再度お読みください。update()doFinal()

于 2012-07-05T14:48:38.903 に答える
1

ここ見てみましたか?

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

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

あなたが持っているコードは、Javaでバイトデータを暗号化および復号化する方法の非常に簡単なコード例です。

クラスのドキュメントを読み終えたら、このコードの動作に関する質問をより明確に説明できます。

于 2012-07-05T03:12:19.147 に答える