?? publicexample.java
字號:
package ss.logic;
import java.security.Key;
import javax.crypto.Cipher;
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.*;
public class PublicExample{
public static void main(String[] args) throws Exception{
String plainTextStr = "ABCDFDFEFERE";
byte[] plainText = plainTextStr.getBytes("UTF8");
System.out.println(plainTextStr);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom secrand = new SecureRandom();
secrand.setSeed("CSR".getBytes());
keyGen.initialize(1024, secrand);
KeyPair key = keyGen.generateKeyPair();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());
byte[] cipherText = cipher.doFinal(plainText);
System.out.println(new String(cipherText, "UTF8"));
cipher.init(Cipher.DECRYPT_MODE,key.getPrivate());
byte[] newPlainText=cipher.doFinal(cipherText);
System.out.println(new String(newPlainText, "UTF8"));
String myinfo = "orderId=10dkfadsdfdffksdkssdkd&amount=80&orderTime=20060509";
Signature signet = Signature.getInstance("MD5withRSA");
signet.initSign(key.getPrivate());
signet.update(myinfo.getBytes());
byte[] signed = signet.sign();
System.out.println("signed=" + new String(signed));
// System.out.println("info=" + myinfo);
String info = "orderId=10dkfadsdfdffksdkssdkd&amount=80&orderTime=20060509";
signet.initVerify(key.getPublic());
// System.out.println(info.getBytes());
// System.out.println(info.getBytes());
signet.update(info.getBytes());
if (signet.verify(signed)) {
//System.out.println("info=" + info);
System.out.println("yes!!");
}
else System.out.println("Wrong!!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -