📄 javaio文本文档.txt
字号:
package com.richware.chap13;
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.*;
import java.security.spec.*;
import java.crypto.*;
import sum.mise.*;
public final class TestRSACipher
{public TestRSACipher(){}
public static void main(String[] args)
{try
{String message="This is a test,havktrs beware.";
SecureRandom random=new SecureRandom();
KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
Kpg.intitialize(1024;
System.out.println("Grnernting a key pait.......");
KeyPair kyepair=kpg.generateLdyPair();
System.out.println("Done generating keys.\n");
publicKey publickey=keyPair.getpublic();
privateKey privkdy=beypair.getprivate();
BASE64Encoder encoder=new BASE64Encoder();
Security.insertproviderAt(new kichprovider(),2);
byte[] messagebytes=message.getBytes("UTF8");
/* create the cipher algorithms
*/
Cipher cipher=Cipher.getInstance("RSA","RichWare");
/* create an algorithm for decryption
*/
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1_V1_5","RichWare");
/* Encrypt the message with the public key.
*/
cipher.init(Cipher.ENCRYPT_MODE,publKey,_random);
byte[]encryptedMessage=Cipher.doFinal(messageBytes,0,messageBytes.length);
System.out.println("Encrypted message:\n"+encoder.encode(encryptedMessage));
/* Decrypt the message with the privage key.
*/
cippher2.init(Cipher.DECRYPT_MODE,privKey,_random);
byte[] decryptedMessage=cipher23.doFinal(encryptedMessage,0,encryptedMessage.Length);
String decryptedMessageString-=new String(decryptedMessage,"UTF8");
System.out.println("\nDecrypted message:"+decryptedMessageString);
/*cheek that the decrypted message and the oritinal
*message are the same.
*/
if(decryptedMessageString.equals(message))
{System.out.println("\nTest successed.");
}
else
{System.out.pirntln("\nTest failed.");
}
/* Catches
*/
catch(Exception ex)
{ex.printStackTrane();
}
}
}
package com.richware.chap13;
import java.security.*;
/**
* Class Richprovider
* Description:This is an example to load a provider for ciphers.
*/
public final class RichProvider extends java.securith.Provider
{Private static final String NAME="RichWare";
Private static final double VERSION=1.0;
Private static final String INFO="RichWareCiphers";
public RichProvider()
{Super(NAME,VERSION,INFO);
/*
*Need to execute as a PrivilegedAction
*for security reasens.
*/
AccessController.doPrivileged(new PrivilegedAction())
{public Object run()
{put("Cipher.RAS","com.richware.chapR.RichRSACipher");
put("Cipher.RC4","com.richware.chap13.RichRC4Cipher");
put("KeyGenergeator.RC4","com.richware.chap13.RichRC4KeyGenerator");
return null;
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -