harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r447327 - /incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/internal/kerberos/v5/EncryptedData.java
Date Mon, 18 Sep 2006 08:58:37 GMT
Author: smishura
Date: Mon Sep 18 01:58:36 2006
New Revision: 447327

URL: http://svn.apache.org/viewvc?view=rev&rev=447327
Log:
Add EncryptedData.decrypt(SecretKey key)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/internal/kerberos/v5/EncryptedData.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/internal/kerberos/v5/EncryptedData.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/internal/kerberos/v5/EncryptedData.java?view=diff&rev=447327&r1=447326&r2=447327
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/internal/kerberos/v5/EncryptedData.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/internal/kerberos/v5/EncryptedData.java
Mon Sep 18 01:58:36 2006
@@ -18,6 +18,10 @@
 
 import java.io.IOException;
 
+import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.IvParameterSpec;
+
 import org.apache.harmony.security.asn1.ASN1Explicit;
 import org.apache.harmony.security.asn1.ASN1Integer;
 import org.apache.harmony.security.asn1.ASN1OctetString;
@@ -32,13 +36,28 @@
  */
 public class EncryptedData {
 
+    /**
+     * DES in CBC mode with CRC-based checksum
+     */
+    public static final int DES_CBC_CRC = 1;
+
+    /**
+     * DES in CBC mode with CRC-based checksum
+     */
+    public static final int DES_CBC_MD4 = 2;
+
+    /**
+     * DES in CBC mode with CRC-based checksum
+     */
+    public static final int DES_CBC_MD5 = 3;
+
     private final int etype;
 
     private final int kvno;
 
     private final byte[] cipher;
 
-    private EncryptedData(int etype, int kvno, byte[] cipher) {
+    public EncryptedData(int etype, int kvno, byte[] cipher) {
         this.etype = etype;
         this.kvno = kvno;
         this.cipher = cipher;
@@ -54,6 +73,48 @@
 
     public byte[] getCipher() {
         return cipher;
+    }
+
+    public byte[] decrypt(SecretKey key) {
+
+        int offset;
+
+        IvParameterSpec initCipherState;
+        switch (etype) {
+        case DES_CBC_CRC:
+            offset = 12;// confounder(8)+CRC-32 checksum(4)
+            // copy of original key
+            initCipherState = new IvParameterSpec(key.getEncoded());
+            break;
+        case DES_CBC_MD4:
+        case DES_CBC_MD5:
+            offset = 24;// confounder(8)+ MD4/5 checksum(16)
+            // all-zero
+            initCipherState = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0,
+                    0, 0, 0, });
+            break;
+        default:
+            throw new RuntimeException();//FIXME not implemented yet
+        }
+
+        try {
+            Cipher dcipher = Cipher.getInstance("DES/CBC/NoPadding");
+            
+            dcipher.init(Cipher.DECRYPT_MODE, key, initCipherState);
+            
+            byte[] tmp = dcipher.doFinal(cipher);
+            
+            // TODO: verify checksum
+
+            // cat out: confounder and checksum bytes
+            // TODO: how to do the same for padding bytes?
+            byte[] result = new byte[tmp.length-offset];
+            System.arraycopy(tmp, offset, result, 0, result.length);
+            
+            return result;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
     //



Mime
View raw message