Java
Very portable code
This one should also be added ... Run it .. javac ArchlinuxIsTheBest.java java ArchlinuxIsTheBest It works on windows, mac, linux, bsd, solaris .... (portable, WORA code)
public class ArchlinuxIsTheBest {
public static void main(String args[]) {
System.out.println("\nArch is the best!!\n");
}
}
~ paramthegreat, 2008-05-14 14:33:23
Public Key Verification
Hi all. I developed a java version of the program which provide two functionalities: - create a signed file of the message "ArchLinux is the best!" and export the public key - verify that a signed message file is the really true and correct message "ArchLinux is the best!" I used a self-signed certificate, but we can ask for a certificate form a recognized Certification Authority, in this way we could distribute a signed and secured "ArchLinux is the best!" message around the World. Here a demonstration code:
[Signer]
package it.la41318.archlinux;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
public class ArchLinuxSigner
{
public static void main(String[] args)
{
try
{
KeyPairGenerator keyGen=KeyPairGenerator.getInstance("DSA", "SUN");
SecureRandom random=SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(priv);
dsa.update("ArchLinux is the best!".getBytes());
byte[] realSig = dsa.sign();
FileOutputStream sigfos = new FileOutputStream("archlinux.signature");
sigfos.write(realSig);
sigfos.close();
byte[] key = pub.getEncoded();
FileOutputStream keyfos = new FileOutputStream("archlinux.pk");
keyfos.write(key);
keyfos.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
[Verifier]
package it.la41318.archlinux;
import java.io.FileInputStream;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
public class ArchLinuxMessage
{
public static void main(String[] args)
{
try
{
FileInputStream keyfis = new FileInputStream("archlinux.pk");
byte[] encKey = new byte[keyfis.available()];
keyfis.read(encKey);
keyfis.close();
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
FileInputStream sigfis = new FileInputStream("archlinux.signature");
byte[] sigToVerify = new byte[sigfis.available()];
sigfis.read(sigToVerify);
sigfis.close();
Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
sig.initVerify(pubKey);
sig.update("ArchLinux is the best!".getBytes());
boolean verifies = sig.verify(sigToVerify);
if (verifies) System.out.println("Yes, ArchLinux is the best!");
else System.out.println("ATTENTION: this is not the real ArchLinux message");
} catch (Exception e)
{
e.printStackTrace();
}
}
}
~ pulsar1971, 2008-05-14 14:40:34