Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to <a href="http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#trans" rel="noreferrer">Java™ Cryptography Architecture (JCA) Reference Guide</a> (emphasis mine):</p> <blockquote> <p><code>Cipher</code> objects are obtained by using one of the <a href="http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#ProviderImplReq" rel="noreferrer"><code>Cipher getInstance()</code> static factory methods</a>. Here, the algorithm name is slightly different than with other engine classes, in that it specifies not just an algorithm name, but a "transformation". A transformation is a string that describes the operation (or set of operations) to be performed on the given input to produce some output. A transformation always includes the name of a cryptographic algorithm (e.g., <code>DES</code>), and may be followed by a mode and padding scheme. </p> <p>A transformation is of the form:</p> <ul> <li>"<em>algorithm/mode/padding</em>" or</li> <li>"<em>algorithm</em>"</li> </ul> <p>For example, the following are valid transformations:</p> <ul> <li><code>"DES/CBC/PKCS5Padding"</code></li> <li><code>"DES"</code></li> </ul> <p>If just a transformation name is specified, the system will determine if there is an implementation of the requested transformation available in the environment, and if there is more than one, returns there is a preferred one.</p> <p>If both a transformation name and a package provider are specified, the system will determine if there is an implementation of the requested transformation in the package requested, and throw an exception if there is not.</p> <p><strong>If no mode or padding is specified, provider-specific default values for the mode and padding scheme are used.</strong> For example, the <code>SunJCE</code> provider uses <code>ECB</code> as the default mode, and <code>PKCS5Padding</code> as the default padding scheme for <code>DES</code>, <code>DES-EDE</code> and <code>Blowfish</code> ciphers. This means that in the case of the <code>SunJCE</code> provider:</p> <pre><code>Cipher c1 = Cipher.getInstance("DES/ECB/PKCS5Padding"); </code></pre> <p>and</p> <pre><code>Cipher c1 = Cipher.getInstance("DES"); </code></pre> <p>are equivalent statements. </p> <p>Using modes such as CFB and OFB, block ciphers can encrypt data in units smaller than the cipher's actual block size. When requesting such a mode, you may optionally specify the number of bits to be processed at a time by appending this number to the mode name as shown in the "<em>DES/CFB8/NoPadding</em>" and "<em>DES/OFB32/PKCS5Padding</em>" transformations. If no such number is specified, a provider-specific default is used. (For example, the <code>SunJCE</code> provider uses a default of 64 bits for DES.) Thus, block ciphers can be turned into byte-oriented stream ciphers by using an 8 bit mode such as CFB8 or OFB8.</p> <p><strong><a href="http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA" rel="noreferrer">Appendix A</a> of this document contains a list of standard names that can be used to specify the algorithm name, mode, and padding scheme components of a transformation.</strong></p> <p>The objects returned by factory methods are uninitialized, and must be initialized before they become usable.</p> </blockquote> <p>Because your code does not specify mode or padding, provider-specific default values are being used. It appears that your provider is <code>SunJCE</code> and that it's default padding is probably <code>"NoPadding"</code>. With this padding, you are responsible for ensuring that the size of the byte array being encrypted is a multiple of the number of bytes in the secret key. You can make you're life easier by specifying the mode and padding in your transformation:</p> <pre><code>Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); </code></pre> <p><strong>WARNING: You should not use ECB mode in real code. Try CBC instead.</strong></p> <p><strong>Update:</strong> I didn't think it was fair to recommend CBC mode without offering a little sample of how it works:</p> <pre><code>public static void main(String... args) throws Exception { byte[] data = "hello".getBytes(); KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); // 192 and 256 bits may not be available SecretKey secretKey = keyGenerator.generateKey(); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // By initializing the cipher in CBC mode, an "initialization vector" has been randomly // generated. This initialization vector will be necessary to decrypt the encrypted data. // It is safe to store the initialization vector in plain text for later use. You can obtain // it's bytes by calling iv.getIV(). cipher.init(Cipher.ENCRYPT_MODE, secretKey); IvParameterSpec iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class); byte[] encryptedData = cipher.doFinal(data); // When decrypting the encrypted data, you must provide the initialization vector used // during the encryption phase. cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] decryptedData = cipher.doFinal(encryptedData); if (!Arrays.equals(data, decryptedData)) { throw new Exception("Data was not decrypted successfully"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload