openpgp加密解密文件 -凯发k8网页登录

天平山上白云泉,云自无心水自闲。何必奔冲山下去,更添波浪向人间!
posts - 288, comments - 524, trackbacks - 0, articles - 6
  凯发k8网页登录-凯发天生赢家一触即发官网 :: 凯发k8网页登录首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
posted on 2014-12-10 06:50 云自无心水自闲 阅读(11617) 评论(5)     所属分类: java心得体会
openpgp 号称是世界上使用最广泛的邮件加密标准.  openpgp is the most widely used email encryption standard in the world. ( http://www.openpgp.org/ )
这篇例子介绍如何使用这个标准进行文件的加密解密 (https://www.bouncycastle.org/latest_releases.html, 需要下载: bcprov-jdk15on-151.jar, bcpg-jdk15on-151.jar).

主要是使用bouncycastle提供的openpgp的库来完成这个功能,参照了其提供的示例程序,进行了部分改动 ( bouncy castle 是一种用于 java 平台的开放源码的轻量级密码术包。它支持大量的密码术算法,并提供 jce 1.2.1 的实现。因为 bouncy castle 被设计成轻量级的,所以从 j2se 1.4 到 j2me(包括 midp)平台,它都可以运行。它是在 midp 上运行的唯一完整的密码术包。)
1. 添加循环遍历来查找第一个可用的message
2. 需要注意的是在main函数中的,如果不添加这一句的话 security.addprovider(new bouncycastleprovider()); 程序运行中会报错:no such provider "bc"
3. 
错误exception in thread "main" java.security.invalidkeyexception: illegal key size or default parameters , 这是因为java缺省的库支持的key长度比较短,需要到oracle的网站上去下载一个支持更长key的库覆盖原有的库文件
/lib/securty/ 目录下的两个jar文件
local_policy.jar and us_export_policy.jar
搜索这个文件: java cryptography extension (jce) unlimited strength jurisdiction policy files
下载页面(以jdk6为例):

package org.bouncycastle.openpgp.examples;

import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.security.nosuchproviderexception;
import java.security.securerandom;
import java.security.security;
import java.util.iterator;

import org.bouncycastle.bcpg.armoredoutputstream;
import org.bouncycastle.bcpg.compressionalgorithmtags;
import org.bouncycastle.jce.provider.bouncycastleprovider;
import org.bouncycastle.openpgp.pgpcompresseddata;
import org.bouncycastle.openpgp.pgpencrypteddata;
import org.bouncycastle.openpgp.pgpencrypteddatagenerator;
import org.bouncycastle.openpgp.pgpencrypteddatalist;
import org.bouncycastle.openpgp.pgpexception;
import org.bouncycastle.openpgp.pgpliteraldata;
import org.bouncycastle.openpgp.pgponepasssignaturelist;
import org.bouncycastle.openpgp.pgpprivatekey;
import org.bouncycastle.openpgp.pgppublickey;
import org.bouncycastle.openpgp.pgppublickeyencrypteddata;
import org.bouncycastle.openpgp.pgpsecretkeyringcollection;
import org.bouncycastle.openpgp.pgpsignaturelist;
import org.bouncycastle.openpgp.pgputil;
import org.bouncycastle.openpgp.jcajce.jcapgpobjectfactory;
import org.bouncycastle.openpgp.operator.jcajce.jcakeyfingerprintcalculator;
import org.bouncycastle.openpgp.operator.jcajce.jcepgpdataencryptorbuilder;
import org.bouncycastle.openpgp.operator.jcajce.jcepublickeydatadecryptorfactorybuilder;
import org.bouncycastle.openpgp.operator.jcajce.jcepublickeykeyencryptionmethodgenerator;
import org.bouncycastle.util.io.streams;

/**
 * a simple utility class that encrypts/decrypts public key based
 * encryption files.
 * 


 * to encrypt a file: keybasedfileprocessor -e [-a|-ai] filename publickeyfile.

 * if -a is specified the output file will be "ascii-armored".
 * if -i is specified the output file will be have integrity checking added.
 * 


 * to decrypt: keybasedfileprocessor -d filename secretkeyfile passphrase.
 * 


 * note 1: this example will silently overwrite files, nor does it pay any attention to
 * the specification of "_console" in the filename. it also expects that a single pass phrase
 * will have been used.
 * 


 * note 2: if an empty file name has been specified in the literal data object contained in the
 * encrypted packet a file with the name filename.out will be generated in the current working directory.
 */
public class keybasedfileprocessor
{
    private static void decryptfile(
        string inputfilename,
        string keyfilename,
        char[] passwd,
        string defaultfilename)
        throws ioexception, nosuchproviderexception
    {
        inputstream in = new bufferedinputstream(new fileinputstream(inputfilename));
        inputstream keyin = new bufferedinputstream(new fileinputstream(keyfilename));
        decryptfile(in, keyin, passwd, defaultfilename);
        keyin.close();
        in.close();
    }

    /**
     * decrypt the passed in message stream
     
*/
    private static void decryptfile(
        inputstream in,
        inputstream keyin,
        char[]      passwd,
        string      defaultfilename)
        throws ioexception, nosuchproviderexception
    {
        in = pgputil.getdecoderstream(in);
        
        try
        {
            jcapgpobjectfactory pgpf = new jcapgpobjectfactory(in);
            pgpencrypteddatalist    enc;

            object                  o = pgpf.nextobject();
            //
            
// the first object might be a pgp marker packet.
            
//
            if (o instanceof pgpencrypteddatalist)
            {
                enc = (pgpencrypteddatalist)o;
            }
            else
            {
                enc = (pgpencrypteddatalist)pgpf.nextobject();
            }
            
            //
            
// find the secret key
            
//
            iterator                    it = enc.getencrypteddataobjects();
            pgpprivatekey               skey = null;
            pgppublickeyencrypteddata   pbe = null;
            pgpsecretkeyringcollection  pgpsec = new pgpsecretkeyringcollection(
                pgputil.getdecoderstream(keyin), new jcakeyfingerprintcalculator());

            while (skey == null && it.hasnext())
            {
                pbe = (pgppublickeyencrypteddata)it.next();
                
                skey = pgpexampleutil.findsecretkey(pgpsec, pbe.getkeyid(), passwd);
            }
            
            if (skey == null)
            {
                throw new illegalargumentexception("secret key for message not found.");
            }
    
            inputstream         clear = pbe.getdatastream(new jcepublickeydatadecryptorfactorybuilder().setprovider("bc").build(skey));
            
            jcapgpobjectfactory    plainfact = new jcapgpobjectfactory(clear);
            
            object              message = plainfact.nextobject();
    
            while ( true ) {
                if (message instanceof pgpcompresseddata)
                {
                    pgpcompresseddata   cdata = (pgpcompresseddata)message;
                    jcapgpobjectfactory    pgpfact = new jcapgpobjectfactory(cdata.getdatastream());
                    
                    message = pgpfact.nextobject();
                }
                
                if (message instanceof pgpliteraldata)
                {
                    pgpliteraldata ld = (pgpliteraldata)message;

                    string outfilename = ld.getfilename();
                    if (outfilename.length() == 0)
                    {
                        outfilename = defaultfilename;
                    }

                    inputstream unc = ld.getinputstream();
                    outputstream fout = new bufferedoutputstream(new fileoutputstream(outfilename));

                    streams.pipeall(unc, fout);

                    fout.close();
                    break;
                }
                else if (message instanceof pgponepasssignaturelist)
                {
                    system.out.println("encrypted message contains a signed message - not literal data.");
                }
                else if (message instanceof pgpsignaturelist)
                {
                    system.out.println("encrypted message contains a signed message - not literal data.");
                }
                else
                {
                    throw new pgpexception("message is not a simple encrypted file - type unknown.");
                }
                message = plainfact.nextobject();
            }
            
            if (pbe.isintegrityprotected())
            {
                if (!pbe.verify())
                {
                    system.err.println("message failed integrity check");
                }
                else
                {
                    system.err.println("message integrity check passed");
                }
            }
            else
            {
                system.err.println("no message integrity check");
            }
        }
        catch (pgpexception e)
        {
            system.err.println(e);
            if (e.getunderlyingexception() != null)
            {
                e.getunderlyingexception().printstacktrace();
            }
        }
    }

    private static void encryptfile(
        string          outputfilename,
        string          inputfilename,
        string          enckeyfilename,
        boolean         armor,
        boolean         withintegritycheck)
        throws ioexception, nosuchproviderexception, pgpexception
    {
        outputstream out = new bufferedoutputstream(new fileoutputstream(outputfilename));
        pgppublickey enckey = pgpexampleutil.readpublickey(enckeyfilename);
        encryptfile(out, inputfilename, enckey, armor, withintegritycheck);
        out.close();
    }

    private static void encryptfile(
        outputstream    out,
        string          filename,
        pgppublickey    enckey,
        boolean         armor,
        boolean         withintegritycheck)
        throws ioexception, nosuchproviderexception
    {
        if (armor)
        {
            out = new armoredoutputstream(out);
        }

        try
        {
            byte[] bytes = pgpexampleutil.compressfile(filename, compressionalgorithmtags.zip);

            pgpencrypteddatagenerator encgen = new pgpencrypteddatagenerator(
                new jcepgpdataencryptorbuilder(pgpencrypteddata.cast5).setwithintegritypacket(withintegritycheck).setsecurerandom(new securerandom()).setprovider("bc"));

            encgen.addmethod(new jcepublickeykeyencryptionmethodgenerator(enckey).setprovider("bc"));

            outputstream cout = encgen.open(out, bytes.length);

            cout.write(bytes);
            cout.close();

            if (armor)
            {
                out.close();
            }
        }
        catch (pgpexception e)
        {
            system.err.println(e);
            if (e.getunderlyingexception() != null)
            {
                e.getunderlyingexception().printstacktrace();
            }
        }
    }

    public static void main(
        string[] args)
        throws exception
    {
        security.addprovider(new bouncycastleprovider());

        if (args.length == 0)
        {
            system.err.println("usage: keybasedfileprocessor -e|-d [-a|ai] file [secretkeyfile passphrase|pubkeyfile]");
            return;
        }

        if (args[0].equals("-e"))
        {
            if (args[1].equals("-a") || args[1].equals("-ai") || args[1].equals("-ia"))
            {
                encryptfile(args[2]   ".asc", args[2], args[3], true, (args[1].indexof('i') > 0));
            }
            else if (args[1].equals("-i"))
            {
                encryptfile(args[2]   ".bpg", args[2], args[3], falsetrue);
            }
            else
            {
                encryptfile(args[1]   ".bpg", args[1], args[2], falsefalse);
            }
        }
        else if (args[0].equals("-d"))
        {
            decryptfile(args[1], args[2], args[3].tochararray(), new file(args[1]).getname()   ".out");
        }
        else
        {
            system.err.println("usage: keybasedfileprocessor -d|-e [-a|ai] file [secretkeyfile passphrase|pubkeyfile]");
        }
    }
}



asdf


评论

# re: openpgp加密解密文件   回复     

2014-12-11 11:47 by
不错的文章,赞一个,我还想问楼主个问题,是不是文件加密了,就一定安全了呢

# re: openpgp加密解密文件   回复     

2014-12-11 19:45 by
@专业祛痘
从理论上来说,如果你的私钥和密码保存妥当,使用的密钥长度足够的话,被加密的文件是不可能被破解的. 就算要破解,需要消耗的时间也将是一个非常大的数字

# re: openpgp加密解密文件   回复     

2014-12-26 16:36 by
可以提供下这个pgpexampleutil类吗?

# re: openpgp加密解密文件 [未登录]  回复     

2015-05-29 15:14 by
@usherlight
为什么我的程序会进这个判断里,求解,急。。。。
else if (message instanceof pgponepasssignaturelist)
{
system.out.println("encrypted message contains a signed message - not literal data.");
}

# re: openpgp加密解密文件   回复     

2016-05-20 11:06 by
还是直接拿源码包中的 两个实例代码,直接运行就ok,这个类改的,解密文件跳过去了。

只有注册用户后才能发表评论。


网站导航:
              
 
网站地图