History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/crypto/provider/PCBC.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/crypto/provider/PCBC.java+198−0
@@ -0,0 +1,198 @@
1+/*
2+ * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package com.sun.crypto.provider;
27+
28+import java.security.InvalidKeyException;
29+
30+/**
31+ * This class represents ciphers in Plaintext Cipher Block Chaining (PCBC)
32+ * mode.
33+ *
34+ * <p>This mode is implemented independently of a particular cipher.
35+ * Ciphers to which this mode should apply (e.g., DES) must be
36+ * <i>plugged-in</i> using the constructor.
37+ *
38+ * <p>NOTE: This class does not deal with buffering or padding.
39+ *
40+ * @author Gigi Ankeny
41+ */
42+
43+final class PCBC extends FeedbackCipher {
44+
45+ /*
46+ * output buffer
47+ */
48+ private final byte[] k;
49+
50+ // variables for save/restore calls
51+ private byte[] kSave = null;
52+
53+ PCBC(SymmetricCipher embeddedCipher) {
54+ super(embeddedCipher);
55+ k = new byte[blockSize];
56+ }
57+
58+ /**
59+ * Gets the name of this feedback mode.
60+ *
61+ * @return the string <code>PCBC</code>
62+ */
63+ String getFeedback() {
64+ return "PCBC";
65+ }
66+
67+ /**
68+ * Initializes the cipher in the specified mode with the given key
69+ * and iv.
70+ *
71+ * @param decrypting flag indicating encryption or decryption
72+ * @param algorithm the algorithm name
73+ * @param key the key
74+ * @param iv the iv
75+ *
76+ * @exception InvalidKeyException if the given key is inappropriate for
77+ * initializing this cipher
78+ */
79+ void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
80+ throws InvalidKeyException {
81+ if ((key == null) || (iv == null) || (iv.length != blockSize)) {
82+ throw new InvalidKeyException("Internal error");
83+ }
84+ this.iv = iv;
85+ reset();
86+ embeddedCipher.init(decrypting, algorithm, key);
87+ }
88+
89+ /**
90+ * Resets the iv to its original value.
91+ * This is used when doFinal is called in the Cipher class, so that the
92+ * cipher can be reused (with its original iv).
93+ */
94+ void reset() {
95+ System.arraycopy(iv, 0, k, 0, blockSize);
96+ }
97+
98+ /**
99+ * Save the current content of this cipher.
100+ */
101+ void save() {
102+ if (kSave == null) {
103+ kSave = new byte[blockSize];
104+ }
105+ System.arraycopy(k, 0, kSave, 0, blockSize);
106+
107+ }
108+
109+ /**
110+ * Restores the content of this cipher to the previous saved one.
111+ */
112+ void restore() {
113+ System.arraycopy(kSave, 0, k, 0, blockSize);
114+ }
115+
116+ /**
117+ * Performs encryption operation.
118+ *
119+ * <p>The input plain text <code>plain</code>, starting at
120+ * <code>plainOffset</code> and ending at
121+ * <code>(plainOffset + len - 1)</code>, is encrypted.
122+ * The result is stored in <code>cipher</code>, starting at
123+ * <code>cipherOffset</code>.
124+ *
125+ * <p>It is the application's responsibility to make sure that
126+ * <code>plainLen</code> is a multiple of the embedded cipher's block size,
127+ * as any excess bytes are ignored.
128+ *
129+ * <p>It is also the application's responsibility to make sure that
130+ * <code>init</code> has been called before this method is called.
131+ * (This check is omitted here, to avoid double checking.)
132+ *
133+ * @param plain the buffer with the input data to be encrypted
134+ * @param plainOffset the offset in <code>plain</code>
135+ * @param plainLen the length of the input data
136+ * @param cipher the buffer for the result
137+ * @param cipherOffset the offset in <code>cipher</code>
138+ */
139+ void encrypt(byte[] plain, int plainOffset, int plainLen,
140+ byte[] cipher, int cipherOffset)
141+ {
142+ int i;
143+ int endIndex = plainOffset + plainLen;
144+
145+ for (; plainOffset < endIndex;
146+ plainOffset += blockSize, cipherOffset += blockSize) {
147+ for (i=0; i<blockSize; i++) {
148+ k[i] ^= (byte)(plain[i+plainOffset]);
149+ }
150+ embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
151+ for (i = 0; i < blockSize; i++) {
152+ k[i] = (byte)(plain[i+plainOffset] ^ cipher[i+cipherOffset]);
153+ }
154+ }
155+ }
156+
157+ /**
158+ * Performs decryption operation.
159+ *
160+ * <p>The input cipher text <code>cipher</code>, starting at
161+ * <code>cipherOffset</code> and ending at
162+ * <code>(cipherOffset + len - 1)</code>, is decrypted.
163+ * The result is stored in <code>plain</code>, starting at
164+ * <code>plainOffset</code>.
165+ *
166+ * <p>It is the application's responsibility to make sure that
167+ * <code>cipherLen</code> is a multiple of the embedded cipher's block
168+ * size, as any excess bytes are ignored.
169+ *
170+ * <p>It is also the application's responsibility to make sure that
171+ * <code>init</code> has been called before this method is called.
172+ * (This check is omitted here, to avoid double checking.)
173+ *
174+ * @param cipher the buffer with the input data to be decrypted
175+ * @param cipherOffset the offset in <code>cipherOffset</code>
176+ * @param cipherLen the length of the input data
177+ * @param plain the buffer for the result
178+ * @param plainOffset the offset in <code>plain</code>
179+ */
180+ void decrypt(byte[] cipher, int cipherOffset, int cipherLen,
181+ byte[] plain, int plainOffset)
182+ {
183+ int i;
184+ int endIndex = cipherOffset + cipherLen;
185+
186+ for (; cipherOffset < endIndex;
187+ plainOffset += blockSize, cipherOffset += blockSize) {
188+ embeddedCipher.decryptBlock(cipher, cipherOffset,
189+ plain, plainOffset);
190+ for (i = 0; i < blockSize; i++) {
191+ plain[i+plainOffset] ^= k[i];
192+ }
193+ for (i = 0; i < blockSize; i++) {
194+ k[i] = (byte)(plain[i+plainOffset] ^ cipher[i+cipherOffset]);
195+ }
196+ }
197+ }
198+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t