Chinaunix首页 | 论坛 | 博客
  • 博客访问: 212962
  • 博文数量: 70
  • 博客积分: 2050
  • 博客等级: 大尉
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-15 21:42
文章分类

全部博文(70)

文章存档

2013年(1)

2011年(5)

2010年(3)

2009年(9)

2008年(17)

2007年(6)

2006年(29)

我的朋友

分类: Java

2011-01-27 10:15:03

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

class blowfish
{
    public static void show(String name, byte[] info)
    {
        String strHex = "0123456789ABCDEF";

        System.out.print(name + "(" + info.length + "):");
        for (int i = 0; i < info.length; ++i)
        {
            int index = info[i];
            if (index < 0)
                index += 256;
            System.out.print(" " + strHex.charAt(index >> 4) + strHex.charAt(index & 0xf));
        }
        System.out.println("");
    }
    public static void main(String[] args)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("Blowfish/CFB/NoPadding");
            SecretKeySpec secretKeySpec = new SecretKeySpec("1234567890ABCDEF".getBytes(), "Blowfish");
            IvParameterSpec ivParameterSpec = new IvParameterSpec("12345678".getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

            byte[] plain = "中华人民共和国".getBytes("UTF-8");
            byte[] encrypted = cipher.doFinal(plain);

            show("plain", plain);
            show("encrypted result", encrypted);

            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
            show("decrypted result", cipher.doFinal(encrypted));
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}
阅读(600) | 评论(0) | 转发(0) |
0

上一篇:lighttpd config file

下一篇:python blowfish cfb

给主人留下些什么吧!~~