1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.shared.crypto.encryption;
21
22
23 import javax.security.auth.kerberos.KerberosKey;
24 import javax.security.auth.kerberos.KerberosPrincipal;
25
26 import junit.framework.TestCase;
27
28 import org.apache.directory.server.kerberos.shared.messages.value.EncryptedData;
29 import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
30
31
32
33
34
35
36
37
38 public class DesCbcCrcEncryptionTest extends TestCase
39 {
40 private static final char[] PASSWORD = "password".toCharArray();
41
42
43
44
45
46
47
48 public void testPlainTextSizeLessThanBlockSize() throws Exception
49 {
50 KerberosKey key = new KerberosKey( new KerberosPrincipal( "hnelson@EXAMPLE.COM" ), PASSWORD, "DES" );
51 byte[] keyBytes = key.getEncoded();
52 EncryptionKey encryptionKey = new EncryptionKey( EncryptionType.DES_CBC_CRC, keyBytes );
53
54 byte[] plainText =
55 { 1, 2, 3, 4, 5, 6, 7 };
56
57 DesCbcCrcEncryption encryption = new DesCbcCrcEncryption();
58 EncryptedData encryptedData = encryption.getEncryptedData( encryptionKey, plainText, null );
59
60 byte[] recoveredText = encryption.getDecryptedData( encryptionKey, encryptedData, null );
61
62 assertTrue( beginsWith( plainText, recoveredText ) );
63 }
64
65
66
67
68
69
70
71 public void testPlainTextSizeEqualsBlockSize() throws Exception
72 {
73 KerberosKey key = new KerberosKey( new KerberosPrincipal( "hnelson@EXAMPLE.COM" ), PASSWORD, "DES" );
74 byte[] keyBytes = key.getEncoded();
75 EncryptionKey encryptionKey = new EncryptionKey( EncryptionType.DES_CBC_CRC, keyBytes );
76
77 byte[] plainText =
78 { 1, 2, 3, 4, 5, 6, 7, 8 };
79
80 DesCbcCrcEncryption encryption = new DesCbcCrcEncryption();
81 EncryptedData encryptedData = encryption.getEncryptedData( encryptionKey, plainText, null );
82
83 byte[] recoveredText = encryption.getDecryptedData( encryptionKey, encryptedData, null );
84
85 assertTrue( beginsWith( plainText, recoveredText ) );
86 }
87
88
89
90
91
92
93
94 public void testPlainTextSizeGreaterThanBlockSize() throws Exception
95 {
96 KerberosKey key = new KerberosKey( new KerberosPrincipal( "hnelson@EXAMPLE.COM" ), PASSWORD, "DES" );
97 byte[] keyBytes = key.getEncoded();
98 EncryptionKey encryptionKey = new EncryptionKey( EncryptionType.DES_CBC_CRC, keyBytes );
99
100 byte[] plainText =
101 { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
102
103 DesCbcCrcEncryption encryption = new DesCbcCrcEncryption();
104 EncryptedData encryptedData = encryption.getEncryptedData( encryptionKey, plainText, null );
105
106 byte[] recoveredText = encryption.getDecryptedData( encryptionKey, encryptedData, null );
107
108 assertTrue( beginsWith( plainText, recoveredText ) );
109 }
110
111
112 private boolean beginsWith( byte[] plainText, byte[] recoveredText )
113 {
114 for ( int i = 0; i < plainText.length; i++ )
115 {
116 if ( plainText[i] != recoveredText[i] )
117 {
118 return false;
119 }
120 }
121
122 return true;
123 }
124 }