Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestCICOWithGCM.java
41161 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 801263726* @library ../UTIL27* @build TestUtil28* @run main TestCICOWithGCM29* @summary Test CipherInputStream/OutputStream with AES GCM mode.30* @author Valerie Peng31* @key randomness32*/3334import java.security.*;35import javax.crypto.*;36import javax.crypto.spec.*;37import java.math.*;38import java.io.*;3940import java.util.*;4142public class TestCICOWithGCM {43public static void main(String[] args) throws Exception {44//init Secret Key45KeyGenerator kg = KeyGenerator.getInstance("AES", "SunJCE");46kg.init(128);47SecretKey key = kg.generateKey();4849//do initialization of the plainText50byte[] plainText = new byte[800];51Random rdm = new Random();52rdm.nextBytes(plainText);5354//init ciphers55Cipher encCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");56encCipher.init(Cipher.ENCRYPT_MODE, key);57Cipher decCipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");58decCipher.init(Cipher.DECRYPT_MODE, key, encCipher.getParameters());5960//init cipher streams61ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);62CipherInputStream ciInput = new CipherInputStream(baInput, encCipher);63ByteArrayOutputStream baOutput = new ByteArrayOutputStream();64CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decCipher);6566//do test67byte[] buffer = new byte[800];68int len = ciInput.read(buffer);69System.out.println("read " + len + " bytes from input buffer");7071while (len != -1) {72ciOutput.write(buffer, 0, len);73System.out.println("wite " + len + " bytes to output buffer");74len = ciInput.read(buffer);75if (len != -1) {76System.out.println("read " + len + " bytes from input buffer");77} else {78System.out.println("finished reading");79}80}8182ciOutput.flush();83ciInput.close();84ciOutput.close();85byte[] recovered = baOutput.toByteArray();86System.out.println("recovered " + recovered.length + " bytes");87if (!Arrays.equals(plainText, recovered)) {88throw new RuntimeException("diff check failed!");89} else {90System.out.println("diff check passed");91}92}93}949596