jdk/test/com/sun/crypto/provider/Cipher/RSA/TestOAEP.java
changeset 12685 8a448b5b9006
parent 5506 202f599c92aa
child 30046 cf2c86e1819e
equal deleted inserted replaced
12676:3b7fae360d04 12685:8a448b5b9006
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    56         publicKey = kp.getPublic();
    56         publicKey = kp.getPublic();
    57 
    57 
    58         Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding");
    58         Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding");
    59         Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding");
    59         Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding");
    60         Cipher.getInstance("RSA/ECB/OAEPwithSHA-1andMGF1Padding");
    60         Cipher.getInstance("RSA/ECB/OAEPwithSHA-1andMGF1Padding");
       
    61         Cipher.getInstance("RSA/ECB/OAEPwithSHA-224andMGF1Padding");
    61         Cipher.getInstance("RSA/ECB/OAEPwithSHA-256andMGF1Padding");
    62         Cipher.getInstance("RSA/ECB/OAEPwithSHA-256andMGF1Padding");
    62         Cipher.getInstance("RSA/ECB/OAEPwithSHA-384andMGF1Padding");
    63         Cipher.getInstance("RSA/ECB/OAEPwithSHA-384andMGF1Padding");
    63         Cipher.getInstance("RSA/ECB/OAEPwithSHA-512andMGF1Padding");
    64         Cipher.getInstance("RSA/ECB/OAEPwithSHA-512andMGF1Padding");
    64 
    65 
    65         // basic test using MD5
    66         // basic test using MD5
    85             // ok
    86             // ok
    86             System.out.println(e);
    87             System.out.println(e);
    87         }
    88         }
    88         // tests alias works
    89         // tests alias works
    89         testEncryptDecrypt("SHA-1", 16);
    90         testEncryptDecrypt("SHA-1", 16);
       
    91 
       
    92         // basic test using SHA-224
       
    93         testEncryptDecrypt("SHA-224", 0);
       
    94         testEncryptDecrypt("SHA-224", 16);
       
    95         testEncryptDecrypt("SHA-224", 38);
       
    96         try {
       
    97             testEncryptDecrypt("SHA-224", 39);
       
    98             throw new Exception("Unexpectedly completed call");
       
    99         } catch (IllegalBlockSizeException e) {
       
   100             // ok
       
   101             System.out.println(e);
       
   102         }
    90 
   103 
    91         // basic test using SHA-256
   104         // basic test using SHA-256
    92         testEncryptDecrypt("SHA-256", 0);
   105         testEncryptDecrypt("SHA-256", 0);
    93         testEncryptDecrypt("SHA-256", 16);
   106         testEncryptDecrypt("SHA-256", 16);
    94         testEncryptDecrypt("SHA-256", 30);
   107         testEncryptDecrypt("SHA-256", 30);
   193 
   206 
   194         long stop = System.currentTimeMillis();
   207         long stop = System.currentTimeMillis();
   195         System.out.println("Done (" + (stop - start) + " ms).");
   208         System.out.println("Done (" + (stop - start) + " ms).");
   196     }
   209     }
   197 
   210 
       
   211     // NOTE: OAEP can process up to (modLen - 2*digestLen - 2) bytes of data
   198     private static void testEncryptDecrypt(String hashAlg, int dataLength) throws Exception {
   212     private static void testEncryptDecrypt(String hashAlg, int dataLength) throws Exception {
   199         System.out.println("Testing OAEP with hash " + hashAlg + ", " + dataLength + " bytes");
   213         System.out.println("Testing OAEP with hash " + hashAlg + ", " + dataLength + " bytes");
   200         Cipher c = Cipher.getInstance("RSA/ECB/OAEPwith" + hashAlg + "andMGF1Padding", cp);
   214         Cipher c = Cipher.getInstance("RSA/ECB/OAEPwith" + hashAlg + "andMGF1Padding", cp);
   201         c.init(Cipher.ENCRYPT_MODE, publicKey);
   215         c.init(Cipher.ENCRYPT_MODE, publicKey);
   202         byte[] data = new byte[dataLength];
   216         byte[] data = new byte[dataLength];