コードコピーは次のとおりです。
/*stringdede(3des)暗号化*/
java.security.securityをインポートします。
javax.crypto.cipherをインポートします。
javax.crypto.secretkeyをインポートします。
javax.crypto.spec.secretkeyspecをインポートします。
sun.misc.base64decoderをインポートします。
sun.misc.base64encoderをインポートします。
パブリッククラスDES3 {
プライベート静的な弦アルゴリズム= "desede";
// des、dede、blowfish
// keybyteは24バイトの長さの暗号化キーです
// SRCは暗号化されたデータバッファー(ソース)です
public static string encryptmode(byte [] keybyte、byte [] src)
{
試す
{
//キーを生成します
SecretKey Desktop = new SecretKeyspec(keybyte、algorithm);
//暗号化
cipher c1 = cipher.getInstance(アルゴリズム);
c1.init(cipher.encrypt_mode、desktop);
//暗号化を開始します
byte [] encryptedbytearray = c1.dofinal(src);
//暗号化操作後、Byte []をBase64の文字列に変換します
base64encoder enc = new base64encoder();
return enc.encode(encryptedbytearray);
}
catch(java.security.nosuchalgorithmexception e1)
{
e1.printstacktrace();
}
catch(javax.crypto.nosuchpaddingexception e2)
{
e2.printstacktrace();
}
catch(java.lang.exception e3)
{
e3.printstacktrace();
}
nullを返します。
}
// keybyteは24バイトの長さの暗号化キーです
// SRCは暗号化されたバッファーです
public static byte [] decryptmode(byte [] keybyte、string src)
{
試す {
//キーを生成します
SecretKey Desktop = new SecretKeyspec(keybyte、algorithm);
// Decrypt
cipher c1 = cipher.getInstance(アルゴリズム);
c1.init(cipher.decrypt_mode、desktop);
//復号化操作の前
base64Decoder dec = new Base64Decoder();
byte [] encryptedbytearray = dec.decodebuffer(src);
//復号化操作は、base64の文字列をbyteに変換します[]
c1.dofinal(necryptedbytearray);
} catch(java.security.nosuchalgorithmexception e1){
e1.printstacktrace();
} catch(javax.crypto.nosuchpaddingexception e2){
e2.printstacktrace();
} catch(java.lang.exception e3){
e3.printstacktrace();
}
nullを返します。
}
// 16進文字列に変換します
public static string byte2hex(byte [] b){
文字列hs = "";
文字列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;
それ以外
HS = HS + STMP;
if(n <b.length -1)
hs = hs + ":";
}
return hs.touppercase();
}
public static void main(string [] args)
{
//新しいセキュリティアルゴリズムを追加します。
security.addprovider(new com.sun.crypto.provider.sunjce());
final byte [] keybytes = {0x11、0x22、0x4f、0x58、(byte)0x88、0x10、
0x40、0x38、0x28、0x25、0x79、0x51、(byte)0xcb、(byte)0xdd、
0x55、0x66、0x77、0x29、0x74、(byte)0x98、0x30、0x40、0x36、
(byte)0xe2}; // 24バイトキー
文字列szsrc = "これは3desテストです。テスト";
system.out.println( "暗号化の前にスタンド:" + szsrc);
string encoded = encryptMode(keybytes、szsrc.getBytes());
System.out.println( "暗号化された文字列:" +エンコード);
byte [] srcbytes = decryptmode(keybytes、エンコード);
system.out.println( "decrypted string:" +(new String(srcbytes)));
}
}