It took me two weeks to complete the registration of a development platform. I won't say what platform I guess! String ARGs = ""; the original encrypted information, with a company name, had to be removed.
Import java. Io. bytearrayoutputstream;
Import java. Security. keyfactory;
Import java. Security. keypair;
Import java. Security. keypairgenerator;
Import java. security. Security;
Import java. Security. spec. pkcs8encodedkeyspec;
Import java. Security. spec. x509encodedkeyspec;
Import javax. crypto. cipher;
Import org. bouncycastle. JCE. provider. bouncycastleprovider;
Public class rsaok {
Private byte [] privatekey;
Private byte [] publickey;
Public static void main (string [] ARGs ){
Bouncycastleprovider = new bouncycastleprovider ();
If (Security. getproperty (bouncycastleprovider. getname () = NULL)
Security. addprovider (bouncycastleprovider );
Rsaok T = new rsaok ();
T. Key ();
T. Encrypt ();
}
Public void encrypt (){
String ARGs = "";
Byte abyte0 [] = NULL;
Cipher cipher;
Byte abyte1 [];
Int I;
Bytearrayoutputstream;
Int K;
Try {
Pkcs8encodedkeyspec pkcs8encodedkeyspec = new pkcs8encodedkeyspec (
Privatekey );
Keyfactory = keyfactory. getinstance ("RSA ");
Java. Security. privatekey = keyfactory
. Generateprivate (pkcs8encodedkeyspec );
Cipher = cipher. getinstance ("RSA", "BC ");
Cipher. INIT (2, privatekey );
Abyte1 = args. getbytes ();
I = cipher. getblocksize ();
Bytearrayoutputstream = new bytearrayoutputstream ();
For (k = 0; k <abyte1.length ;){
Int J;
If (abyte1.length-k> = I)
J = I;
Else
J = abyte1.length-K;
Bytearrayoutputstream. Write (Cipher. dofinal (abyte1, K, j ));
K + = I;
}
Bytearrayoutputstream. Flush ();
Bytearrayoutputstream. Close ();
Abyte0 = bytearrayoutputstream. tobytearray ();
System. Out. println (byte2hex (abyte0 ));
Decrypt (abyte0 );
} Catch (exception e ){
E. printstacktrace ();
}
}
Public void decrypt (byte [] abyte1 ){
Try {
String A = "success ";
X509encodedkeyspec x509encodedkeyspec = new x509encodedkeyspec (publickey );
// X509encodedkeyspec x509encodedkeyspec = new x509encodedkeyspec (hex2byte ("signature "));
Cipher cipher;
Int I;
Bytearrayoutputstream;
Int K;
Keyfactory = keyfactory. getinstance ("RSA ");
Java. Security. publickey = keyfactory
. Generatepublic (x509encodedkeyspec );
Cipher = cipher. getinstance ("RSA", "BC ");
Cipher. INIT (2, publickey );
I = cipher. getblocksize ();
Bytearrayoutputstream = new bytearrayoutputstream ();
For (k = 0; k <abyte1.length ;){
Int J;
If (abyte1.length-k> = I)
J = I;
Else
J = abyte1.length-K;
Bytearrayoutputstream. Write (Cipher. dofinal (abyte1, K, j ));
K + = I;
}
Byte abyte0 [];
Bytearrayoutputstream. Flush ();
Bytearrayoutputstream. Close ();
Abyte0 = bytearrayoutputstream. tobytearray ();
System. Out. println (new string (abyte0 ));
} Catch (exception e ){
E. printstacktrace ();
}
}
Public void key (){
Try {
Keypairgenerator KPG = NULL;
KPG = keypairgenerator. getinstance ("RSA", "BC ");
KPG. initialize (1024 );
Keypair Kp = KPG. generatekeypair ();
Privatekey = KP. getprivate (). getencoded ();
System. Out. println ("privatekey:" + byte2hex (privatekey ));
Publickey = KP. getpublic (). getencoded ();
System. Out. println ("publickey:" + byte2hex (publickey ));
} Catch (exception e ){
E. printstacktrace ();
}
}
Private 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 ();
}
Public byte [] hex2byte (string HEX) throws illegalargumentexception {
If (Hex. Length () % 2! = 0 ){
Throw new illegalargumentexception ();
}
Char [] arr = Hex. tochararray ();
Byte [] B = new byte [Hex. Length ()/2];
For (INT I = 0, j = 0, L = Hex. Length (); I <L; I ++, J ++ ){
String swap = "" + arr [I ++] + arr [I];
Int byteint = integer. parseint (SWAP, 16) & 0xff;
B [J] = new INTEGER (byteint). bytevalue ();
}
Return B;
}
}