- PR -

javaで暗号化するやりかた

1
投稿者投稿内容
kirito
常連さん
会議室デビュー日: 2004/03/19
投稿数: 24
投稿日時: 2004-04-08 15:11
文字列の暗号を考えています
例として konnitiwa という文字列を 1W45R というkeyで暗号化して
E1F4wd という文字列になりこれを 1W45R というkeyで復号すると konnitiwa
になるというような単純なものでいいのですが
やり方がわからないので教えてください
いっきゅう
大ベテラン
会議室デビュー日: 2004/04/04
投稿数: 153
お住まい・勤務地: 兵庫
投稿日時: 2004-04-08 15:24
以前作ったコードがあったんでそのままペタ
意味は各クラスをjavadocで勉強すべきでしょう。
----------------------------------------------------------------
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class MyCrypt {

PBEParameterSpec pbeParamSpec;
char[] pass = "MyCrypt".toCharArray();
SecureRandom random = new SecureRandom();

public MyCrypt(String passwd){

// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};

// Iteration count
int count = 20;

// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);

pass = passwd.toCharArray();
}

public boolean decrypt(InputStream in, OutputStream out) {

try {
PBEKeySpec pbe = new PBEKeySpec(pass);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey sk = skf.generateSecret(pbe);

Cipher cip = Cipher.getInstance("PBEWithMD5AndDES");

cip.init(Cipher.DECRYPT_MODE, sk, pbeParamSpec);

int tmp = 0;
byte[] buf = new byte[1024];

while( (tmp = in.read(buf) ) != -1 ) {
out.write(cip.update(buf,0,tmp));
}

byte[] buftmp = cip.doFinal();
if ( buftmp != null ) {
out.write(buftmp);
}

}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
public boolean decryptFile(File in, File out) throws IOException,FileNotFoundException{
FileInputStream inF = new FileInputStream(in);
FileOutputStream outF = new FileOutputStream(out);
boolean ret = decrypt(inF,outF);
inF.close();
outF.close();
return ret;
}

public boolean decryptFile(String in, String out) throws IOException,FileNotFoundException {
File inF = new File(in);
File outF = new File(out);
return decryptFile(inF,outF);
}

public boolean encrypt(InputStream in, OutputStream out) {

try {
PBEKeySpec pbe = new PBEKeySpec(pass);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey sk = skf.generateSecret(pbe);

Cipher cip = Cipher.getInstance("PBEWithMD5AndDES");

cip.init(Cipher.ENCRYPT_MODE, sk, pbeParamSpec,random);

int tmp = 0;
byte[] buf = new byte[1024];

while( (tmp = in.read(buf) ) != -1 ) {
out.write(cip.update(buf,0,tmp));
}

byte[] buftmp = cip.doFinal();
if ( buftmp != null ) {
out.write(buftmp);
}

}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
public boolean encryptFile(File in, File out) throws IOException,FileNotFoundException{
FileInputStream inF = new FileInputStream(in);
FileOutputStream outF = new FileOutputStream(out);
boolean ret = encrypt(inF,outF);
inF.close();
outF.close();
return ret;
}

public boolean encryptFile(String in, String out) throws IOException,FileNotFoundException {
File inF = new File(in);
File outF = new File(out);
return encryptFile(inF,outF);
}

public static void main(String[] args) {
MyCrypt c = new MyCrypt("test");
try {
if ( c.encryptFile("d:\\a.txt","d:\\b.txt") ) {
c.decryptFile("d:\\b.txt", "d:\\c.txt");
}
}catch(Exception e ){
e.printStackTrace();
}

}

}
kirito
常連さん
会議室デビュー日: 2004/03/19
投稿数: 24
投稿日時: 2004-04-08 15:29
ありがとうございます
しかし私の力では難解すぎます..

[ メッセージ編集済み 編集者: kirito 編集日時 2004-04-08 15:29 ]
kabura
会議室デビュー日: 2004/04/05
投稿数: 3
投稿日時: 2004-04-08 23:32
いい 勉強に なりました。
ありがとう ございます。
1

スキルアップ/キャリアアップ(JOB@IT)