Using the KeyGenerator class and showing how to create a SecretKeySpec from an encoded key : 密钥生成器 « 安全 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 安全 » 密钥生成器屏幕截图 
Using the KeyGenerator class and showing how to create a SecretKeySpec from an encoded key
 
import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Basic example using the KeyGenerator class and showing how to create a
 * SecretKeySpec from an encoded key.
 */
public class MainClass {
  public static void main(String[] argsthrows Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());        
    byte[] input = "www.java2java.com".getBytes();
    
    byte[] ivBytes = new byte[] { 0x000x000x000x010x040x050x060x070x000x000x00,
        0x000x000x000x000x01 };

    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding""BC");
    KeyGenerator generator = KeyGenerator.getInstance("AES""BC");

    generator.init(192);

    Key encryptionKey = generator.generateKey();

    System.out.println("key   : " + Utils.toHex(encryptionKey.getEncoded()));
    System.out.println("input : " new String(input));

    // encryption pass
    cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes));
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);

    // decryption pass
    Key decryptionKey = new SecretKeySpec(encryptionKey.getEncoded(), encryptionKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec(ivBytes));
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    ptLength += cipher.doFinal(plainText, ptLength);
    System.out.println("plain : " new String(plainText" bytes: " + ptLength);
  }
}


class Utils
{
    private static String digits = "0123456789abcdef";
    
    /**
     * Return length many bytes of the passed in byte array as a hex string.
     
     @param data the bytes to be converted.
     @param length the number of bytes in the data block to be converted.
     @return a hex representation of length bytes of data.
     */
    public static String toHex(byte[] data, int length)
    {
        StringBuffer  buf = new StringBuffer();
        
        for (int i = 0; i != length; i++)
        {
            int v = data[i0xff;
            
            buf.append(digits.charAt(v >> 4));
            buf.append(digits.charAt(v & 0xf));
        }
        
        return buf.toString();
    }
    
    /**
     * Return the passed in byte array as a hex string.
     
     @param data the bytes to be converted.
     @return a hex representation of data.
     */
    public static String toHex(byte[] data)
    {
        return toHex(data, data.length);
    }
}

           
         
  
HowToCreateASecretKeySpecFromAnEncodedKey.zip( 1,199 k)
Related examples in the same category
1. 关键词生成Mac
2. 生成密钥DSA
3. 密钥生成私钥
4. 密钥发生器的公共密钥
5. 包裹和打开关键词
6. 生成公钥/私钥对
7. 生成一个576位密钥
8. 产生1024位RSA密钥对
9. 入门字节的生成的密钥对
10. 获取字节的公用钥匙和私人钥匙
11. 字节可以转换回公共和私人关键对象
12. 创造一个关键的HMAC - SHA1键控-散列算法
13. 非对称密钥制作
14. 产生一个对称密钥
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.