Inhibiting corrosion of metals by anodic or cathodic protection