📄 t_cipher.java
字号:
/* Derby - Class org.apache.derbyTesting.unitTests.crypto.T_Cipher Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derbyTesting.unitTests.crypto;import org.apache.derbyTesting.unitTests.harness.T_Generic;import org.apache.derbyTesting.unitTests.harness.T_Fail;import org.apache.derby.iapi.services.crypto.*;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.db.PropertyInfo;import org.apache.derby.iapi.error.StandardException;import java.security.Key;import java.io.File;import java.io.InputStream;import java.io.OutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.RandomAccessFile;import java.io.IOException;import java.util.Properties;/*// PTimport javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import java.security.spec.KeySpec;import java.security.AlgorithmParameters;// import java.security.spec.AlgorithmParameterSpec;import javax.crypto.spec.IvParameterSpec;import java.security.GeneralSecurityException;import java.security.MessageDigest;import java.lang.reflect.*;*//* To run, put the following line in derby.properties derby.module.test.T_Cipher=org.apache.derbyTesting.unitTests.crypto.T_Cipher and run java org.apache.derbyTesting.unitTests.harness.UnitTestMain*/public class T_Cipher extends T_Generic{ private static final String testService = "CipherText"; CipherProvider enEngine; CipherProvider deEngine; Key secretKey; byte[] IV; CipherFactory factory; public T_Cipher() { super(); } /* ** Methods required by T_Generic */ public String getModuleToTestProtocolName() { return org.apache.derby.iapi.reference.Module.CipherFactory; } protected String getAlgorithm() { return "DES/CBC/NoPadding"; } protected String getProvider() { // allow for alternate providers String testProvider = System.getProperty("testEncryptionProvider"); if (testProvider != null) return testProvider; else return null; } public void runTests() throws T_Fail { File testFile = new File("extinout/T_Cipher.data"); if (testFile.exists()) testFile.delete(); String bootPassword = "a secret, don't tell anyone"; try { //if external input output files dir does not exist ,create one File ifdir = new File("extinout"); if(!ifdir.exists()) ifdir.mkdirs(); RandomAccessFile file = new RandomAccessFile(testFile, "rw"); setupCiphers(bootPassword); // run thru some in patterns int patternLength = 8192; byte[] pattern = new byte[patternLength]; for (int i = 0; i < patternLength; i++) pattern[i] = (byte)(i & 0xFF); test(pattern, 0, 8, file); // test short patterns test(pattern, 8, 8, file); test(pattern, 1, 16, file); test(pattern, 0, patternLength, file); // test long pattern test(pattern, 0, patternLength/2, file); test(pattern, 1, patternLength/2, file); test(pattern, 2, patternLength/2, file); test(pattern, 3, patternLength/2, file); file.seek(0); check(pattern, 0, 8, file); // file offset 0 check(pattern, 8, 8, file); // file offset 8 check(pattern, 1, 16, file); // file offset 16 check(pattern, 0, patternLength, file); // file offset 32 check(pattern, 0, patternLength/2, file);// file offset 32+patternLength check(pattern, 1, patternLength/2, file);// file offset 32+patternLength+(patternLength/2) check(pattern, 2, patternLength/2, file);// file offset 32+(2*patternLength) check(pattern, 3, patternLength/2, file);// file offset 32+(2*patternLength)+(patternLength/2); REPORT("starting random test"); // now do some random testing from file file.seek(32+patternLength); check(pattern, 0, patternLength/2, file); file.seek(32); check(pattern, 0, patternLength, file); file.seek(32+(2*patternLength)); check(pattern, 2, patternLength/2, file); file.seek(0); check(pattern, 0, 8, file); file.seek(16); check(pattern, 1, 16, file); file.seek(32+(2*patternLength)+(patternLength/2)); check(pattern, 3, patternLength/2, file); file.seek(8); check(pattern, 8, 8, file); file.seek(32+patternLength+(patternLength/2)); check(pattern, 1, patternLength/2, file); file.close(); // test streaming File streamFile = new File("extinout/T_CipherStream.data"); if (streamFile.exists()) streamFile.delete(); // RESOLVE: defectId 1834 if (false) {/* OutputStream enOutputStream = factory.createNewCipherOutputStream( new FileOutputStream(streamFile), factory.ENCRYPT, secretKey, IV); int encryptLength = patternLength; // make it a length that is // not on 8 bytes boundary enOutputStream.write(pattern, 0, encryptLength); enOutputStream.write(pattern, 0, 3); // write a couple of bytes of // garbage at the end enOutputStream.close(); enOutputStream = null; InputStream inStream = new FileInputStream(streamFile); byte[] check = new byte[encryptLength + 100]; int r = inStream.read(check); inStream.close(); REPORT("Stream of " + encryptLength + " is encrypted into " + r + " length"); if (byteArrayIdentical(check, pattern, 0, encryptLength)) throw T_Fail.testFailMsg("encryption stream did not encrypt"); InputStream deInputStream = factory.createNewCipherInputStream( new FileInputStream(streamFile), factory.DECRYPT, secretKey, IV); int totalRead = 0; while((r = deInputStream.read(check, totalRead, check.length-totalRead)) != -1) totalRead += r; if (totalRead != encryptLength) throw T_Fail.testFailMsg("decrypted stream is " + totalRead + " is not the same length"); if (byteArrayIdentical(check, pattern, 0, encryptLength) == false) throw T_Fail.testFailMsg("decryption stream did not decrypt"); deInputStream.close();*/ } } catch (StandardException se) { se.printStackTrace(System.out); throw T_Fail.exceptionFail(se); } catch (IOException ioe) { throw T_Fail.exceptionFail(ioe); } PASS("T_Cipher"); } protected void setupCiphers(String bootPassword) throws T_Fail, StandardException { // set properties for testing Properties props = new Properties(); props.put("encryptionAlgorithm",getAlgorithm()); String provider = getProvider(); if (provider != null) props.put("encryptionProvider",getProvider()); props.put("bootPassword", bootPassword); REPORT("encryption algorithm used : " + getAlgorithm()); REPORT("encryption provider used : " + provider); factory = (CipherFactory)Monitor.bootServiceModule(true, (Object)null, org.apache.derby.iapi.reference.Module.CipherFactory, props); if (factory == null) throw T_Fail.testFailMsg("cannot find Cipher factory "); enEngine = factory.createNewCipher(CipherFactory.ENCRYPT); deEngine = factory.createNewCipher(CipherFactory.DECRYPT); if (enEngine == null) throw T_Fail.testFailMsg("cannot create encryption engine"); if (deEngine == null) throw T_Fail.testFailMsg("cannot create decryption engine"); } protected void test(byte[] cleartext, int offset, int length, RandomAccessFile outfile) throws T_Fail, StandardException, IOException { byte[] ciphertext = new byte[length]; System.arraycopy(cleartext, offset, ciphertext, 0, length); if (enEngine.encrypt(ciphertext, 0, length, ciphertext, 0) != length) throw T_Fail.testFailMsg("encrypted text length != length"); if (byteArrayIdentical(ciphertext, cleartext, offset, length)) throw T_Fail.testFailMsg("encryption just made a copy of the clear text"); outfile.write(ciphertext); // now decrypt it and check deEngine.decrypt(ciphertext, 0, length, ciphertext, 0); if (byteArrayIdentical(ciphertext, cleartext, offset, length) == false) throw T_Fail.testFailMsg("decryption did not yield the same clear text"); } protected void check(byte[] cleartext, int offset, int length, RandomAccessFile infile) throws IOException, T_Fail, StandardException { byte[] ciphertext = new byte[length]; infile.read(ciphertext); if (deEngine.decrypt(ciphertext, 0, length, ciphertext, 0) != length) throw T_Fail.testFailMsg("decrypted text length != length"); if (byteArrayIdentical(ciphertext, cleartext, offset, length) == false) throw T_Fail.testFailMsg("decryption did not yield the same clear text"); } // see if 2 byte arrays are identical protected boolean byteArrayIdentical(byte[] compare, byte[] original, int offset, int length) { for (int i = 0; i < length; i++) { if (compare[i] != original[offset+i]) return false; } return true; } /* private void testBlowfish() { System.out.println("Running testBlowfish"); try { // set up the provider java.security.Provider sunJce = new com.sun.crypto.provider.SunJCE(); java.security.Security.addProvider(sunJce);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -