×

generatekey java

generatekey(java问题)

admin admin 发表于2022-09-06 21:21:24 浏览161 评论0

抢沙发发表评论

本文目录

java问题


-------------参考下------------------
JAVA加密算法的实现用例
对象
参数 algorithm 如:“DSA“
public final void initSign(PrivateKey privateKey)
throws InvalidKeyException
用指定的私钥初始化
参数:privateKey 所进行签名时用的私钥
public final void update(byte data)
throws SignatureException
public final void update(byte data)
throws SignatureException
public final void update(byte data, int off, int len)
throws SignatureException
添加要签名的信息
public final byte sign()
throws SignatureException
返回签名的数组,前提是initSign和update
public final void initVerify(PublicKey publicKey)
throws InvalidKeyException
用指定的公钥初始化
参数:publicKey 验证时用的公钥
public final boolean verify(byte signature)
throws SignatureException
验证签名是否有效,前提是已经initVerify初始化
参数: signature 签名数组
*/
import java.security.*;
import java.security.spec.*;
public class testdsa {
public static void main(String args) throws java.security.NoSuchAlgorithmException,java.lang.Exception {
testdsa my=new testdsa();
my.run();
}
public void run()
{
//数字签名生成密钥
//第一步生成密钥对,如果已经生成过,本过程就可以跳过,对用户来讲myprikey.dat要保存在本地
//而mypubkey.dat给发布给其它用户
if ((new java.io.File(“myprikey.dat“)).exists()==false) {
if (generatekey()==false) {
System.out.println(“生成密钥对败“);
return;
};
}
//第二步,此用户
//从文件中读入私钥,对一个字符串进行签名后保存在一个文件(myinfo.dat)中
//并且再把myinfo.dat发送出去
//为了方便数字签名也放进了myifno.dat文件中,当然也可分别发送
try {
java.io.ObjectInputStream in=new java.io.ObjectInputStream(new java.io.FileInputStream(“myprikey.dat“));
PrivateKey myprikey=(PrivateKey)in.readObject();
in.close();
// java.security.spec.X509EncodedKeySpec pubX509=new java.security.spec.X509EncodedKeySpec(bX509);
//java.security.spec.X509EncodedKeySpec pubkeyEncode=java.security.spec.X509EncodedKeySpec
String myinfo=“这是我的信息“; //要签名的信息
//用私钥对信息生成数字签名
java.security.Signature signet=java.security.Signature.getInstance(“DSA“);
signet.initSign(myprikey);
signet.update(myinfo.getBytes());
byte signed=signet.sign(); //对信息的数字签名
System.out.println (“signed(签名内容)=“+byte2hex(signed));
//把信息和数字签名保存在一个文件中
java.io.ObjectOutputStream out=new java.io.ObjectOutputStream(new java.io.FileOutputStream(“myinfo.dat“));
out.writeObject(myinfo);
out.writeObject(signed);
out.close();
System.out.println(“签名并生成文件成功“);
}
catch (java.lang.Exception e) {
e.printStackTrace();
System.out.println(“签名并生成文件失败“);
};
//第三步
//其他人通过公共方式得到此户的公钥和文件
//其他人用此户的公钥,对文件进行检查,如果成功说明是此用户发布的信息.
//
try {
java.io.ObjectInputStream in=new java.io.ObjectInputStream(new java.io.FileInputStream(“mypubkey.dat“));
PublicKey pubkey=(PublicKey)in.readObject();
in.close();
System.out.println(pubkey.getFormat());
in=new java.io.ObjectInputStream(new java.io.FileInputStream(“myinfo.dat“));
String info=(String)in.readObject();
byte signed=(byte)in.readObject();
in.close();
java.security.Signature signetcheck=java.security.Signature.getInstance(“DSA“);
signetcheck.initVerify(pubkey);
signetcheck.update (info.getBytes());
if (signetcheck.verify(signed)) {
System.out.println(“info=“+info);
System.out.println(“签名正常“);
}
else System.out.println(“非签名正常“);
}
catch ( java.lang.Exception e) {e.printStackTrace();};
}
//生成一对文件myprikey.dat和mypubkey.dat---私钥和公钥,
//公钥要用户发送(文件,网络等方法)给其它用户,私钥保存在本地
public boolean generatekey()
{
try {
java.security.KeyPairGenerator keygen=java.security.KeyPairGenerator.getInstance(“DSA“);
// SecureRandom secrand=new SecureRandom();
// secrand.setSeed(“tttt“.getBytes()); //初始化随机产生器
// keygen.initialize(576,secrand); //初始化密钥生成器
keygen.initialize(512);
KeyPair keys=keygen.genKeyPair();
// KeyPair keys=keygen.generateKeyPair(); //生成密钥组
PublicKey pubkey=keys.getPublic();
PrivateKey prikey=keys.getPrivate();
java.io.ObjectOutputStream out=new java.io.ObjectOutputStream(new java.io.FileOutputStream(“myprikey.dat“));
out.writeObject(prikey);
out.close();
System.out.println(“写入对象 prikeys ok“);
out=new java.io.ObjectOutputStream (new java.io.FileOutputStream(“mypubkey.dat“));
out.writeObject(pubkey);
out.close();
System.out.println(“写入对象 pubkeys ok“);
System.out.println(“生成密钥对成功“);
return true;
}
catch (java.lang.Exception e) {
e.printStackTrace();
System.out.println(“生成密钥对失败“);
return false;
};
}
public String byte2hex(byte b)
{
String hs=““;
String stmp=““;
for (int n=0;n《b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+“0“+stmp;
else hs=hs+stmp;
if (n《b.length-1) hs=hs+“:“;
}
return hs.toUpperCase();
}
}
2.4. DESede/DES对称算法
首先生成密钥,并保存(这里并没的保存的代码,可参考DSA中的方法)
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
用密钥加密明文(myinfo),生成密文(cipherByte)
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte cipherByte=c1.doFinal(myinfo.getBytes());
传送密文和密钥,本文没有相应代码可参考DSA
.............
用密钥解密密文
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte clearByte= c1.doFinal(cipherByte);
相对来说对称密钥的使用是很简单的,对于JCE来讲支技DES,DESede,Blowfish三种加密术
对于密钥的保存各传送可使用对象流或者用二进制编码,相关参考代码如下
SecretKey deskey = keygen.generateKey();
byte desEncode=deskey.getEncoded();
javax.crypto.spec.SecretKeySpec destmp=new javax.crypto.spec.SecretKeySpec(desEncode,Algorithm);
SecretKey mydeskey=destmp;
相关API
KeyGenerator 在DSA中已经说明,在添加JCE后在instance进可以如下参数
DES,DESede,Blowfish,HmacMD5,HmacSHA1
javax.crypto.Cipher 加/解密器
public static final Cipher getInstance(java.lang.String transformation)
throws java.security.NoSuchAlgorithmException ,
NoSuchPaddingException
返回一个指定方法的Cipher对象
参数:transformation 方法名(可用 DES,DESede,Blowfish)
public final void init(int opmode, java.security.Key key)
throws java.security.InvalidKeyException
用指定的密钥和模式初始化Cipher对象
参数:opmode 方式(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
key 密钥
public final byte doFinal(byte input)
throws java.lang.IllegalStateException,
IllegalBlockSizeException,
BadPaddingException
对input内的串,进行编码处理,返回处理后二进制串,是返回解密文还是加解文由init时的opmode决定
注意:本方法的执行前如果有update,是对updat和本次input全部处理,否则是本inout的内容
/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;
public class testdes {
public static void main(String args){
testdes my=new testdes();
my.run();
}
public void run() {
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm=“DES“; //定义 加密算法,可用 DES,DESede,Blowfish
String myinfo=“要加密的信息“;
try {
//生成密钥
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
//加密
System.out.println(“加密前的二进串:“+byte2hex( myinfo.getBytes()));
System.out.println(“加密前的信息:“+myinfo);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte cipherByte=c1.doFinal(myinfo.getBytes());
System.out.println(“加密后的二进串:“+byte2hex(cipherByte));
//解密
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte clearByte=c1.doFinal(cipherByte);
System.out.println (“解密后的二进串:“+byte2hex(clearByte));
System.out.println(“解密后的信息:“+(new String(clearByte)));
}
catch (java.security.NoSuchAlgorithmException e1) {e1.printStackTrace();}
catch (javax.crypto.NoSuchPaddingException e2) {e2.printStackTrace();}
catch (java.lang.Exception e3) {e3.printStackTrace();}
}
public String byte2hex(byte b) //二行制转字符串
{
String hs=““;
String stmp=““;
for (int n=0;n《b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+“0“+stmp;
else hs=hs+stmp;
if (n《b.length-1 ) hs=hs+“:“;
}
return hs.toUpperCase();
}
}
2.5. Diffie-Hellman密钥一致协议
公 开密钥密码体制的奠基人Diffie和Hellman所提出的 “指数密钥一致协议“ (Exponential Key Agreement Protocol),该协议不要求别的安全性先决条件,允许两名用户在公开媒体上交换信息以生成 “一致“的,可以共享的密钥。在JCE的中实现用户alice生成DH类型的密钥对,如果长度用1024生成的时间请,推荐第一次生成后保存 DHParameterSpec,以便下次使用直接初始化.使其速度加快
System.out.println(“ALICE: 产生 DH 对 ...“);
KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance(“DH“);
aliceKpairGen.initialize(512);
KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
alice生成公钥发送组bob
byte alicePubKeyEnc = aliceKpair.getPublic().getEncoded();
bob从alice发送来的公钥中读出DH密钥对的初始参数生成bob的DH密钥对
注意这一步一定要做,要保证每个用户用相同的初始参数生成的
DHParameterSpec dhParamSpec = ((DHPublicKey)alicePubKey).getParams();
KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance(“DH“);
bobKpairGen.initialize(dhParamSpec);
KeyPair bobKpair = bobKpairGen.generateKeyPair();
bob根据alice的公钥生成本地的DES密钥
KeyAgreement bobKeyAgree = KeyAgreement.getInstance(“DH“);
bobKeyAgree.init(bobKpair.getPrivate());
bobKeyAgree.doPhase (alicePubKey, true);
SecretKey bobDesKey = bobKeyAgree.generateSecret(“DES“);
bob已经生成了他的DES密钥,他现把他的公钥发给alice,
byte bobPubKeyEnc = bobKpair.getPublic().getEncoded();
alice根据bob的公钥生成本地的DES密钥
,,,,,,解码
KeyAgreement aliceKeyAgree = KeyAgreement.getInstance(“DH“);
aliceKeyAgree.init(aliceKpair.getPrivate());
aliceKeyAgree.doPhase (bobPubKey, true);
SecretKey aliceDesKey = aliceKeyAgree.generateSecret(“DES“);
bob和alice能过这个过程就生成了相同的DES密钥,在这种基础就可进行安全能信
常用API
java.security.KeyPairGenerator 密钥生成器类
public static KeyPairGenerator getInstance(String algorithm)
throws NoSuchAlgorithmException
以指定的算法返回一个KeyPairGenerator 对象
参数: algorithm 算法名.如:原来是DSA,现在添加了 DiffieHellman(DH)
public void initialize(int keysize)
以指定的长度初始化KeyPairGenerator对象,如果没有初始化系统以1024长度默认设置
参数:keysize 算法位长.其范围必须在 512 到 1024 之间,且必须为 64 的倍数
注意:如果用1024生长的时间很长,最好生成一次后就保存,下次就不用生成了
public void initialize(AlgorithmParameterSpec params)
throws InvalidAlgorithmParameterException
以指定参数初始化
javax.crypto.interfaces.DHPublicKey
public DHParameterSpec getParams()
返回
java.security.KeyFactory
public static KeyFactory getInstance(String algorithm)
throws NoSuchAlgorithmException
以指定的算法返回一个KeyFactory
参数: algorithm 算法名:DSH,DH
public final PublicKey generatePublic(KeySpec keySpec)
throws InvalidKeySpecException
根据指定的key说明,返回一个PublicKey对象
java.security.spec.X509EncodedKeySpec
public X509EncodedKeySpec(byte encodedKey)
根据指定的二进制编码的字串生成一个key的说明
参数:encodedKey 二进制编码的字串(一般能过PublicKey.getEncoded()生成)
javax.crypto.KeyAgreement 密码一至类
public static final KeyAgreement getInstance(java.lang.String algorithm)
throws java.security.NoSuchAlgorithmException
返回一个指定算法的KeyAgreement对象
参数:algorithm 算法名,现在只能是DiffieHellman(DH)
public final void init(java.security.Key key)
throws java.security.InvalidKeyException
用指定的私钥初始化
参数:key 一个私钥
public final java.security.Key doPhase(java.security.Key key,
boolean lastPhase)
throws java.security.InvalidKeyException,
java.lang.IllegalStateException
用指定的公钥进行定位,lastPhase确定这是否是最后一个公钥,对于两个用户的
情况下就可以多次定次,最后确定
参数:key 公钥
lastPhase 是否最后公钥
public final SecretKey generateSecret(java.lang.String algorithm)
throws java.lang.IllegalStateException,
java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException
根据指定的算法生成密钥
参数:algorithm 加密算法(可用 DES,DESede,Blowfish)
*/
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import com.sun.crypto.provider.SunJCE;
public class testDHKey {
public static void main(String argv) {
try {
testDHKey my= new testDHKey();
my.run();
} catch (Exception e) {
System.err.println(e);
}
}
private void run() throws Exception {
Security.addProvider (new com.sun.crypto.provider.SunJCE());
System.out.println(“ALICE: 产生 DH 对 ...“);
KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance(“DH“);
aliceKpairGen.initialize (512);
KeyPair aliceKpair = aliceKpairGen.generateKeyPair(); //生成时间长
// 张三(Alice)生成公共密钥 alicePubKeyEnc 并发送给李四(Bob) ,
//比如用文件方式,socket.....
byte alicePubKeyEnc = aliceKpair.getPublic ().getEncoded();
//bob接收到alice的编码后的公钥,将其解码
KeyFactory bobKeyFac = KeyFactory.getInstance(“DH“);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (alicePubKeyEnc);
PublicKey alicePubKey = bobKeyFac.generatePublic(x509KeySpec);
System.out.println(“alice公钥bob解码成功“);
// bob必须用相同的参数初始化的他的DH KEY对,所以要从Alice发给他的公开密钥,
//中读出参数,再用这个参数初始化他的 DH key对
//从alicePubKye中取alice初始化时用的参数
DHParameterSpec dhParamSpec = ((DHPublicKey)alicePubKey).getParams();
KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance(“DH“);
bobKpairGen.initialize(dhParamSpec);
KeyPair bobKpair = bobKpairGen.generateKeyPair();
System.out.println(“BOB: 生成 DH key 对成功“);
KeyAgreement bobKeyAgree = KeyAgreement.getInstance(“DH“);
bobKeyAgree.init(bobKpair.getPrivate());
System.out.println(“BOB: 初始化本地key成功“);
//李四(bob) 生成本地的密钥 bobDesKey
bobKeyAgree.doPhase(alicePubKey, true);
SecretKey bobDesKey = bobKeyAgree.generateSecret(“DES“);
System.out.println(“BOB: 用alice的公钥定位本地key,生成本地DES密钥成功“);
// Bob生成公共密钥 bobPubKeyEnc 并发送给Alice,
//比如用文件方式,socket.....,使其生成本地密钥
byte bobPubKeyEnc = bobKpair.getPublic().getEncoded();
System.out.println(“BOB向ALICE发送公钥“);
// alice接收到 bobPubKeyEnc后生成bobPubKey
// 再进行定位,使aliceKeyAgree定位在bobPubKey
KeyFactory aliceKeyFac = KeyFactory.getInstance(“DH“);
x509KeySpec = new X509EncodedKeySpec(bobPubKeyEnc);
PublicKey bobPubKey = aliceKeyFac.generatePublic(x509KeySpec);
System.out.println(“ALICE接收BOB公钥并解码成功“);
;
KeyAgreement aliceKeyAgree = KeyAgreement.getInstance(“DH“);
aliceKeyAgree.init(aliceKpair.getPrivate());
System.out.println(“ALICE: 初始化本地key成功“);
aliceKeyAgree.doPhase(bobPubKey, true);
// 张三(alice) 生成本地的密钥 aliceDesKey
SecretKey aliceDesKey = aliceKeyAgree.generateSecret(“DES“);
System.out.println(“ALICE: 用bob的公钥定位本地key,并生成本地DES密钥“);
if (aliceDesKey.equals(bobDesKey)) System.out.println (“张三和李四的密钥相同“);
//现在张三和李四的本地的deskey是相同的所以,完全可以进行发送加密,接收后解密,达到
//安全通道的的目的
/*
* bob用bobDesKey密钥加密信息
*/
Cipher bobCipher = Cipher.getInstance(“DES“);
bobCipher.init(Cipher.ENCRYPT_MODE, bobDesKey);
String bobinfo= “这是李四的机密信息“;
System.out.println(“李四加密前原文:“+bobinfo);
byte cleartext =bobinfo.getBytes();
byte ciphertext = bobCipher.doFinal(cleartext);
/*
* alice用aliceDesKey密钥解密
*/
Cipher aliceCipher = Cipher.getInstance(“DES“);
aliceCipher.init(Cipher.DECRYPT_MODE, aliceDesKey);
byte recovered = aliceCipher.doFinal(ciphertext);
System.out.println(“alice解密bob的信息:“+(new String(recovered)));
if (!java.util.Arrays.equals(cleartext, recovered))
throw new Exception(“解密后与原文信息不同“);
System.out.println(“解密后相同“);
}
}

java web系统如何制作License


publicInterfaceLecenseFactory{/***生成公钥、私钥对。公钥公开,注意保管好私钥(如果泄露,则有可能被hacker随意创建license*/publicstaticvoidgenerateKey()throwsException;
/**用私钥对license进行数据签名*/
publicstaticsynchronizedvoidsign(Licenselicense)throwsException;/***验证license是否合法。
*首先验证Mac地址是否有改变,有的话则非法。(防止用户自由拷贝软件)。
*然后根据公钥验证签名是否合法。*/booleanvalidate(Licenselicense)throwsException;}
License创建及验证步骤代码
第一步:生成公钥、私钥对。公钥公开,注意保管好私钥(如果泄露,则有可能被随意创建license)。
LicenseFactory.generateKey();
第二步:根据产品、版本、Mac地址、有效期等信息,签名产生注册号,并将该注册号复制到license中。
第三步:利用公钥对license进行合法性验证。可以在软件代码的重要模块中加入下面的验证,比如登录模块关键字:java, web, security java web系

java如何用Aes加密和解密


你解密的key必须是加密的key啊
你看看,你解密的时候又KeyGenerator.getInstance(“AES“).generateKey();这是重新搞了一个key啊,当然解不出来了
我估计你这代码人家原先是写在一起的吧,加密完了再直接解密给你看,人家只generateKey一次,自然很顺利,你分成了两个例子,居然分别generateKey,自然失败
-JAVA

java des 加密 解密 密钥随机取得方法


java DES 加密 解密 生成随机密钥
举例说明:
//保存生成的key
public static void saveDesKey() {
try {
SecureRandom sr = new SecureRandom();
// 选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance(“DES“);
kg.init(sr);
// 相对路径 需要新建 conf 文件夹
// String fileName = “conf/DesKey.xml“;
// 绝对路径
String fileName = “d:/DesKey.xml“;
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密钥
Key key = kg.generateKey();
oos.writeObject(key);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取生成的key
public static Key getKey() {
Key kp = null;
try {
// 相对路径 需要新建 conf 文件夹
// String fileName = “conf/DesKey.xml“;
// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);
// 绝对路径
String fileName = “d:/DesKey.xml“;
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (Key) oos.readObject();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
return kp;
}
//加密开始
public static void encrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance(“DES“);
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte buffer = new byte;
int r;
while ((r = cis.read(buffer)) 》 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}
//解密开始
public static void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance(“DES“);
cipher.init(Cipher.DECRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte buffer = new byte;
int r;
while ((r = is.read(buffer)) 》= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
}
//des加密主方法
public class DES {
public static void main(String args) throws Exception {
Encrypt.saveDesKey();
System.out.println(“生成key“);
Encrypt.getKey();
System.out.println(“获取key“);
Encrypt.encrypt(“d:\\hello.txt“, “d:\\encrypt.txt“);
System.out.println(“加密“);
Encrypt.decrypt(“d:\\encrypt.txt“, “d:\\decrypt.txt“);
System.out.println(“解密“);
}
以上方法亲测可用。
-java

C语言 文件加密解密


根据你的需要,修改了之前的代码。

#include 《stdio.h》
#include 《string.h》
#include 《stdlib.h》
#include 《time.h》
const unsigned int MAX_KEY_LENGTH = 1000;
int encode(char const *datafile, char const *keyfill);
int decode(char const *datafile, char const *keyfile);
int loadKey(char const *keyfile, int *keys, unsigned int size);
int saveKey(char const *keyfile, int *keys, unsigned int size);
int generateKey(int *keys, unsigned int size);
int main(int argc, char const *argv)
{
    char datafile = “encrypted.txt“;
    char keyfile = “key.txt“;
    int retcode, choice, loop = 1;
    char ch = {’\0’};
    while(1)
    {
        printf(“1. Encryption.\n“);
        printf(“2. Decryption.\n“);
        printf(“3. Exit.\n“);
        printf(“Selection (1,2,3):“);        
        fgets(ch, sizeof(ch), stdin);
        sscanf(ch, “%d“, &choice);        
        switch(choice)
        {
            case 1:                 
                retcode = encode(datafile, keyfile);
                if (retcode != 0) printf(“error, %d\0“, retcode);
                break;
                
            case 2:                                
                retcode = decode(datafile, keyfile);
                if (retcode != 0) printf(“error, %d\0“, retcode);
                break;
                
            case 3:
                loop = 0;
                break;
            default:
                ;
                break;    
        }        
        if (0 == loop) break;
    }    
    return 0;
}

int generateKey(int *keys, unsigned int size) 
{
    char str=“0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,./;\“’《》?“;    
    size_t str_len = sizeof(str)/sizeof(str);
    int i;
    
    srand(time(NULL));
    for (i = 0; i 《 size; ++i)
        keys[i] = str[rand() % str_len];
    return 0;
}
int loadKey(char const *keyfile, int *keys, unsigned int size)
{
    int i = 0;
    FILE *pfile;    
    int retcode = 0;
    pfile = fopen(keyfile, “r“);
    if (pfile == NULL) return -1;
    while ( !feof(pfile) ) {
        if (i 《 size)
            fscanf(pfile, “%d “, &keys[i++]);
        else        
            break;        
    }
    fclose(pfile);
    return i;
}
int saveKey(char const *keyfile, int *keys, unsigned int size) 
{
    FILE *pfile;
    int i;
    pfile = fopen(keyfile, “w“);
    if (pfile == NULL) return -1;
    
    for(i = 0; i 《 size; ++i) {
        fprintf(pfile, “%d “, keys[i]);
    }
    fclose(pfile);
    return 0;
}
int encode(char const *datafile, char const *keyfile) 
{
    char original[MAX_KEY_LENGTH] = {’\0’};
    char encrypted[MAX_KEY_LENGTH] = {’\0’};
    int i, size;
    int keys[MAX_KEY_LENGTH];
    FILE *pdatafile, *pkeyfile;
    
    pkeyfile = fopen(keyfile, “w“);
    if (NULL == pkeyfile) return -1;
    fclose(pkeyfile);    
    puts(“ input message:“);
    gets(original);
    size = strlen(original);
    if (0 != generateKey(keys, size)) return -2;
    if (0 != saveKey(keyfile, keys, size) ) return -3;
    
    pdatafile = fopen(datafile, “w“);
    if (NULL == pdatafile) return -4;
    for (i = 0; i 《 size; ++i) {
        encrypted[i] = original[i] + keys[i];
        fputc(encrypted[i], pdatafile);
        fputc(encrypted[i], stdout);
    }
    printf(“\n“);
    fclose(pdatafile);
    return 0;
}
int decode(char const *datafile, char const *keyfile)
{
    FILE *pdatafile, *pkeyfile;
    int keys[MAX_KEY_LENGTH] = {0};
    char original[MAX_KEY_LENGTH] = {’\0’};
    char encrypted[MAX_KEY_LENGTH] = {’\0’};
    int i, size;
    pkeyfile = fopen(keyfile, “r“);
    if (NULL == pkeyfile) return -1;
    fclose(pkeyfile);
    pdatafile = fopen(datafile, “r“);
    if (NULL == pdatafile) return -2;
    
    fscanf(pdatafile,“%s“,encrypted);
    fclose(pdatafile);
    size = loadKey(keyfile, keys, MAX_KEY_LENGTH);
    if (size 《 1) return -3;
    
    for (i = 0; i 《 strlen(encrypted); ++i) {
        original[i] = encrypted[i]-keys[i];
        fputc(original[i], stdout);
    }
    printf(“\n“);
    return 0;
}

运行结果:-JAVA

1. Encryption.
2. Decryption.
3. Exit.
Selection (1,2,3):1
 input message:
this is A test!
╓┐»╞Lñ╗ù|t▄╬╢╒è
1. Encryption.
2. Decryption.
3. Exit.
Selection (1,2,3):2
this is A test!
1. Encryption.
2. Decryption.
3. Exit.
Selection (1,2,3):3-java


如何保存密钥文件更安全


下面是一个进行加密的工具类,加密类会产生一个密钥文件,密钥文件保存到了硬盘文件中,程序中要进行对数据的加解密操作。密钥文件保存在文件,别人也就可以读取密钥文件,获得加密数据的内容。publicclass EncryptUtil { privatestatic String keyPath = null; privatestatic String getKeyPath() { keyPath = “c:\\yhb.des“; return keyPath; } /** * 对称加密-产生密钥 */publicstaticvoid generatorKey() { SecretKey key = null; try { // 指定算法,这里为DES;如果想用Blowfish算法,则用getInstance(“Blowfish“) // BouncyCastle基本上支持所有通用标准算法 KeyGenerator keygen = KeyGenerator.getInstance(“DES“); // 指定密钥长度,长度越高,加密强度越大 keygen.init(56); // 产生密钥 key = keygen.generateKey(); // 构造输出文件,这里的目录是动态的,根据用户名称来构造目录 ObjectOutputStream keyFile = new ObjectOutputStream( new FileOutputStream(getKeyPath())); keyFile.writeObject(key); keyFile.close(); } catch (NoSuchAlgorithmException e5) { e5.printStackTrace(); System.exit(0); } catch (IOException e4) { e4.printStackTrace(); System.exit(0); } } /** * 对称加密-读取密钥. */privatestatic SecretKey getSecretKey() { // 从密钥文件中读密钥 SecretKey key = null; try { ObjectInputStream keyFile = new ObjectInputStream( new FileInputStream(getKeyPath())); key = (SecretKey) keyFile.readObject(); keyFile.close(); } catch (FileNotFoundException ey1) { e1.printStackTrace(); System.exit(0); } catch (Exception ey2) { e2.printStackTrace(); } return key; } /** * 加密文本信息. */publicstatic String encrypt(String encryptStr) { SecretKey key = getSecretKey(); Cipher cipher = null; try { // 设置算法,应该与加密时的设置一样 cipher = Cipher.getInstance(“DES“); // 设置解密模式 cipher.init(Cipher.ENCRYPT_MODE, key); } catch (Exception ey3) { ey3.printStackTrace(); } byte data = null; try { data = cipher.doFinal(encryptStr.getBytes()); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } encryptStr = Base64.encodeBase64String(data); return encryptStr; } /** * 解密文本信息. */publicstatic String decrypt(String decryptStr) { SecretKey key = getSecretKey(); // 用key产生Cipher Cipher cipher = null; try { // 设置算法,应该与加密时的设置一样 cipher = Cipher.getInstance(“DES“); // 设置解密模式 cipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception ey3) { ey3.printStackTrace(); System.exit(0); } byte data = Base64.decodeBase64(decryptStr); try { data = cipher.doFinal(data); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } decryptStr = new String(data); return decryptStr; } }
-JAVA

为什么解密后和明文不一样


第一确定加密后的文件长度和加密前长度一致
因为如果一个字符‘a’加密后成了’\0‘的话,你这是用了strcat这样的函数就会默认把这个’\0’去掉的,
所以不要用strcat之类的函数,
要用指针。看看是不是这个问题。
-java